diff --git a/lib/active_record/connection_adapters/clickhouse/schema_statements.rb b/lib/active_record/connection_adapters/clickhouse/schema_statements.rb index a275477a..9dfe9733 100644 --- a/lib/active_record/connection_adapters/clickhouse/schema_statements.rb +++ b/lib/active_record/connection_adapters/clickhouse/schema_statements.rb @@ -79,7 +79,7 @@ def materialized_views(name = nil) end def functions - result = do_system_execute("SELECT name FROM system.functions WHERE origin = 'SQLUserDefined'") + result = do_system_execute("SELECT name FROM system.functions WHERE origin = 'SQLUserDefined' ORDER BY name") return [] if result.nil? result['data'].flatten end diff --git a/lib/clickhouse-activerecord/tasks.rb b/lib/clickhouse-activerecord/tasks.rb index 0551d7b6..7440a17f 100644 --- a/lib/clickhouse-activerecord/tasks.rb +++ b/lib/clickhouse-activerecord/tasks.rb @@ -47,12 +47,12 @@ def structure_dump(*args) tables.sort_by! {|table| table.match(/^CREATE\s+(MATERIALIZED\s+)?VIEW/) ? 1 : 0} # get all functions - functions = connection.execute("SELECT create_query FROM system.functions WHERE origin = 'SQLUserDefined'")['data'].flatten + functions = connection.execute("SELECT create_query FROM system.functions WHERE origin = 'SQLUserDefined' ORDER BY name")['data'].flatten # put to file File.open(args.first, 'w:utf-8') do |file| functions.each do |function| - file.puts function + ";\n\n" + file.puts function.gsub('\\n', "\n") + ";\n\n" end tables.each do |table| diff --git a/spec/fixtures/migrations/plain_function_creation/1_create_some_function.rb b/spec/fixtures/migrations/plain_function_creation/1_create_some_function.rb index 329dc7f5..ede6a3e2 100644 --- a/spec/fixtures/migrations/plain_function_creation/1_create_some_function.rb +++ b/spec/fixtures/migrations/plain_function_creation/1_create_some_function.rb @@ -3,7 +3,12 @@ class CreateSomeFunction < ActiveRecord::Migration[7.1] def up sql = <<~SQL - CREATE FUNCTION some_fun AS (x,y) -> x + y + CREATE FUNCTION multFun AS (x,y) -> x * y + SQL + do_execute(sql, format: nil) + + sql = <<~SQL + CREATE FUNCTION addFun AS (x,y) -> x + y SQL do_execute(sql, format: nil) end diff --git a/spec/single/migration_spec.rb b/spec/single/migration_spec.rb index d4c11c81..9b1c574f 100644 --- a/spec/single/migration_spec.rb +++ b/spec/single/migration_spec.rb @@ -381,7 +381,7 @@ it 'creates a function' do subject - expect(ActiveRecord::Base.connection.functions).to match_array(['some_fun']) + expect(ActiveRecord::Base.connection.functions).to match_array(['addFun', 'multFun']) end end @@ -392,7 +392,7 @@ subject - expect(ActiveRecord::Base.connection.functions).to match_array(['some_fun', 'forced_fun']) + expect(ActiveRecord::Base.connection.functions).to match_array(['forced_fun', 'some_fun']) expect(ActiveRecord::Base.connection.show_create_function('forced_fun').chomp).to eq('CREATE FUNCTION forced_fun AS (x, y) -> (x + y)') end end