Add an flag for skipping the schema version check

If you chose to use the rollback migration feature on your current
version for example, you should still have a way to migrate, being
that you are still on a supported migration path.
This commit is contained in:
DJ Mountney 2019-06-20 08:50:46 -07:00
parent f4232d848e
commit e448124fab
5 changed files with 25 additions and 9 deletions

View File

@ -244,7 +244,7 @@ migration:path-pg:
extends: .dedicated-no-docs-and-no-qa-pull-cache-job
script:
- bundle exec rake db:migrate VERSION=20170523121229
- bundle exec rake db:migrate
- bundle exec rake db:migrate SKIP_SCHEMA_VERSION_CHECK=true
dependencies:
- setup-test-env

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 db:migrate
merge_request: 29882
author:
type: added

View File

@ -1,5 +1,7 @@
desc 'Configures the database by running migrate, or by loading the schema and seeding if needed'
task schema_version_check: :environment do
next if ENV['SKIP_SCHEMA_VERSION_CHECK']
schema_version = ActiveRecord::Migrator.current_version
# Ensure migrations are being run from a supported schema version

View File

@ -16,7 +16,6 @@ describe 'gitlab:db namespace rake task' do
allow(Rake::Task['db:migrate']).to receive(:invoke).and_return(true)
allow(Rake::Task['db:schema:load']).to receive(:invoke).and_return(true)
allow(Rake::Task['db:seed_fu']).to receive(:invoke).and_return(true)
allow(ActiveRecord::Migrator).to receive(:current_version).and_return(Gitlab::Database::MIN_SCHEMA_VERSION)
end
describe 'configure' do
@ -28,12 +27,6 @@ describe 'gitlab:db namespace rake task' do
expect { run_rake_task('gitlab:db:configure') }.not_to raise_error
end
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::Migrator).to receive(:current_version).and_return(25)
expect { run_rake_task('gitlab:db:configure') }.to raise_error(RuntimeError, /current database version is too old to be migrated/)
end
it 'invokes db:shema:load and db:seed_fu when schema is not loaded' do
allow(ActiveRecord::Base.connection).to receive(:tables).and_return([])
expect(Rake::Task['db:schema:load']).to receive(:invoke)

View File

@ -2,8 +2,11 @@ require 'spec_helper'
require 'rake'
describe 'schema_version_check rake task' do
include StubENV
before :all do
Rake.application.rake_require 'active_record/railties/databases'
Rake.application.rake_require 'tasks/migrate/schema_check'
# empty task as env is already loaded
Rake::Task.define_task :environment
@ -13,6 +16,13 @@ describe 'schema_version_check rake task' 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)
# Ensure our check can re-run each time
Rake::Task[:schema_version_check].reenable
end
it 'allows migrations on databases meeting the min schema version requirement' do
expect { run_rake_task('db:migrate') }.not_to raise_error
end
it 'raises an error when schema version is too old to migrate' do
@ -20,6 +30,17 @@ describe 'schema_version_check rake task' do
expect { run_rake_task('db:migrate') }.to raise_error(RuntimeError, /current database version is too old to be migrated/)
end
it 'skips running validation when passed the skip env variable' do
stub_env('SKIP_SCHEMA_VERSION_CHECK', 'true')
allow(ActiveRecord::Migrator).to receive(:current_version).and_return(25)
expect { run_rake_task('db:migrate') }.not_to raise_error
end
it 'allows migrations on fresh databases' do
allow(ActiveRecord::Migrator).to receive(:current_version).and_return(0)
expect { run_rake_task('db:migrate') }.not_to raise_error
end
def run_rake_task(task_name)
Rake::Task[task_name].reenable
Rake.application.invoke_task task_name