Skip to content

Conversation

danielwestendorf
Copy link
Contributor

@danielwestendorf danielwestendorf commented Jul 25, 2024

Fixes ##145

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))

This can be addressed in at least two ways:

  1. Guard the CREATE FUNCTION statement with IF NOT EXISTS; this would prevent trying to create the function, however, it also does not guarantee that the function is up-to-date with the schema
  2. Use CREATE OR REPLACE FUNCTION instead. This has the benefit of ensuring the function is up-to-date with the schema

This commit pursues option #2. It adds a force option to the create_function method definition; force: true us used when dumping the schema so that schema can be dumped/loaded idempotently.

@PNixx
Copy link
Owner

PNixx commented Jul 26, 2024

I think we need make force option like a table create and use it in schema by default.

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))
```

This can be addressed in at least two ways:
1. Guard the `CREATE FUNCTION` statement with `IF NOT EXISTS`; this would prevent trying to create the function, however, it also does not guarantee that the function is up-to-date with the schema
2. Use `CREATE OR REPLACE FUNCTION` instead. This has the benefit of ensuring the function is up-to-date with the schema

This commit pursues option #2.
@danielwestendorf danielwestendorf force-pushed the create_or_replace_function_main branch from 0efbcb6 to 23ba2e2 Compare October 4, 2024 18:17
@danielwestendorf
Copy link
Contributor Author

I've added the force option which is automatically added to the schema when dumped.

@danielwestendorf danielwestendorf changed the title Switch from CREATE FUNCTION to CREATE OR REPLACE FUNCTION Add support for CREATE FUNCTION and CREATE OR REPLACE FUNCTION; later in schema loading Oct 4, 2024
@danielwestendorf danielwestendorf changed the title Add support for CREATE FUNCTION and CREATE OR REPLACE FUNCTION; later in schema loading Add support for CREATE FUNCTION and CREATE OR REPLACE FUNCTION; the later in schema loading Oct 5, 2024
@PNixx PNixx merged commit 5cb4ea7 into PNixx:master Oct 9, 2024
16 checks passed
dmitrytrager added a commit to mayak-bz/clickhouse-activerecord that referenced this pull request Jan 20, 2025
* new-master: (77 commits)
  Use a flag to track updates/deletes instead of guessing (PNixx#188)
  Do not squish table definitions in schema dump. (PNixx#187)
  Add support for integer limits in map type (PNixx#178)
  Improve function dumps (PNixx#179)
  Reliably sort functions, views, and materialized views in schema (PNixx#181)
  Maintain primary key type specificity (PNixx#183)
  Support create table with request settings
  Support codec column PNixx#135, refactoring
  Add Array support to Map (PNixx#158)
  SchemaDumper adds materialized view destination (PNixx#159)
  Improve DB::Exception error handling (PNixx#165)
  Add blank line after create_function in schema (PNixx#170)
  Include column definitions in schema dump if the column name is not `id` (PNixx#173)
  Add support for LIMIT BY clause (PNixx#169)
  Add support for force `CREATE FUNCTION` (PNixx#146)
  Implement GROUP BY GROUPING SETS with new `group_by_grouping_sets` query method. (PNixx#161)
  fix for function creation in structure.sql (PNixx#166)
  fix schema dumper
  fix spec
  fix detect model primary key for older clickhouse versions
  ...
dmitrytrager added a commit to mayak-bz/clickhouse-activerecord that referenced this pull request Jan 20, 2025
* new-master: (77 commits)
  Use a flag to track updates/deletes instead of guessing (PNixx#188)
  Do not squish table definitions in schema dump. (PNixx#187)
  Add support for integer limits in map type (PNixx#178)
  Improve function dumps (PNixx#179)
  Reliably sort functions, views, and materialized views in schema (PNixx#181)
  Maintain primary key type specificity (PNixx#183)
  Support create table with request settings
  Support codec column PNixx#135, refactoring
  Add Array support to Map (PNixx#158)
  SchemaDumper adds materialized view destination (PNixx#159)
  Improve DB::Exception error handling (PNixx#165)
  Add blank line after create_function in schema (PNixx#170)
  Include column definitions in schema dump if the column name is not `id` (PNixx#173)
  Add support for LIMIT BY clause (PNixx#169)
  Add support for force `CREATE FUNCTION` (PNixx#146)
  Implement GROUP BY GROUPING SETS with new `group_by_grouping_sets` query method. (PNixx#161)
  fix for function creation in structure.sql (PNixx#166)
  fix schema dumper
  fix spec
  fix detect model primary key for older clickhouse versions
  ...
dmitrytrager added a commit to mayak-bz/clickhouse-activerecord that referenced this pull request Jan 21, 2025
* new-master: (77 commits)
  Use a flag to track updates/deletes instead of guessing (PNixx#188)
  Do not squish table definitions in schema dump. (PNixx#187)
  Add support for integer limits in map type (PNixx#178)
  Improve function dumps (PNixx#179)
  Reliably sort functions, views, and materialized views in schema (PNixx#181)
  Maintain primary key type specificity (PNixx#183)
  Support create table with request settings
  Support codec column PNixx#135, refactoring
  Add Array support to Map (PNixx#158)
  SchemaDumper adds materialized view destination (PNixx#159)
  Improve DB::Exception error handling (PNixx#165)
  Add blank line after create_function in schema (PNixx#170)
  Include column definitions in schema dump if the column name is not `id` (PNixx#173)
  Add support for LIMIT BY clause (PNixx#169)
  Add support for force `CREATE FUNCTION` (PNixx#146)
  Implement GROUP BY GROUPING SETS with new `group_by_grouping_sets` query method. (PNixx#161)
  fix for function creation in structure.sql (PNixx#166)
  fix schema dumper
  fix spec
  fix detect model primary key for older clickhouse versions
  ...
tylerCaineRhodes pushed a commit to magellan-ai/clickhouse-activerecord that referenced this pull request Jul 25, 2025
* 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))
```
syabruk added a commit to machinio/clickhouse-activerecord that referenced this pull request Aug 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants