Class: Gitlab::Database::Migrations::Runner
- Inherits:
-
Object
- Object
- Gitlab::Database::Migrations::Runner
- 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
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#migrations ⇒ Object
readonly
Returns the value of attribute migrations.
-
#result_dir ⇒ Object
readonly
Returns the value of attribute result_dir.
Class Method Summary collapse
- .background_migrations ⇒ Object
- .batched_background_migrations(for_database:, legacy_mode: false) ⇒ Object
-
.batched_migrations_last_id(for_database) ⇒ Object
rubocop:enable Database/MultipleDatabases.
- .down(database:, legacy_mode: false) ⇒ Object
- .migration_context ⇒ Object
- .up(database:, legacy_mode: false) ⇒ Object
-
.within_context_for_database(database) ⇒ Object
rubocop:disable Database/MultipleDatabases.
Instance Method Summary collapse
-
#initialize(direction:, database:, migrations:, legacy_mode: false) ⇒ Runner
constructor
A new instance of Runner.
- #run ⇒ Object
Constructor Details
#initialize(direction:, database:, migrations:, legacy_mode: false) ⇒ Runner
Returns a new instance of Runner.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/gitlab/database/migrations/runner.rb', line 115 def initialize(direction:, database:, migrations:, legacy_mode: false) raise "Direction must be up or down" unless %i[up down].include?(direction) @direction = direction @migrations = migrations @result_dir = if legacy_mode BASE_RESULT_DIR.join(direction.to_s) else BASE_RESULT_DIR.join(database.to_s, direction.to_s) end @database = database @legacy_mode = legacy_mode end |
Instance Attribute Details
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
111 112 113 |
# File 'lib/gitlab/database/migrations/runner.rb', line 111 def direction @direction end |
#migrations ⇒ Object (readonly)
Returns the value of attribute migrations.
111 112 113 |
# File 'lib/gitlab/database/migrations/runner.rb', line 111 def migrations @migrations end |
#result_dir ⇒ Object (readonly)
Returns the value of attribute result_dir.
111 112 113 |
# File 'lib/gitlab/database/migrations/runner.rb', line 111 def result_dir @result_dir end |
Class Method Details
.background_migrations ⇒ Object
25 26 27 |
# File 'lib/gitlab/database/migrations/runner.rb', line 25 def background_migrations TestBackgroundRunner.new(result_dir: BASE_RESULT_DIR.join('background_migrations')) end |
.batched_background_migrations(for_database:, legacy_mode: false) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/gitlab/database/migrations/runner.rb', line 29 def batched_background_migrations(for_database:, legacy_mode: false) runner = nil result_dir = background_migrations_dir(for_database, legacy_mode) # 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
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/gitlab/database/migrations/runner.rb', line 67 def batched_migrations_last_id(for_database) runner = nil base_dir = background_migrations_dir(for_database, false) Gitlab::Database::EachDatabase.each_connection(only: for_database) do |connection| runner = Gitlab::Database::Migrations::BatchedMigrationLastId .new(connection, base_dir) end runner end |
.down(database:, legacy_mode: false) ⇒ Object
19 20 21 22 23 |
# File 'lib/gitlab/database/migrations/runner.rb', line 19 def down(database:, legacy_mode: false) within_context_for_database(database) do Runner.new(direction: :down, database: database, migrations: migrations_for_down(database), legacy_mode: legacy_mode) end end |
.migration_context ⇒ Object
45 46 47 48 49 50 |
# File 'lib/gitlab/database/migrations/runner.rb', line 45 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.migration_context # rubocop:disable Database/MultipleDatabases end |
.up(database:, legacy_mode: false) ⇒ Object
13 14 15 16 17 |
# File 'lib/gitlab/database/migrations/runner.rb', line 13 def up(database:, legacy_mode: false) within_context_for_database(database) do Runner.new(direction: :up, database: database, migrations: migrations_for_up(database), legacy_mode: legacy_mode) end end |
.within_context_for_database(database) ⇒ Object
rubocop:disable Database/MultipleDatabases
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/gitlab/database/migrations/runner.rb', line 53 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
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/gitlab/database/migrations/runner.rb', line 130 def run FileUtils.mkdir_p(result_dir) 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) 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.version).run end end end ensure = File.join(result_dir, METADATA_FILENAME) version = if @legacy_mode 3 else SCHEMA_VERSION end 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 |