From 063a2e4fa7f8d65de2e3fa0f7cbcb47f68f34116 Mon Sep 17 00:00:00 2001 From: Daniel Westendorf Date: Mon, 18 Nov 2024 07:04:22 -0700 Subject: [PATCH] Reliably sort functions, views, and materialized views in schema (#181) --- .../connection_adapters/clickhouse/schema_statements.rb | 6 ++++++ lib/clickhouse-activerecord/schema_dumper.rb | 9 ++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/active_record/connection_adapters/clickhouse/schema_statements.rb b/lib/active_record/connection_adapters/clickhouse/schema_statements.rb index a4e73ff..a275477 100644 --- a/lib/active_record/connection_adapters/clickhouse/schema_statements.rb +++ b/lib/active_record/connection_adapters/clickhouse/schema_statements.rb @@ -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? diff --git a/lib/clickhouse-activerecord/schema_dumper.rb b/lib/clickhouse-activerecord/schema_dumper.rb index 422b34b..f244de4 100644 --- a/lib/clickhouse-activerecord/schema_dumper.rb +++ b/lib/clickhouse-activerecord/schema_dumper.rb @@ -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