Class: Gitlab::Database::Migrations::Runner

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

Constant Summary collapse

BASE_RESULT_DIR =
Rails.root.join('tmp', 'migration-testing').freeze
METADATA_FILENAME =
'metadata.json'
SCHEMA_VERSION =

Version of the output format produced by the runner

4
POST_MIGRATION_MATCHER =
%r{db/post_migrate/}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(direction:, database:, migrations:) ⇒ Runner

Returns a new instance of Runner.



109
110
111
112
113
114
115
116
# File 'lib/gitlab/database/migrations/runner.rb', line 109

def initialize(direction:, database:, migrations:)
  raise "Direction must be up or down" unless %i[up down].include?(direction)

  @direction = direction
  @migrations = migrations
  @result_dir = BASE_RESULT_DIR.join(database.to_s, direction.to_s)
  @database = database
end

Instance Attribute Details

#direction ⇒ Object (readonly)

Returns the value of attribute direction.



105
106
107
# File 'lib/gitlab/database/migrations/runner.rb', line 105

def direction
  @direction
end

#migrations ⇒ Object (readonly)

Returns the value of attribute migrations.



105
106
107
# File 'lib/gitlab/database/migrations/runner.rb', line 105

def migrations
  @migrations
end

#result_dir ⇒ Object (readonly)

Returns the value of attribute result_dir.



105
106
107
# File 'lib/gitlab/database/migrations/runner.rb', line 105

def result_dir
  @result_dir
end

Class Method Details

.batched_background_migrations(for_database:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gitlab/database/migrations/runner.rb', line 25

def batched_background_migrations(for_database:)
  runner = nil

  result_dir = background_migrations_dir(for_database)

  # Only one loop iteration since we pass `only:` here
  Gitlab::Database::EachDatabase.each_connection(only: for_database) do |connection|
    from_id = batched_migrations_last_id(for_database).read

    runner = Gitlab::Database::Migrations::TestBatchedBackgroundRunner
               .new(result_dir: result_dir, connection: connection, from_id: from_id)
  end

  runner
end

.batched_migrations_last_id(for_database) ⇒ Object

rubocop:enable Database/MultipleDatabases



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gitlab/database/migrations/runner.rb', line 63

def batched_migrations_last_id(for_database)
  runner = nil
  base_dir = background_migrations_dir(for_database)

  Gitlab::Database::EachDatabase.each_connection(only: for_database) do |connection|
    runner = Gitlab::Database::Migrations::BatchedMigrationLastId
               .new(connection, base_dir)
  end

  runner
end

.down(database:) ⇒ Object



19
20
21
22
23
# File 'lib/gitlab/database/migrations/runner.rb', line 19

def down(database:)
  within_context_for_database(database) do
    Runner.new(direction: :down, database: database, migrations: migrations_for_down(database))
  end
end

.migration_context ⇒ Object



41
42
43
44
45
46
# File 'lib/gitlab/database/migrations/runner.rb', line 41

def migration_context
  # We're mirroring rails internal migration code, which requires that
  # ActiveRecord::Base has connected to the current database. The correct database is chosen by
  # within_context_for_database
  ActiveRecord::Base.connection_pool.migration_context # rubocop:disable Database/MultipleDatabases
end

.up(database:) ⇒ Object



13
14
15
16
17
# File 'lib/gitlab/database/migrations/runner.rb', line 13

def up(database:)
  within_context_for_database(database) do
    Runner.new(direction: :up, database: database, migrations: migrations_for_up(database))
  end
end

.within_context_for_database(database) ⇒ Object

rubocop:disable Database/MultipleDatabases



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gitlab/database/migrations/runner.rb', line 49

def within_context_for_database(database)
  original_db_config = ActiveRecord::Base.connection_db_config
  # The config only works if passed a string
  db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: database.to_s)
  raise ArgumentError, "Cannot find a database configuration for #{database}" unless db_config

  ActiveRecord::Base.establish_connection(db_config) # rubocop:disable Database/EstablishConnection

  yield
ensure
  ActiveRecord::Base.establish_connection(original_db_config) # rubocop:disable Database/EstablishConnection
end

Instance Method Details

#run ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/gitlab/database/migrations/runner.rb', line 118

def run
  verbose_was = ActiveRecord::Migration.verbose
  ActiveRecord::Migration.verbose = true

  sorted_migrations = migrations.sort_by do |m|
    [m.filename.match?(POST_MIGRATION_MATCHER) ? 1 : 0, m.version]
  end

  sorted_migrations.reverse! if direction == :down

  instrumentation = Instrumentation.new(result_dir: result_dir)

  Gitlab::Database::Migrations::PreparedAsyncDmlOperationsTesting::AsyncOperationsRunner.install!

  within_context_for_database(@database) do
    sorted_migrations.each do |migration|
      instrumentation.observe(version: migration.version, name: migration.name, connection: ActiveRecord::Migration.connection) do
        ActiveRecord::Migrator.new(direction, migration_context.migrations, migration_context.schema_migration, migration_context., migration.version).run
        Gitlab::Database::Migrations::PreparedAsyncDmlOperationsTesting::AsyncOperationsRunner.execute!
      end
    end
  end
ensure
   = File.join(result_dir, METADATA_FILENAME)
  version = SCHEMA_VERSION

  File.write(, { database: @database.to_s, version: version }.to_json)

  # We clear the cache here to mirror the cache clearing that happens at the end of `db:migrate` tasks
  # This clearing makes subsequent rake tasks in the same execution pick up database schema changes caused by
  # the migrations that were just executed
  ApplicationRecord.clear_cache!
  ActiveRecord::Migration.verbose = verbose_was
end