From de399406f2d78cc47b2bd5bdcaf9a099e3eb9e5f Mon Sep 17 00:00:00 2001 From: Chris Bisnett Date: Wed, 23 Oct 2024 07:10:48 -0400 Subject: [PATCH] SchemaDumper adds materialized view destination (#159) --- lib/clickhouse-activerecord/schema_dumper.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/clickhouse-activerecord/schema_dumper.rb b/lib/clickhouse-activerecord/schema_dumper.rb index f727d84..d3990fe 100644 --- a/lib/clickhouse-activerecord/schema_dumper.rb +++ b/lib/clickhouse-activerecord/schema_dumper.rb @@ -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 @@ -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' }) @@ -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"