Move min schema version check to db:migrate

Rather than have it checked only as part of gitlab:db:configure, we will
instead have it as a pre-req for every db:migrate command
This commit is contained in:
DJ Mountney 2019-06-20 07:56:46 -07:00
parent 7a089438fa
commit f4e1553519
6 changed files with 41 additions and 6 deletions

View File

@ -1,5 +1,5 @@
--- ---
title: Added a min schema version check to gitlab:db:configure title: Added a min schema version check to gitlab:db:configure
merge_request: merge_request: 29882
author: author:
type: added type: added

View File

@ -14,6 +14,7 @@ module Gitlab
# Minimum schema version from which migrations are be supported # Minimum schema version from which migrations are be supported
# Migrations before this version may have been removed # Migrations before this version may have been removed
MIN_SCHEMA_VERSION = 20190506135400 MIN_SCHEMA_VERSION = 20190506135400
MIN_SCHEMA_GITLAB_VERSION = '11.11.0'
def self.config def self.config
ActiveRecord::Base.configurations[Rails.env] ActiveRecord::Base.configurations[Rails.env]

View File

@ -53,10 +53,6 @@ namespace :gitlab do
# Check if we have existing db tables # Check if we have existing db tables
# The schema_migrations table will still exist if drop_tables was called # The schema_migrations table will still exist if drop_tables was called
if ActiveRecord::Base.connection.tables.count > 1 if ActiveRecord::Base.connection.tables.count > 1
if ActiveRecord::Migrator.current_version < Gitlab::Database::MIN_SCHEMA_VERSION
raise "Your current database version is too old to be migrated. Please see https://docs.gitlab.com/ee/policy/maintenance.html#upgrade-recommendations"
end
Rake::Task['db:migrate'].invoke Rake::Task['db:migrate'].invoke
else else
# Add post-migrate paths to ensure we mark all migrations as up # Add post-migrate paths to ensure we mark all migrations as up

View File

@ -0,0 +1,11 @@
desc 'Configures the database by running migrate, or by loading the schema and seeding if needed'
task schema_version_check: :environment do
if ActiveRecord::Migrator.current_version < Gitlab::Database::MIN_SCHEMA_VERSION
raise "Your current database version is too old to be migrated. " \
"You should upgrade to GitLab #{Gitlab::Database::MIN_SCHEMA_GITLAB_VERSION} before moving to this version. " \
"Please see https://docs.gitlab.com/ee/policy/maintenance.html#upgrade-recommendations"
end
end
# Ensure the check is a pre-requisite when running db:migrate
Rake::Task["db:migrate"].enhance [:schema_version_check]

View File

@ -31,7 +31,7 @@ describe 'gitlab:db namespace rake task' do
it 'raises an when schema has been loaded, but version is too old to migrate' do it 'raises an when schema has been loaded, but version is too old to migrate' do
allow(ActiveRecord::Base.connection).to receive(:tables).and_return(%w[table1 table2]) allow(ActiveRecord::Base.connection).to receive(:tables).and_return(%w[table1 table2])
allow(ActiveRecord::Migrator).to receive(:current_version).and_return(25) allow(ActiveRecord::Migrator).to receive(:current_version).and_return(25)
expect { run_rake_task('gitlab:db:configure') }.to raise_error(RuntimeErrorm, /current database version is too old to be migrated/) expect { run_rake_task('gitlab:db:configure') }.to raise_error(RuntimeError, /current database version is too old to be migrated/)
end end
it 'invokes db:shema:load and db:seed_fu when schema is not loaded' do it 'invokes db:shema:load and db:seed_fu when schema is not loaded' do

View File

@ -0,0 +1,27 @@
require 'spec_helper'
require 'rake'
describe 'schema_version_check rake task' do
before :all do
Rake.application.rake_require 'active_record/railties/databases'
# empty task as env is already loaded
Rake::Task.define_task :environment
end
before do
# Stub out db tasks
allow(ActiveRecord::Tasks::DatabaseTasks).to receive(:migrate).and_return(true)
allow(ActiveRecord::Migrator).to receive(:current_version).and_return(Gitlab::Database::MIN_SCHEMA_VERSION)
end
it 'raises an error when schema version is too old to migrate' do
allow(ActiveRecord::Migrator).to receive(:current_version).and_return(25)
expect { run_rake_task('db:migrate') }.to raise_error(RuntimeError, /current database version is too old to be migrated/)
end
def run_rake_task(task_name)
Rake::Task[task_name].reenable
Rake.application.invoke_task task_name
end
end