Skip to content

Commit

Permalink
Improve function dumps (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-dumit authored Nov 18, 2024
1 parent 063a2e4 commit 41edbf7
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/clickhouse-activerecord/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/single/migration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 41edbf7

Please sign in to comment.