Skip to content

Fix `rails db:*` when the database does not exist

Kassio Borges requested to merge fix-rake-db-tasks into master

What does this MR do?

Fix rails db:* when the database does not exist

Currently tasks like rails db:drop && rails db:create fails due to the console message that tries to show the database version and since the database was just dropped it fails. This fix just verify if the database exists before printing messages that depends on the database.

$ rails db:drop
WARNING: This version of GitLab depends on gitlab-shell 11.0.0, but you're running Unknown. Please update gitlab-shell.
--------------------------------------------------------------------------------
 GitLab:       12.7.0-pre (1c4e95cbf51) EE
 GitLab Shell: Unknown
 PostgreSQL:   10.11
--------------------------------------------------------------------------------
Dropped database 'gitlabhq_development'
Dropped database 'gitlabhq_test'
~/src/gdk/gitlab (master u=)
$ rails db:create
WARNING: This version of GitLab depends on gitlab-shell 11.0.0, but you're running Unknown. Please update gitlab-shell.
--------------------------------------------------------------------------------
 GitLab:       12.7.0-pre (1c4e95cbf51) EE
 GitLab Shell: Unknown
rails aborted!
ActiveRecord::NoDatabaseError: FATAL:  database "gitlabhq_development" does not exist
/Users/kassioborges/src/gdk/gitlab/lib/gitlab/database.rb:226:in `connection'
/Users/kassioborges/src/gdk/gitlab/lib/gitlab/database.rb:239:in `database_version'
/Users/kassioborges/src/gdk/gitlab/lib/gitlab/database.rb:83:in `version'
/Users/kassioborges/src/gdk/gitlab/config/initializers/console_message.rb:9:in `<main>'
/Users/kassioborges/src/gdk/gitlab/config/environment.rb:6:in `<main>'
bin/rails:4:in `<main>'

Caused by:
PG::ConnectionBad: FATAL:  database "gitlabhq_development" does not exist
/Users/kassioborges/src/gdk/gitlab/lib/gitlab/database.rb:226:in `connection'
/Users/kassioborges/src/gdk/gitlab/lib/gitlab/database.rb:239:in `database_version'
/Users/kassioborges/src/gdk/gitlab/lib/gitlab/database.rb:83:in `version'
/Users/kassioborges/src/gdk/gitlab/config/initializers/console_message.rb:9:in `<main>'
/Users/kassioborges/src/gdk/gitlab/config/environment.rb:6:in `<main>'
bin/rails:4:in `<main>'
Tasks: TOP => db:create => db:load_config => environment
(See full trace by running task with --trace)

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • Security reports checked/validated by a reviewer from the AppSec team
Edited by Kassio Borges

Merge request reports