AlchemyCMS / alchemy_cms

Alchemy is the Open Source Rails CMS framework for the component based web that can be used as classic server side rendered or headless CMS.
https://www.alchemy-cms.com
BSD 3-Clause "New" or "Revised" License
846 stars 315 forks source link

Installation does not work when using DB read-replica #2265

Closed dssjoblom closed 2 years ago

dssjoblom commented 2 years ago

Description

When installing (rake alchemy:install) with a DB replica setup, e.g. something along the lines of

development:
  primary:
    database: primary
    username: <%= ENV["DB_USERNAME"] %>
    password: <%= ENV["DB_PASSWORD"] %>
    host: localhost
    port: 5432
  replica:
    database: replica
    username: <%= ENV["DB_REPLICA_USERNAME"] %>
    password: <%= ENV["DB_REPLICA_PASSWORD"] %>
    host: localhost
    port: 5432

in database.yml, the installer fails with an exception.

Expected behavior

The installation should work normally. Specifically, it should not try to do anything with the replica.

Actual behavior

Installation fails with an exception:

PG::InsufficientPrivilege: ERROR:  permission denied to create database
Couldn't create 'replica' database. Please check your configuration.
rake aborted!
ActiveRecord::StatementInvalid: PG::InsufficientPrivilege: ERROR:  permission denied to create database
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activerecord-6.1.4.7/lib/active_record/connection_adapters/postgresql/database_statements.rb:47:in `exec'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activerecord-6.1.4.7/lib/active_record/connection_adapters/postgresql/database_statements.rb:47:in `block (2 levels) in execute'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activesupport-6.1.4.7/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activesupport-6.1.4.7/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activesupport-6.1.4.7/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activerecord-6.1.4.7/lib/active_record/connection_adapters/postgresql/database_statements.rb:46:in `block in execute'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activerecord-6.1.4.7/lib/active_record/connection_adapters/abstract_adapter.rb:696:in `block (2 levels) in log'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activesupport-6.1.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activesupport-6.1.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activesupport-6.1.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activesupport-6.1.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activesupport-6.1.4.7/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activerecord-6.1.4.7/lib/active_record/connection_adapters/abstract_adapter.rb:695:in `block in log'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activesupport-6.1.4.7/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activerecord-6.1.4.7/lib/active_record/connection_adapters/abstract_adapter.rb:687:in `log'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activerecord-6.1.4.7/lib/active_record/connection_adapters/postgresql/database_statements.rb:45:in `execute'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activerecord-6.1.4.7/lib/active_record/connection_adapters/postgresql/schema_statements.rb:46:in `create_database'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activerecord-6.1.4.7/lib/active_record/tasks/postgresql_database_tasks.rb:26:in `create'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activerecord-6.1.4.7/lib/active_record/tasks/database_tasks.rb:133:in `create'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activerecord-6.1.4.7/lib/active_record/tasks/database_tasks.rb:193:in `block in create_current'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activerecord-6.1.4.7/lib/active_record/tasks/database_tasks.rb:506:in `block (2 levels) in each_current_configuration'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activerecord-6.1.4.7/lib/active_record/tasks/database_tasks.rb:503:in `each'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activerecord-6.1.4.7/lib/active_record/tasks/database_tasks.rb:503:in `block in each_current_configuration'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activerecord-6.1.4.7/lib/active_record/tasks/database_tasks.rb:502:in `each'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activerecord-6.1.4.7/lib/active_record/tasks/database_tasks.rb:502:in `each_current_configuration'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activerecord-6.1.4.7/lib/active_record/tasks/database_tasks.rb:193:in `create_current'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/activerecord-6.1.4.7/lib/active_record/railties/databases.rake:45:in `block (2 levels) in <main>'
/home/daniel/.rvm/gems/ruby-2.7.5/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
/home/daniel/.rvm/gems/ruby-2.7.5/bin/ruby_executable_hooks:22:in `eval'
/home/daniel/.rvm/gems/ruby-2.7.5/bin/ruby_executable_hooks:22:in `<main>'

System configuration

tvdeyen commented 2 years ago

You are missing the privilege to create the database. We could add an option to skip creation of database during install.

tvdeyen commented 2 years ago

@dssjoblom I added an option to the installer. Can you try with

# Gemfile
gem "alchemy_cms", github: "tvdeyen/alchemy_cms", branch: "installer/skip-db-migrate"
dssjoblom commented 2 years ago

@tvdeyen thanks for the response. However, I realized that the problem was actually in the database config, as it failed to set replica: true on the replica instance. After fixing this, the installation works even without the --skip-db-create flag.