Commit 7db8efe7 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'pb-dont-create-missing-migration-files' into 'master'

Only track schema versions with actual migrations

See merge request gitlab-org/gitlab!38645
parents ab354e73 b765adaf
...@@ -3,14 +3,19 @@ ...@@ -3,14 +3,19 @@
module Gitlab module Gitlab
module Database module Database
class SchemaVersionFiles class SchemaVersionFiles
SCHEMA_DIRECTORY = "db/schema_migrations" SCHEMA_DIRECTORY = 'db/schema_migrations'
MIGRATION_DIRECTORIES = %w[db/migrate db/post_migrate].freeze
MIGRATION_VERSION_GLOB = '20[0-9][0-9]*'
def self.touch_all(versions) def self.touch_all(versions_from_database)
filenames_with_path = find_version_filenames.map { |f| schema_dirpath.join(f) } versions_from_migration_files = find_versions_from_migration_files
FileUtils.rm(filenames_with_path)
versions.each do |version| version_filepaths = find_version_filenames.map { |f| schema_directory.join(f) }
version_filepath = schema_dirpath.join(version) FileUtils.rm(version_filepaths)
versions_to_create = versions_from_database & versions_from_migration_files
versions_to_create.each do |version|
version_filepath = schema_directory.join(version)
File.open(version_filepath, 'w') do |file| File.open(version_filepath, 'w') do |file|
file << Digest::SHA256.hexdigest(version) file << Digest::SHA256.hexdigest(version)
...@@ -25,17 +30,30 @@ module Gitlab ...@@ -25,17 +30,30 @@ module Gitlab
values = version_filenames.map { |vf| "('#{connection.quote_string(vf)}')" } values = version_filenames.map { |vf| "('#{connection.quote_string(vf)}')" }
connection.execute(<<~SQL) connection.execute(<<~SQL)
INSERT INTO schema_migrations (version) INSERT INTO schema_migrations (version)
VALUES #{values.join(",")} VALUES #{values.join(',')}
ON CONFLICT DO NOTHING ON CONFLICT DO NOTHING
SQL SQL
end end
def self.schema_dirpath def self.schema_directory
@schema_dirpath ||= Rails.root.join(SCHEMA_DIRECTORY) @schema_directory ||= Rails.root.join(SCHEMA_DIRECTORY)
end
def self.migration_directories
@migration_directories ||= MIGRATION_DIRECTORIES.map { |dir| Rails.root.join(dir) }
end end
def self.find_version_filenames def self.find_version_filenames
Dir.glob("20[0-9][0-9]*", base: schema_dirpath) Dir.glob(MIGRATION_VERSION_GLOB, base: schema_directory)
end
def self.find_versions_from_migration_files
migration_directories.each_with_object([]) do |directory, migration_versions|
directory_migrations = Dir.glob(MIGRATION_VERSION_GLOB, base: directory)
directory_versions = directory_migrations.map! { |m| m.split('_').first }
migration_versions.concat(directory_versions)
end
end end
def self.connection def self.connection
......
...@@ -4,30 +4,53 @@ require 'spec_helper' ...@@ -4,30 +4,53 @@ require 'spec_helper'
RSpec.describe Gitlab::Database::SchemaVersionFiles do RSpec.describe Gitlab::Database::SchemaVersionFiles do
describe '.touch_all' do describe '.touch_all' do
let(:versions) { %w[2020123 2020456 2020890] } let(:version1) { '20200123' }
let(:version2) { '20200410' }
it 'creates a file containing a checksum for each version given' do let(:version3) { '20200602' }
let(:version4) { '20200809' }
let(:relative_schema_directory) { 'db/schema_migrations' }
let(:relative_migrate_directory) { 'db/migrate' }
let(:relative_post_migrate_directory) { 'db/post_migrate' }
it 'creates a file containing a checksum for each version with a matching migration' do
Dir.mktmpdir do |tmpdir| Dir.mktmpdir do |tmpdir|
schema_dirpath = Pathname.new(tmpdir).join("test") schema_directory = Pathname.new(tmpdir).join(relative_schema_directory)
FileUtils.mkdir_p(schema_dirpath) migrate_directory = Pathname.new(tmpdir).join(relative_migrate_directory)
post_migrate_directory = Pathname.new(tmpdir).join(relative_post_migrate_directory)
FileUtils.mkdir_p(migrate_directory)
FileUtils.mkdir_p(post_migrate_directory)
FileUtils.mkdir_p(schema_directory)
migration1_filepath = migrate_directory.join("#{version1}_migration.rb")
FileUtils.touch(migration1_filepath)
old_version_filepath = schema_dirpath.join("2020001") migration2_filepath = post_migrate_directory.join("#{version2}_post_migration.rb")
FileUtils.touch(migration2_filepath)
old_version_filepath = schema_directory.join('20200101')
FileUtils.touch(old_version_filepath) FileUtils.touch(old_version_filepath)
expect(File.exist?(old_version_filepath)).to be(true) expect(File.exist?(old_version_filepath)).to be(true)
allow(described_class).to receive(:schema_dirpath).and_return(schema_dirpath) allow(described_class).to receive(:schema_directory).and_return(schema_directory)
allow(described_class).to receive(:migration_directories).and_return([migrate_directory, post_migrate_directory])
described_class.touch_all(versions) described_class.touch_all([version1, version2, version3, version4])
expect(File.exist?(old_version_filepath)).to be(false) expect(File.exist?(old_version_filepath)).to be(false)
versions.each do |version| [version1, version2].each do |version|
version_filepath = schema_dirpath.join(version) version_filepath = schema_directory.join(version)
expect(File.exist?(version_filepath)).to be(true) expect(File.exist?(version_filepath)).to be(true)
hashed_value = Digest::SHA256.hexdigest(version) hashed_value = Digest::SHA256.hexdigest(version)
expect(File.read(version_filepath)).to eq(hashed_value) expect(File.read(version_filepath)).to eq(hashed_value)
end end
[version3, version4].each do |version|
version_filepath = schema_directory.join(version)
expect(File.exist?(version_filepath)).to be(false)
end
end end
end 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