Skip to content

Commit

Permalink
Add force option for create or replace
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwestendorf committed Oct 4, 2024
1 parent fda4bb2 commit 23ba2e2
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/active_record/connection_adapters/clickhouse_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ def create_table(table_name, **options, &block)
end
end

def create_function(name, body)
fd = "CREATE OR REPLACE FUNCTION #{apply_cluster(quote_table_name(name))} AS #{body}"
def create_function(name, body, force: false)
fd = "CREATE#{' OR REPLACE' if force} FUNCTION #{apply_cluster(quote_table_name(name))} AS #{body}"
do_execute(fd, format: nil)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/clickhouse-activerecord/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def function(function, stream)
stream.puts " # FUNCTION: #{function}"
sql = @connection.show_create_function(function)
stream.puts " # SQL: #{sql}" if sql
stream.puts " create_function \"#{function}\", \"#{sql.gsub(/^CREATE FUNCTION (.*?) AS/, '').strip}\"" if sql
stream.puts " create_function \"#{function}\", \"#{sql.gsub(/^CREATE FUNCTION (.*?) AS/, '').strip}\", force: true" if sql
end

def format_options(options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
class CreateSomeFunction < ActiveRecord::Migration[7.1]
def up
create_function :some_fun, "(x,y) -> x + y"
create_function :forced_fun, "(x,y) -> x + y", force: true
end
end
5 changes: 4 additions & 1 deletion spec/single/migration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,12 @@
context 'dsl' do
let(:directory) { 'dsl_create_function' }
it 'creates a function' do
ActiveRecord::Base.connection.do_execute('CREATE FUNCTION forced_fun AS (x, k, b) -> k*x + b', format: nil)

subject

expect(ActiveRecord::Base.connection.functions).to match_array(['some_fun'])
expect(ActiveRecord::Base.connection.functions).to match_array(['some_fun', 'forced_fun'])
expect(ActiveRecord::Base.connection.show_create_function('forced_fun').chomp).to eq('CREATE FUNCTION forced_fun AS (x, y) -> (x + y)')
end
end
end
Expand Down

0 comments on commit 23ba2e2

Please sign in to comment.