Commit 7a089438 authored by DJ Mountney's avatar DJ Mountney

Check supported version when migrating

Set the mininum supported migration version to be the schema version as
of 11.11.0, and errors you if that is not detected during
gitlab:db:configure
parent 4d1e2ec4
---
title: Added a min schema version check to gitlab:db:configure
merge_request:
author:
type: added
......@@ -324,7 +324,7 @@ sudo -u git -H bundle install --deployment --without development test mysql aws
sudo -u git -H bundle clean
# Run database migrations
sudo -u git -H bundle exec rake db:migrate RAILS_ENV=production
sudo -u git -H bundle exec rake gitlab:db:configure RAILS_ENV=production
# Compile GetText PO files
......
......@@ -11,6 +11,10 @@ module Gitlab
# https://dev.mysql.com/doc/refman/5.7/en/datetime.html
MAX_TIMESTAMP_VALUE = Time.at((1 << 31) - 1).freeze
# Minimum schema version from which migrations are be supported
# Migrations before this version may have been removed
MIN_SCHEMA_VERSION = 20190506135400
def self.config
ActiveRecord::Base.configurations[Rails.env]
end
......
......@@ -53,6 +53,10 @@ namespace :gitlab do
# Check if we have existing db tables
# The schema_migrations table will still exist if drop_tables was called
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
else
# Add post-migrate paths to ensure we mark all migrations as up
......
......@@ -16,6 +16,7 @@ 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
......@@ -27,6 +28,12 @@ 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(RuntimeErrorm, /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)
......
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