Class: Gitlab::Database::SchemaVersionFiles

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/database/schema_version_files.rb

Constant Summary collapse

SCHEMA_DIRECTORY =
'db/schema_migrations'
MIGRATION_DIRECTORIES =
%w[db/migrate db/post_migrate].freeze
MIGRATION_VERSION_GLOB =
'20[0-9][0-9]*'

Class Method Summary collapse

Class Method Details

.connectionObject


59
60
61
# File 'lib/gitlab/database/schema_version_files.rb', line 59

def self.connection
  ActiveRecord::Base.connection
end

.find_version_filenamesObject


46
47
48
# File 'lib/gitlab/database/schema_version_files.rb', line 46

def self.find_version_filenames
  Dir.glob(MIGRATION_VERSION_GLOB, base: schema_directory)
end

.find_versions_from_migration_filesObject


50
51
52
53
54
55
56
57
# File 'lib/gitlab/database/schema_version_files.rb', line 50

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

.load_allObject


26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gitlab/database/schema_version_files.rb', line 26

def self.load_all
  version_filenames = find_version_filenames
  return if version_filenames.empty?

  values = version_filenames.map { |vf| "('#{connection.quote_string(vf)}')" }
  connection.execute(<<~SQL)
    INSERT INTO schema_migrations (version)
    VALUES #{values.join(',')}
    ON CONFLICT DO NOTHING
  SQL
end

.migration_directoriesObject


42
43
44
# File 'lib/gitlab/database/schema_version_files.rb', line 42

def self.migration_directories
  @migration_directories ||= MIGRATION_DIRECTORIES.map { |dir| Rails.root.join(dir) }
end

.schema_directoryObject


38
39
40
# File 'lib/gitlab/database/schema_version_files.rb', line 38

def self.schema_directory
  @schema_directory ||= Rails.root.join(SCHEMA_DIRECTORY)
end

.touch_all(versions_from_database) ⇒ Object


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gitlab/database/schema_version_files.rb', line 10

def self.touch_all(versions_from_database)
  versions_from_migration_files = find_versions_from_migration_files

  version_filepaths = find_version_filenames.map { |f| schema_directory.join(f) }
  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 << Digest::SHA256.hexdigest(version)
    end
  end
end