2019-06-20 13:02:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-06-20 10:56:46 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
require 'rake'
|
|
|
|
|
2021-06-08 11:10:00 -04:00
|
|
|
RSpec.describe 'schema_version_check rake task', :silence_stdout do
|
2019-06-20 11:50:46 -04:00
|
|
|
include StubENV
|
|
|
|
|
2019-06-20 10:56:46 -04:00
|
|
|
before :all do
|
|
|
|
Rake.application.rake_require 'active_record/railties/databases'
|
2019-06-20 11:50:46 -04:00
|
|
|
Rake.application.rake_require 'tasks/migrate/schema_check'
|
2019-06-20 10:56:46 -04:00
|
|
|
|
|
|
|
# empty task as env is already loaded
|
|
|
|
Rake::Task.define_task :environment
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(ActiveRecord::Migrator).to receive(:current_version).and_return(Gitlab::Database::MIN_SCHEMA_VERSION)
|
2019-06-20 11:50:46 -04:00
|
|
|
|
|
|
|
# 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
|
2019-06-30 23:48:14 -04:00
|
|
|
expect { run_rake_task('schema_version_check') }.not_to raise_error
|
2019-06-20 10:56:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when schema version is too old to migrate' do
|
|
|
|
allow(ActiveRecord::Migrator).to receive(:current_version).and_return(25)
|
2019-06-30 23:48:14 -04:00
|
|
|
expect { run_rake_task('schema_version_check') }.to raise_error(RuntimeError, /current database version is too old to be migrated/)
|
2019-06-20 10:56:46 -04:00
|
|
|
end
|
|
|
|
|
2019-06-20 11:50:46 -04:00
|
|
|
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)
|
2019-06-30 23:48:14 -04:00
|
|
|
expect { run_rake_task('schema_version_check') }.not_to raise_error
|
2019-06-20 11:50:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows migrations on fresh databases' do
|
|
|
|
allow(ActiveRecord::Migrator).to receive(:current_version).and_return(0)
|
2019-06-30 23:48:14 -04:00
|
|
|
expect { run_rake_task('schema_version_check') }.not_to raise_error
|
2019-06-20 11:50:46 -04:00
|
|
|
end
|
|
|
|
|
2019-06-20 10:56:46 -04:00
|
|
|
def run_rake_task(task_name)
|
|
|
|
Rake::Task[task_name].reenable
|
|
|
|
Rake.application.invoke_task task_name
|
|
|
|
end
|
|
|
|
end
|