Skip to content

Commit

Permalink
Add support for force CREATE FUNCTION (#146)
Browse files Browse the repository at this point in the history
* Switch from `CREATE FUNCTION` to `CREATE OR REPLACE FUNCTION`

In a rails app, with existing Clickhouse schema w/a function defined, a `rails db:schema:load` will not always succeed; this is because the function may already exist.

```
Code: 609. DB::Exception: User-defined function 'uuid7ToDateTime' already exists. (FUNCTION_ALREADY_EXISTS) (version 23.9.6.20 (official build))
```
  • Loading branch information
danielwestendorf authored Oct 9, 2024
1 parent f730c49 commit 5cb4ea7
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 5 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 @@ -356,8 +356,8 @@ def create_table(table_name, **options, &block)
end
end

def create_function(name, body)
fd = "CREATE FUNCTION #{apply_cluster(quote_table_name(name))} AS #{body}"
def create_function(name, body, **options)
fd = "CREATE#{' OR REPLACE' if options[: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
5 changes: 4 additions & 1 deletion spec/cluster/migration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,12 @@
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
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 5cb4ea7

Please sign in to comment.