Commit e448124f authored by DJ Mountney's avatar DJ Mountney

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.
parent f4232d84
...@@ -244,7 +244,7 @@ migration:path-pg: ...@@ -244,7 +244,7 @@ migration:path-pg:
extends: .dedicated-no-docs-and-no-qa-pull-cache-job extends: .dedicated-no-docs-and-no-qa-pull-cache-job
script: script:
- bundle exec rake db:migrate VERSION=20170523121229 - bundle exec rake db:migrate VERSION=20170523121229
- bundle exec rake db:migrate - bundle exec rake db:migrate SKIP_SCHEMA_VERSION_CHECK=true
dependencies: dependencies:
- setup-test-env - setup-test-env
......
--- ---
title: Added a min schema version check to gitlab:db:configure title: Added a min schema version check to db:migrate
merge_request: 29882 merge_request: 29882
author: author:
type: added type: added
desc 'Configures the database by running migrate, or by loading the schema and seeding if needed' desc 'Configures the database by running migrate, or by loading the schema and seeding if needed'
task schema_version_check: :environment do task schema_version_check: :environment do
next if ENV['SKIP_SCHEMA_VERSION_CHECK']
schema_version = ActiveRecord::Migrator.current_version schema_version = ActiveRecord::Migrator.current_version
# Ensure migrations are being run from a supported schema version # Ensure migrations are being run from a supported schema version
......
...@@ -16,7 +16,6 @@ describe 'gitlab:db namespace rake task' do ...@@ -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:migrate']).to receive(:invoke).and_return(true)
allow(Rake::Task['db:schema:load']).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(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 end
describe 'configure' do describe 'configure' do
...@@ -28,12 +27,6 @@ describe 'gitlab:db namespace rake task' do ...@@ -28,12 +27,6 @@ describe 'gitlab:db namespace rake task' do
expect { run_rake_task('gitlab:db:configure') }.not_to raise_error expect { run_rake_task('gitlab:db:configure') }.not_to raise_error
end 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 it 'invokes db:shema:load and db:seed_fu when schema is not loaded' do
allow(ActiveRecord::Base.connection).to receive(:tables).and_return([]) allow(ActiveRecord::Base.connection).to receive(:tables).and_return([])
expect(Rake::Task['db:schema:load']).to receive(:invoke) expect(Rake::Task['db:schema:load']).to receive(:invoke)
......
...@@ -2,8 +2,11 @@ require 'spec_helper' ...@@ -2,8 +2,11 @@ require 'spec_helper'
require 'rake' require 'rake'
describe 'schema_version_check rake task' do describe 'schema_version_check rake task' do
include StubENV
before :all do before :all do
Rake.application.rake_require 'active_record/railties/databases' Rake.application.rake_require 'active_record/railties/databases'
Rake.application.rake_require 'tasks/migrate/schema_check'
# empty task as env is already loaded # empty task as env is already loaded
Rake::Task.define_task :environment Rake::Task.define_task :environment
...@@ -13,6 +16,13 @@ describe 'schema_version_check rake task' do ...@@ -13,6 +16,13 @@ describe 'schema_version_check rake task' do
# Stub out db tasks # Stub out db tasks
allow(ActiveRecord::Tasks::DatabaseTasks).to receive(:migrate).and_return(true) allow(ActiveRecord::Tasks::DatabaseTasks).to receive(:migrate).and_return(true)
allow(ActiveRecord::Migrator).to receive(:current_version).and_return(Gitlab::Database::MIN_SCHEMA_VERSION) 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 end
it 'raises an error when schema version is too old to migrate' do it 'raises an error when schema version is too old to migrate' do
...@@ -20,6 +30,17 @@ describe 'schema_version_check rake task' 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/) expect { run_rake_task('db:migrate') }.to raise_error(RuntimeError, /current database version is too old to be migrated/)
end 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) def run_rake_task(task_name)
Rake::Task[task_name].reenable Rake::Task[task_name].reenable
Rake.application.invoke_task task_name Rake.application.invoke_task task_name
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment