PNixx / clickhouse-activerecord

A Ruby database ActiveRecord driver for ClickHouse
MIT License
198 stars 100 forks source link

Don't throw exception when creating database #121

Open PoroshkinaVV opened 8 months ago

PoroshkinaVV commented 8 months ago

This PR aims to fix exception when creating database.

bundle exec rake db:create
Response code: 200:
clickhouse-5.my-cluster.cloud.b-pl.pro 9000 82 Code: 82. DB::Exception: Database my_database already exists. (DATABASE_ALREADY_EXISTS) (version 23.8.3.48 (official build)) 
Code: 82. DB::Exception: There was an error on [clickhouse-5.my-cluster.cloud.b-pl.pro:9000]: Code: 82. DB::Exception: Database my_database already exists. (DATABASE_ALREADY_EXISTS) (version 23.8.3.48 (official build)). (DATABASE_ALREADY_EXISTS) (version 23.8.3.48 (official build))
Couldn't create 'my_database' database. Please check your configuration.
rake aborted!

Clickhouse docs:

Clause IF NOT EXISTS
If the db_name database already exists, then ClickHouse does not create a new database and:

Doesn’t throw an exception if clause is specified.
Throws an exception if clause isn’t specified.
PoroshkinaVV commented 8 months ago

@PNixx, what do you think?

PNixx commented 8 months ago

I will check

PNixx commented 7 months ago
% rake db:create:clickhouse
Database 'clickhouse_test' already exists

Correct behavior when creating a database. Write your version of Ruby, Rails and ClickHouse.

PoroshkinaVV commented 7 months ago

Ruby: 3.2.0 Rails: 7.1.3 Clickhouse: 23.8.3.48

PNixx commented 7 months ago

gem version?

PoroshkinaVV commented 7 months ago

latest: 1.0.4

PoroshkinaVV commented 7 months ago

I also made this change based on method drop database. Not only because I have this error above.

It contains directive 'IF EXISTS', but create database for some reason doesn't have 'IF NOT EXISTS': https://github.com/PNixx/clickhouse-activerecord/blob/8e4ea2ca1fd8609d118e9cc2a4ff30bf441345aa/lib/active_record/connection_adapters/clickhouse_adapter.rb#L312

PNixx commented 3 months ago

Sorry, I mistake. You set in database.yml cluster information?

  cluster_name: 'cluster_name' # optional for creating tables in cluster 
  replica_name: '{replica}' # replica macros name, optional for creating replicated tables
PoroshkinaVV commented 3 months ago

@PNixx yes