Commit efeaa328 authored by Kamil Trzciński's avatar Kamil Trzciński Committed by Thong Kuah

Add `Migration[2.0]` that enforces `restrict_gitlab_migration`

This implements a new migration version and validates if correct
migration version is used depending on a cutoff points.

This allows to enforce a usage of a new `restrict_gitlab_migration`.

Changelog: added
parent 81f5f80f
...@@ -33,15 +33,22 @@ module Gitlab ...@@ -33,15 +33,22 @@ module Gitlab
# We use major version bumps to indicate significant changes and minor version bumps # We use major version bumps to indicate significant changes and minor version bumps
# to indicate backwards-compatible or otherwise minor changes (e.g. a Rails version bump). # to indicate backwards-compatible or otherwise minor changes (e.g. a Rails version bump).
# However, this hasn't been strictly formalized yet. # However, this hasn't been strictly formalized yet.
MIGRATION_CLASSES = {
1.0 => Class.new(ActiveRecord::Migration[6.1]) do class V1_0 < ActiveRecord::Migration[6.1] # rubocop:disable Naming/ClassAndModuleCamelCase
include LockRetriesConcern include LockRetriesConcern
include Gitlab::Database::MigrationHelpers::V2 include Gitlab::Database::MigrationHelpers::V2
end end
}.freeze
class V2_0 < V1_0 # rubocop:disable Naming/ClassAndModuleCamelCase
include Gitlab::Database::MigrationHelpers::RestrictGitlabSchema
end
def self.[](version) def self.[](version)
MIGRATION_CLASSES[version] || raise(ArgumentError, "Unknown migration version: #{version}") version = version.to_s
name = "V#{version.tr('.', '_')}"
raise ArgumentError, "Unknown migration version: #{version}" unless const_defined?(name, false)
const_get(name, false)
end end
# The current version to be used in new migrations # The current version to be used in new migrations
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Migrations Validation' do
using RSpec::Parameterized::TableSyntax
# The range describes the timestamps that given migration helper can be used
let(:all_migration_classes) do
{
2022_01_26_21_06_58.. => Gitlab::Database::Migration[2.0],
2021_09_01_15_33_24.. => Gitlab::Database::Migration[1.0],
2021_05_31_05_39_16..2021_09_01_15_33_24 => ActiveRecord::Migration[6.1],
..2021_05_31_05_39_16 => ActiveRecord::Migration[6.0]
}
end
where(:migration) do
Gitlab::Database.database_base_models.flat_map do |_, model|
model.connection.migration_context.migrations
end.uniq
end
with_them do
let(:migration_instance) { migration.send(:migration) }
let(:allowed_migration_classes) { all_migration_classes.select { |r, _| r.include?(migration.version) }.values }
it 'uses one of the allowed migration classes' do
expect(allowed_migration_classes).to include(be > migration_instance.class)
end
end
end
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