Restore from backup fails due to ActiveRecord::IrreversibleOrderError
When the database is empty, and an admin attempts to restore from backup (e.g. gitlab-rake gitlab:backup:restore force=yes BACKUP=1573704651_2019_11_14_12.4.1-ee
), the following error occurs:
+ gitlab-rake gitlab:backup:restore force=yes BACKUP=1573704651_2019_11_14_12.4.1-ee
rake aborted!
ActiveRecord::IrreversibleOrderError: Relation has no current order and table has no primary key to be used as default order
/opt/gitlab/embedded/service/gitlab-rails/app/models/concerns/cacheable_attributes.rb:19:in `current_without_cache'
/opt/gitlab/embedded/service/gitlab-rails/app/models/concerns/cacheable_attributes.rb:63:in `rescue in current'
/opt/gitlab/embedded/service/gitlab-rails/app/models/concerns/cacheable_attributes.rb:51:in `current'
/opt/gitlab/embedded/service/gitlab-rails/lib/gitlab/current_settings.rb:48:in `uncached_application_settings'
/opt/gitlab/embedded/service/gitlab-rails/lib/gitlab/current_settings.rb:30:in `ensure_application_settings!'
/opt/gitlab/embedded/service/gitlab-rails/lib/gitlab/current_settings.rb:7:in `block in current_application_settings'
/opt/gitlab/embedded/service/gitlab-rails/lib/gitlab/null_request_store.rb:34:in `fetch'
/opt/gitlab/embedded/service/gitlab-rails/lib/gitlab/safe_request_store.rb:12:in `fetch'
/opt/gitlab/embedded/service/gitlab-rails/lib/gitlab/current_settings.rb:7:in `current_application_settings'
/opt/gitlab/embedded/service/gitlab-rails/lib/gitlab/current_settings.rb:20:in `method_missing'
/opt/gitlab/embedded/service/gitlab-rails/config/settings.rb:181:in `cron_for_usage_ping'
/opt/gitlab/embedded/service/gitlab-rails/config/initializers/1_settings.rb:433:in `<top (required)>'
/opt/gitlab/embedded/service/gitlab-rails/config/environment.rb:6:in `<top (required)>'
/opt/gitlab/embedded/bin/bundle:23:in `load'
/opt/gitlab/embedded/bin/bundle:23:in `<main>'
This happens because the Rake task is attempting to load cron_for_usage_ping
in https://gitlab.com/gitlab-org/gitlab/blob/c77bb71ca5944cdaa6d045ee821c29d809ef2624/config/settings.rb#L181 from the database. Since the database schema hasn't been populated yet, this fails.
Normally this works because Rake bypasses the loading if connect_to_db?
is false
in https://gitlab.com/gitlab-org/gitlab/blob/af4a597d3fb687ed2841bb755403f66cf131bdff/lib/gitlab/current_settings.rb#L46. This checks for the existence of application_settings
in https://gitlab.com/gitlab-org/gitlab/blob/af4a597d3fb687ed2841bb755403f66cf131bdff/lib/gitlab/current_settings.rb#L76-77.
Perhaps the workaround is to drop that table?