Skip to content

Commit

Permalink
Reliably sort functions, views, and materialized views in schema (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwestendorf authored Nov 18, 2024
1 parent 83675de commit 063a2e4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ def views(name = nil)
result['data'].flatten
end

def materialized_views(name = nil)
result = do_system_execute("SHOW TABLES WHERE engine = 'MaterializedView'", name)
return [] if result.nil?
result['data'].flatten
end

def functions
result = do_system_execute("SELECT name FROM system.functions WHERE origin = 'SQLUserDefined'")
return [] if result.nil?
Expand Down
9 changes: 6 additions & 3 deletions lib/clickhouse-activerecord/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ def dump(connection = ActiveRecord::Base.connection, stream = STDOUT, config = A
private

def tables(stream)
functions = @connection.functions
functions = @connection.functions.sort
functions.each do |function|
function(function, stream)
end

sorted_tables = @connection.tables.sort {|a,b| @connection.show_create_table(a).match(/^CREATE\s+(MATERIALIZED\s+)?VIEW/) ? 1 : a <=> b }
sorted_tables.each do |table_name|
view_tables = @connection.views.sort
materialized_view_tables = @connection.materialized_views.sort
sorted_tables = @connection.tables.sort - view_tables - materialized_view_tables

(sorted_tables + view_tables + materialized_view_tables).each do |table_name|
table(table_name, stream) unless ignored?(table_name)
end
end
Expand Down

0 comments on commit 063a2e4

Please sign in to comment.