Skip to content

Commit

Permalink
SchemaDumper adds materialized view destination (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbisnett authored Oct 23, 2024
1 parent 41c952f commit de39940
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/clickhouse-activerecord/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def table(table, stream)
# super(table.gsub(/^\.inner\./, ''), stream)

# detect view table
match = sql.match(/^CREATE\s+(MATERIALIZED\s+)?VIEW/)
view_match = sql.match(/^CREATE\s+(MATERIALIZED\s+)?VIEW\s+\S+\s+(TO (\S+))?/)
end

# Copy from original dumper
Expand All @@ -50,8 +50,9 @@ def table(table, stream)

unless simple
# Add materialize flag
tbl.print ', view: true' if match
tbl.print ', materialized: true' if match && match[1].presence
tbl.print ', view: true' if view_match
tbl.print ', materialized: true' if view_match && view_match[1].presence
tbl.print ", to: \"#{view_match[3]}\"" if view_match && view_match[3].presence
end

if (id = columns.detect { |c| c.name == 'id' })
Expand All @@ -75,7 +76,7 @@ def table(table, stream)
tbl.puts ", force: :cascade do |t|"

# then dump all non-primary key columns
if simple || !match
if simple || !view_match
columns.each do |column|
raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" unless @connection.valid_type?(column.type)
next if column.name == pk && column.name == "id"
Expand Down

0 comments on commit de39940

Please sign in to comment.