clickhouse-activerecord icon indicating copy to clipboard operation
clickhouse-activerecord copied to clipboard

Cannot run migrations with multiple databases

Open eni9889 opened this issue 3 years ago • 2 comments

Hi,

I am getting the following error:

ActiveRecord::AdapterNotSpecified: The `development_clickhouse` database is not configured for the `development` environment.

When running

bundle exec rake clickhouse:migrate

This is my database.yml

default: &default
  statistics:
    adapter: clickhouse
    database: statistics
    user: <%= ENV.fetch("DB_USER") { "default" }%>
    host: <%= ENV["DB_HOST"] %>
    port: <%= ENV.fetch("DB_PORT") { "8123" }%>
    pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i + 2 %>
    password: <%= ENV["DB_PASSWORD"] %>
    migrations_paths: db/statistics_migrate

  analytics:
    adapter: clickhouse
    database: analytics
    user: <%= ENV.fetch("DB_USER") { "default" }%>
    host: <%= ENV["DB_HOST"] %>
    port: <%= ENV.fetch("DB_PORT") { "8123" }%>
    pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i + 2 %>
    password: <%= ENV["DB_PASSWORD"] %>
    migrations_paths: db/analytics_migrate

development:
  <<: *default

If I run

bundle exec rake clickhouse:prepare_schema_migration_table
bundle exec rake clickhouse:prepare_internal_metadata_table
bundle exec rake db:migrate

I get

rake aborted!
ActiveRecord::StatementInvalid: ActiveRecord::ActiveRecordError: Response code: 404:
Code: 47, e.displayText() = DB::Exception: There's no column 'schema_migrations.active' in table 'schema_migrations' (version 20.8.7.15 (official build))
/Users/egjoka/.asdf/installs/ruby/2.6.6/bin/bundle:23:in `load'
/Users/egjoka/.asdf/installs/ruby/2.6.6/bin/bundle:23:in `<main>'

eni9889 avatar Feb 15 '22 17:02 eni9889

Hey @eni9889, looks like you're wanting to use multiple databases within your Rails app. If that's the case you'll need to reconfigure your database.yml file to something like:

clickhouse_default: &clickhouse_default
  adapter: clickhouse
  user: <%= ENV.fetch("DB_USER") { "default" }%>
  host: <%= ENV["DB_HOST"] %>
  port: <%= ENV.fetch("DB_PORT") { "8123" }%>
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i + 2 %>
  password: <%= ENV["DB_PASSWORD"] %>

statistics_default: &statistics_default
  <<: *clickhouse_default
  database: statistics
  migrations_paths: db/statistics_migrate

analytics_default: &analytics_default
  <<: *clickhouse_default
  database: analytics
  migrations_paths: db/analytics_migrate

Since the connection parameters are the same for both the statistics and analytics database, I combined those defaults into one yml alias called clickhouse_default. This allows you to inherit the connection parameters in your child configs.

From there, and still within the database.yml file, you'll need to define the databases in each environment you have. For example:

development:
  statistics:
    <<: *statistics_default

  analytics:
    <<: *analytics_default

test:
  statistics:
    <<: *statistics_default

  analytics:
    <<: *analytics_default

production:
  statistics:
    <<: *statistics_default

  analytics:
    <<: *analytics_default

When this is configured your rake commands will actually become:

bundle exec rails db:migrate:statistics
bundle exec rails db:migrate:analytics

Let me know if this works for you or if you have additional questions!

jlholm avatar Apr 08 '22 20:04 jlholm

For what it's worth—I also had this problem trying to run the clickhouse:____ rake tasks, and it took me a while to figure out that I should be using the db:_____:clickhouse tasks instead. The clickhouse:____ tasks always seem to look for development_clickhouse, and I think it won't find it because the database is called clickhouse in Rails 6.

I thought it might just be the documentation heading levels that are causing confusion here, maybe? The Rails 6 section of the readme makes repeated reference to rake clickhouse:____ commands.

Thanks for your work on this gem!

czlee avatar May 26 '22 01:05 czlee

fixed in v1.0.0

PNixx avatar Nov 29 '23 20:11 PNixx