Commit 06349b8f authored by Michael Kozono's avatar Michael Kozono

Merge branch...

Merge branch '348357-deprecate-custom-rake-geo-db-rake-tasks-in-favor-of-built-in-tasks-rake-db-geo' into 'master'

Deprecate custom geo:db:* rake tasks in favor of built-in tasks

See merge request gitlab-org/gitlab!77269
parents 4c24d8aa 40a48618
# frozen_string_literal: true
module Gitlab
module Geo
module DatabaseTasks
extend self
DATABASE_CONFIG = 'config/database.yml'
GEO_DATABASE_CONFIG = 'config/database_geo.yml'
GEO_DB_DIR = 'ee/db/geo'
def method_missing(method_name, *args, &block)
with_geo_db do
ActiveRecord::Tasks::DatabaseTasks.public_send(method_name, *args, &block) # rubocop:disable GitlabSecurity/PublicSend
end
end
def respond_to_missing?(method_name, include_private = false)
ActiveRecord::Tasks::DatabaseTasks.respond_to?(method_name) || super
end
def rollback
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
with_geo_db do
migration_context.rollback(step)
end
end
def version
with_geo_db do
ActiveRecord::Migrator.current_version
end
end
def dump_schema_after_migration?
with_geo_db do
!!ActiveRecord::Base.dump_schema_after_migration
end
end
def pending_migrations
with_geo_db do
migration_context.open.pending_migrations
end
end
def migration_context
ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths, ActiveRecord::SchemaMigration)
end
def abort_if_no_geo_config!
@geo_config_exists ||= File.exist?(Rails.root.join(GEO_DATABASE_CONFIG)) # rubocop:disable Gitlab/ModuleWithInstanceVariables
unless @geo_config_exists # rubocop:disable Gitlab/ModuleWithInstanceVariables
abort("Failed to open #{GEO_DATABASE_CONFIG}. Consult the documentation on how to set up GitLab Geo.")
end
end
module Schema
extend self
def dump
require 'active_record/schema_dumper'
Gitlab::Geo::DatabaseTasks.with_geo_db do
ActiveRecord::Tasks::DatabaseTasks.dump_schema(Gitlab::Geo::DatabaseTasks.db_config)
end
end
end
module Migrate
extend self
def up
version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
raise 'VERSION is required' unless version
Gitlab::Geo::DatabaseTasks.with_geo_db do
Gitlab::Geo::DatabaseTasks.migration_context.run(:up, version)
end
end
def down
version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
raise 'VERSION is required - To go down one migration, run db:rollback' unless version
Gitlab::Geo::DatabaseTasks.with_geo_db do
Gitlab::Geo::DatabaseTasks.migration_context.run(:down, version)
end
end
def status
Gitlab::Geo::DatabaseTasks.with_geo_db do
unless ActiveRecord::SchemaMigration.table_exists?
abort 'Schema migrations table does not exist yet.'
end
db_list = ActiveRecord::SchemaMigration.normalized_versions
file_list =
ActiveRecord::Migrator.migrations_paths.flat_map do |path|
# match "20091231235959_some_name.rb" and "001_some_name.rb" pattern
Dir.foreach(path).grep(/^(\d{3,})_(.+)\.rb$/) do
version = ActiveRecord::SchemaMigration.normalize_migration_number(Regexp.last_match(1))
status = db_list.delete(version) ? 'up' : 'down'
[status, version, Regexp.last_match(2).humanize]
end
end
db_list.map! do |version|
['up', version, '********** NO FILE **********']
end
# output
puts "\ndatabase: #{ActiveRecord::Base.connection_db_config.database}\n\n"
puts "#{'Status'.center(8)} #{'Migration ID'.ljust(14)} Migration Name"
puts "-" * 50
(db_list + file_list).sort_by { |_, version, _| version }.each do |status, version, name|
puts "#{status.center(8)} #{version.ljust(14)} #{name}"
end
puts
end
end
end
module Test
extend self
def load
Gitlab::Geo::DatabaseTasks.with_geo_db do
should_reconnect = ActiveRecord::Base.connection_pool.active_connection?
ActiveRecord::Schema.verbose = false
ActiveRecord::Tasks::DatabaseTasks.load_schema(ActiveRecord::Base.configurations.configs_for(env_name: 'test').first, :sql, ENV['SCHEMA'])
ensure
if should_reconnect
ActiveRecord::Base.establish_connection(Gitlab::Geo::DatabaseTasks.db_config) # rubocop: disable Database/EstablishConnection
end
end
end
def purge
Gitlab::Geo::DatabaseTasks.with_geo_db do
ActiveRecord::Tasks::DatabaseTasks.purge(ActiveRecord::Base.configurations.configs_for(env_name: 'test').first)
end
end
end
def geo_settings
{
database_config: YAML.load_file(GEO_DATABASE_CONFIG),
db_dir: GEO_DB_DIR,
migrations_paths: geo_migrations_paths,
schema_migrations_path: geo_schema_migrations_path,
seed_loader: SeedLoader.new
}
end
def geo_migrations_paths
migrations_paths = [geo_migrate_path]
migrations_paths << geo_post_migration_path unless ENV['SKIP_POST_DEPLOYMENT_MIGRATIONS']
migrations_paths
end
def geo_migrate_path
Rails.root.join(GEO_DB_DIR, 'migrate')
end
def geo_post_migration_path
Rails.root.join(GEO_DB_DIR, 'post_migrate')
end
def geo_schema_migrations_path
Rails.root.join(GEO_DB_DIR, 'schema_migrations').to_s
end
def with_geo_db
abort_if_no_geo_config!
original_settings = {
database_config: ActiveRecord::Tasks::DatabaseTasks.database_configuration&.dup || YAML.load_file(DATABASE_CONFIG),
db_dir: ActiveRecord::Tasks::DatabaseTasks.db_dir,
migrations_paths: ActiveRecord::Tasks::DatabaseTasks.migrations_paths,
seed_loader: ActiveRecord::Tasks::DatabaseTasks.seed_loader,
schema_migrations_path: Gitlab::Database::SchemaMigrations::Context.default_schema_migrations_path
}
set_db_env(geo_settings)
yield
ensure
set_db_env(original_settings)
end
def db_config
ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).first
end
def set_db_env(settings)
ActiveRecord::Tasks::DatabaseTasks.database_configuration = settings[:database_config]
ActiveRecord::Tasks::DatabaseTasks.db_dir = settings[:db_dir]
ActiveRecord::Tasks::DatabaseTasks.migrations_paths = settings[:migrations_paths]
ActiveRecord::Tasks::DatabaseTasks.seed_loader = settings[:seed_loader]
ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration || {}
ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
Gitlab::Database::SchemaMigrations::Context.default_schema_migrations_path = settings[:schema_migrations_path]
ActiveRecord::Base.establish_connection(db_config) # rubocop: disable Database/EstablishConnection
end
class SeedLoader
def load_seed
load(File.join(GEO_DB_DIR, 'seeds.rb'))
end
end
end
end
end
...@@ -2,147 +2,193 @@ ...@@ -2,147 +2,193 @@
task spec: ['db:test:prepare:geo'] task spec: ['db:test:prepare:geo']
databases = ActiveRecord::Tasks::DatabaseTasks.setup_initial_database_yaml
# These db:* tasks are taken from Rails 7.0 since Rails 6.1 does not have
# built-in support for multiple databases for them. To be removed when we
# migrate to Rails 7.0:
# https://gitlab.com/gitlab-org/gitlab/-/issues/352190
#
# https://github.com/rails/rails/blob/main/activerecord/lib/active_record/railties/databases.rake
db_namespace = namespace :db do
namespace :reset do
task all: ["db:drop", "db:setup"]
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
desc "Drops and recreates the #{name} database from its schema for the current environment and loads the seeds."
task name => ["db:drop:#{name}", "db:setup:#{name}"]
end
end
namespace :setup do
task all: ["db:create", :environment, "db:schema:load", :seed]
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
desc "Creates the #{name} database, loads the schema, and initializes with the seed data (use db:reset:#{name} to also drop the database first)"
task name => ["db:create:#{name}", :environment, "db:schema:load:#{name}", "db:seed"]
end
end
namespace :version do
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
desc "Retrieves the current #{name} database schema version number"
task name => :load_config do
original_db_config = ActiveRecord::Base.connection_db_config
db_config = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env, name: name)
ActiveRecord::Base.establish_connection(db_config) # rubocop: disable Database/EstablishConnection
puts "Current version: #{ActiveRecord::Base.connection.migration_context.current_version}"
ensure
ActiveRecord::Base.establish_connection(original_db_config) if original_db_config # rubocop: disable Database/EstablishConnection
end
end
end
namespace :seed do
seed_loader = Class.new do
def self.load_seed
load('ee/db/geo/seeds.rb')
end
end
desc "Loads the seed data from ee/db/geo/seeds.rb"
task geo: :load_config do
db_namespace["abort_if_pending_migrations"].invoke
ActiveRecord::Tasks::DatabaseTasks.seed_loader = seed_loader
ActiveRecord::Tasks::DatabaseTasks.load_seed
end
end
end
namespace :geo do namespace :geo do
GEO_LICENSE_ERROR_TEXT = 'GitLab Geo is not supported with this license. Please contact the sales team: https://about.gitlab.com/sales.' GEO_LICENSE_ERROR_TEXT = 'GitLab Geo is not supported with this license. Please contact the sales team: https://about.gitlab.com/sales.'
def log_deprecated_message(deprecated_task, task_to_invoke)
puts "DEPRECATION WARNING: Using `bin/rails #{deprecated_task}` is deprecated and will be removed in Gitlab 15.0. Please run `bin/rails #{task_to_invoke}` instead.".color(:red)
end
namespace :db do |ns| namespace :db do |ns|
desc 'GitLab | Geo | DB | Drops the Geo tracking database from config/database_geo.yml for the current RAILS_ENV.' desc 'GitLab | Geo | DB | Drops the Geo tracking database from config/database_geo.yml for the current RAILS_ENV.'
task drop: [:environment] do task drop: [:environment] do
Gitlab::Geo::DatabaseTasks.drop_current Rake::Task['db:drop:geo'].invoke
log_deprecated_message('geo:db:drop', 'db:drop:geo')
end end
desc 'GitLab | Geo | DB | Creates the Geo tracking database from config/database_geo.yml for the current RAILS_ENV.' desc 'GitLab | Geo | DB | Creates the Geo tracking database from config/database_geo.yml for the current RAILS_ENV.'
task create: [:environment] do task create: [:environment] do
Gitlab::Geo::DatabaseTasks.create_current Rake::Task['db:create:geo'].invoke
log_deprecated_message('geo:db:create', 'db:create:geo')
end end
desc 'GitLab | Geo | DB | Create the Geo tracking database, load the schema, and initialize with the seed data.' desc 'GitLab | Geo | DB | Create the Geo tracking database, load the schema, and initialize with the seed data.'
task setup: ['geo:db:schema:load', 'geo:db:seed'] task setup: [:environment] do
Rake::Task['db:setup:geo'].invoke
log_deprecated_message('geo:db:setup', 'db:setup:geo')
end
desc 'GitLab | Geo | DB | Migrate the Geo tracking database (options: VERSION=x, VERBOSE=false, SCOPE=blog).' desc 'GitLab | Geo | DB | Migrate the Geo tracking database (options: VERSION=x, VERBOSE=false, SCOPE=blog).'
task migrate: [:environment] do task migrate: [:environment] do
Gitlab::Geo::DatabaseTasks.migrate Rake::Task['db:migrate:geo'].invoke
ns['_dump'].invoke log_deprecated_message('geo:db:migrate', 'db:migrate:geo')
end end
desc 'GitLab | Geo | DB | Rolls the schema back to the previous version (specify steps w/ STEP=n).' desc 'GitLab | Geo | DB | Rolls the schema back to the previous version (specify steps w/ STEP=n).'
task rollback: [:environment] do task rollback: [:environment] do
Gitlab::Geo::DatabaseTasks.rollback Rake::Task['db:rollback:geo'].invoke
ns['_dump'].invoke log_deprecated_message('geo:db:rollback', 'db:rollback:geo')
end end
desc 'GitLab | Geo | DB | Retrieves the current schema version number.' desc 'GitLab | Geo | DB | Retrieves the current schema version number.'
task version: [:environment] do task version: [:environment] do
puts "Current version: #{Gitlab::Geo::DatabaseTasks.version}" Rake::Task['db:version:geo'].invoke
log_deprecated_message('geo:db:version', 'db:version:geo')
end end
desc 'GitLab | Geo | DB | Drops and recreates the database from ee/db/geo/structure.sql for the current environment and loads the seeds.' desc 'GitLab | Geo | DB | Drops and recreates the database from ee/db/geo/structure.sql for the current environment and loads the seeds.'
task reset: [:environment] do task reset: [:environment] do
ns['drop'].invoke Rake::Task['db:reset:geo'].invoke
ns['create'].invoke
ns['setup'].invoke log_deprecated_message('geo:db:reset', 'db:reset:geo')
end end
desc 'GitLab | Geo | DB | Load the seed data from ee/db/geo/seeds.rb' desc 'GitLab | Geo | DB | Load the seed data from ee/db/geo/seeds.rb'
task seed: [:environment] do task seed: [:environment] do
ns['abort_if_pending_migrations'].invoke Rake::Task['db:seed:geo'].invoke
Gitlab::Geo::DatabaseTasks.load_seed
end
# IMPORTANT: This task won't dump the schema if ActiveRecord::Base.dump_schema_after_migration is set to false
task _dump: [:environment] do
if Gitlab::Geo::DatabaseTasks.dump_schema_after_migration?
ns["schema:dump"].invoke
end
# Allow this task to be called as many times as required. An example is the
# migrate:redo task, which calls other two internally that depend on this one.
ns['_dump'].reenable
end
# desc "Raises an error if there are pending migrations"
task abort_if_pending_migrations: [:environment] do
pending_migrations = Gitlab::Geo::DatabaseTasks.pending_migrations
if pending_migrations.any? log_deprecated_message('geo:db:seed', 'db:seed:geo')
puts "You have #{pending_migrations.size} pending #{pending_migrations.size > 1 ? 'migrations:' : 'migration:'}"
pending_migrations.each do |pending_migration|
puts ' %4d %s' % [pending_migration.version, pending_migration.name]
end
abort %{Run `rake geo:db:migrate` to update your database then try again.}
end
end end
namespace :schema do namespace :schema do
desc 'GitLab | Geo | DB | Schema | Load a structure.sql file into the database' desc 'GitLab | Geo | DB | Schema | Load a structure.sql file into the database'
task load: [:environment] do task load: [:environment] do
Gitlab::Geo::DatabaseTasks.load_schema_current(ActiveRecord::Base.schema_format, ENV['SCHEMA']) Rake::Task['db:schema:load:geo'].invoke
log_deprecated_message('geo:db:schema:load', 'db:schema:load:geo')
end end
desc 'GitLab | Geo | DB | Schema | Create a ee/db/geo/structure.sql file that is portable against any DB supported by AR' desc 'GitLab | Geo | DB | Schema | Create a ee/db/geo/structure.sql file that is portable against any DB supported by AR'
task dump: [:environment] do task dump: [:environment] do
Gitlab::Geo::DatabaseTasks::Schema.dump Rake::Task['db:schema:dump:geo'].invoke
ns['schema:dump'].reenable
end
end
# Inform Rake that custom tasks should be run every time rake db:schema:dump is run log_deprecated_message('geo:db:schema:dump', 'db:schema:dump:geo')
Rake::Task['geo:db:schema:dump'].enhance do
Gitlab::Geo::DatabaseTasks.with_geo_db do
Rake::Task['gitlab:db:clean_structure_sql'].invoke
end end
end end
namespace :migrate do namespace :migrate do
desc 'GitLab | Geo | DB | Migrate | Runs the "up" for a given migration VERSION.' desc 'GitLab | Geo | DB | Migrate | Runs the "up" for a given migration VERSION.'
task up: [:environment] do task up: [:environment] do
Gitlab::Geo::DatabaseTasks::Migrate.up Rake::Task['db:migrate:up:geo'].invoke
ns['_dump'].invoke log_deprecated_message('geo:db:migrate:up', 'db:migrate:up:geo')
end end
desc 'GitLab | Geo | DB | Migrate | Runs the "down" for a given migration VERSION.' desc 'GitLab | Geo | DB | Migrate | Runs the "down" for a given migration VERSION.'
task down: [:environment] do task down: [:environment] do
Gitlab::Geo::DatabaseTasks::Migrate.down Rake::Task['db:migrate:down:geo'].invoke
ns['_dump'].invoke log_deprecated_message('geo:db:migrate:down', 'db:migrate:down:geo')
end end
desc 'GitLab | Geo | DB | Migrate | Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x).' desc 'GitLab | Geo | DB | Migrate | Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x).'
task redo: [:environment] do task redo: [:environment] do
if ENV['VERSION'] Rake::Task['db:migrate:redo:geo'].invoke
ns['migrate:down'].invoke
ns['migrate:up'].invoke log_deprecated_message('geo:db:migrate:redo', 'db:migrate:redo:geo')
else
ns['rollback'].invoke
ns['migrate'].invoke
end
end end
desc 'GitLab | Geo | DB | Migrate | Display status of migrations' desc 'GitLab | Geo | DB | Migrate | Display status of migrations'
task status: [:environment] do task status: [:environment] do
Gitlab::Geo::DatabaseTasks::Migrate.status Rake::Task['db:migrate:status:geo'].invoke
log_deprecated_message('geo:db:migrate:status', 'db:migrate:status:geo')
end end
end end
namespace :test do namespace :test do
desc 'GitLab | Geo | DB | Test | Check for pending migrations and load the test schema' desc 'GitLab | Geo | DB | Test | Check for pending migrations and load the test schema'
task prepare: [:environment] do task prepare: [:environment] do
ns['test:load'].invoke Rake::Task['db:test:prepare:geo'].invoke
log_deprecated_message('geo:db:test:prepare', 'db:test:prepare:geo')
end end
# desc "Recreate the test database from the current schema" desc "GitLab | Geo | DB | Test | Recreate the test database from the current schema"
task load: [:environment, 'geo:db:test:purge'] do task load: [:environment] do
Gitlab::Geo::DatabaseTasks::Test.load Rake::Task['db:test:load:geo'].invoke
log_deprecated_message('geo:db:test:load', 'db:test:load:geo')
end end
# desc "Empty the test database" desc "GitLab | Geo | DB | Test | Empty the test database"
task purge: [:environment] do task purge: [:environment] do
Gitlab::Geo::DatabaseTasks::Test.purge Rake::Task['db:test:purge:geo'].invoke
log_deprecated_message('geo:db:test:purge', 'db:test:purge:geo')
end end
end end
end end
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Geo::DatabaseTasks, :reestablished_active_record_base do
let(:schema_file) { Rails.root.join('tmp', 'tests', 'geo_structure.sql').to_s }
subject { described_class }
before do
stub_env('SCHEMA', schema_file) # schema will be dumped to this file
end
after do
FileUtils.rm_rf(schema_file)
end
[:drop_current, :create_current, :migrate, :load_seed, :load_schema_current].each do |method_name|
it "defines the missing method #{method_name}" do
is_expected.to respond_to(method_name)
end
it "forwards method #{method_name} to ActiveRecord::Tasks::DatabaseTasks" do
expect(ActiveRecord::Tasks::DatabaseTasks).to receive(method_name)
subject.public_send(method_name)
end
end
describe '.rollback' do
context 'ENV["STEP"] not set' do
it 'calls ActiveRecord::MigrationContext.rollback with step 1' do
expect_next_instance_of(ActiveRecord::MigrationContext) do |migration_context|
expect(migration_context).to receive(:rollback).with(1)
end
subject.rollback
end
end
end
describe '.version' do
it 'returns a Number' do
expect(subject.version).to be_an(Integer)
end
end
describe '.dump_schema_after_migration?' do
it 'returns a true value' do
expect(subject.dump_schema_after_migration?).to be_truthy
end
end
describe '.pending_migrations' do
it 'returns an array' do
expect(subject.pending_migrations).to be_an(Array)
end
end
describe described_class::Schema do
describe '.dump' do
it 'calls ActiveRecord::Tasks::DatabaseTasks.dump_schema' do
expect(ActiveRecord::Tasks::DatabaseTasks).to receive(:dump_schema)
subject.dump
end
end
end
describe described_class::Migrate do
describe '.up' do
it 'requires ENV["VERSION"] to be set' do
stub_env('VERSION', nil)
expect { subject.up }.to raise_error(/VERSION is required/)
end
it 'calls ActiveRecord::Migrator.run' do
stub_env('VERSION', '19700101120000')
expect_next_instance_of(ActiveRecord::MigrationContext) do |migration_context|
expect(migration_context).to receive(:run).with(:up, any_args)
end
subject.up
end
end
describe '.down' do
it 'requires ENV["VERSION"] to be set' do
stub_env('VERSION', nil)
expect { subject.down }.to raise_error(/VERSION is required/)
end
it 'calls ActiveRecord::Migrator.run' do
stub_env('VERSION', '19700101120000')
expect_next_instance_of(ActiveRecord::MigrationContext) do |migration_context|
expect(migration_context).to receive(:run).with(:down, any_args)
end
subject.down
end
end
describe '.status' do
it 'outputs "database: gitlabhq_geo_test"' do
expect(ActiveRecord::SchemaMigration).to receive(:normalized_versions).and_return([])
expect { subject.status }.to output(/database: gitlabhq_geo_test/).to_stdout
end
end
end
describe described_class::Test do
describe '.load' do
it 'calls ActiveRecord::Tasks::DatabaseTasks.load_schema' do
expect(ActiveRecord::Tasks::DatabaseTasks).to receive(:load_schema)
subject.load
end
end
describe '.purge' do
it 'calls ActiveRecord::Tasks::DatabaseTasks.purge' do
expect(ActiveRecord::Tasks::DatabaseTasks).to receive(:purge)
subject.purge
end
end
end
end
...@@ -22,7 +22,7 @@ module EE ...@@ -22,7 +22,7 @@ module EE
override :migrations_paths override :migrations_paths
def migrations_paths def migrations_paths
if geo_migration? if geo_migration?
::Gitlab::Geo::DatabaseTasks.geo_migrations_paths geo_db_config.configuration_hash[:migrations_paths]
else else
super super
end end
...@@ -54,5 +54,9 @@ module EE ...@@ -54,5 +54,9 @@ module EE
def geo_migration? def geo_migration?
self.class.metadata[:geo] self.class.metadata[:geo]
end end
def geo_db_config
Geo::TrackingBase.connection_db_config
end
end end
end end
...@@ -16,216 +16,40 @@ RSpec.describe 'geo rake tasks', :geo, :silence_stdout do ...@@ -16,216 +16,40 @@ RSpec.describe 'geo rake tasks', :geo, :silence_stdout do
stub_licensed_features(geo: true) stub_licensed_features(geo: true)
end end
it 'Gitlab:Geo::DatabaseTasks responds to all methods used in Geo rake tasks' do context 'custom geo:db:* rake tasks' do
%i[ using RSpec::Parameterized::TableSyntax
drop_current
create_current where(:deprecated_task, :task_to_invoke) do
migrate 'geo:db:create' | 'db:create:geo'
rollback 'geo:db:drop' | 'db:drop:geo'
version 'geo:db:migrate' | 'db:migrate:geo'
load_schema_current 'geo:db:migrate:down' | 'db:migrate:down:geo'
load_seed 'geo:db:migrate:redo' | 'db:migrate:redo:geo'
dump_schema_after_migration? 'geo:db:migrate:status' | 'db:migrate:status:geo'
pending_migrations 'geo:db:migrate:up' | 'db:migrate:up:geo'
].each do |method| 'geo:db:reset' | 'db:reset:geo'
expect(Gitlab::Geo::DatabaseTasks).to respond_to(method) 'geo:db:rollback' | 'db:rollback:geo'
end 'geo:db:schema:dump' | 'db:schema:dump:geo'
end 'geo:db:schema:load' | 'db:schema:load:geo'
'geo:db:seed' | 'db:seed:geo'
it 'Gitlab::Geo::GeoTasks responds to all methods used in Geo rake tasks' do 'geo:db:setup' | 'db:setup:geo'
%i[ 'geo:db:test:load' | 'db:test:load:geo'
set_primary_geo_node 'geo:db:test:prepare' | 'db:test:prepare:geo'
update_primary_geo_node_url 'geo:db:test:purge' | 'db:test:purge:geo'
].each do |method| 'geo:db:version' | 'db:version:geo'
expect(Gitlab::Geo::GeoTasks).to respond_to(method) end
end
end with_them do
it "logs a deprecated message and invokes the Rails built-in task" do
it 'Gitlab::Geo::DatabaseTasks::Migrate responds to all methods used in Geo rake tasks' do expect(Rake::Task[task_to_invoke]).to receive(:invoke).and_return(true)
%i[up down status].each do |method|
expect(Gitlab::Geo::DatabaseTasks::Migrate).to respond_to(method) expect { run_rake_task(deprecated_task) }
end .to output("DEPRECATION WARNING: Using `bin/rails #{deprecated_task}` is deprecated and will be removed in Gitlab 15.0. Please run `bin/rails #{task_to_invoke}` instead.\n")
end .to_stdout
it 'Gitlab::Geo::DatabaseTasks::Test responds to all methods used in Geo rake tasks' do
%i[load purge].each do |method|
expect(Gitlab::Geo::DatabaseTasks::Test).to respond_to(method)
end
end
it 'Gitlab::Geo::DatabaseTasks::Schema responds to .dump method used in Geo rake tasks' do
expect(Gitlab::Geo::DatabaseTasks::Schema).to respond_to(:dump)
end
describe 'geo:db:drop' do
it 'drops the current database' do
expect(Gitlab::Geo::DatabaseTasks).to receive(:drop_current)
run_rake_task('geo:db:drop')
end
end
describe 'geo:db:create' do
it 'creates a Geo tracking database' do
expect(Gitlab::Geo::DatabaseTasks).to receive(:create_current)
run_rake_task('geo:db:create')
end
end
describe 'geo:db:migrate' do
it 'migrates a Geo tracking database' do
expect(Gitlab::Geo::DatabaseTasks).to receive(:migrate)
expect(Rake::Task['geo:db:_dump']).to receive(:invoke)
run_rake_task('geo:db:migrate')
end
end
describe 'geo:db:rollback' do
it 'rolls back a Geo tracking database' do
expect(Gitlab::Geo::DatabaseTasks).to receive(:rollback)
expect(Rake::Task['geo:db:_dump']).to receive(:invoke)
run_rake_task('geo:db:rollback')
end
end
describe 'geo:db:version' do
it 'retrieves current schema version number' do
expect(Gitlab::Geo::DatabaseTasks).to receive(:version)
run_rake_task('geo:db:version')
end
end
describe 'geo:db:reset' do
it 'drops, recreates database, and loads seeds' do
expect(Rake::Task['geo:db:drop']).to receive(:invoke)
expect(Rake::Task['geo:db:create']).to receive(:invoke)
expect(Rake::Task['geo:db:setup']).to receive(:invoke)
run_rake_task('geo:db:reset')
end
end
describe 'geo:db:seed' do
it 'loads seed data' do
allow(Rake::Task['geo:db:abort_if_pending_migrations']).to receive(:invoke).and_return(false)
expect(Gitlab::Geo::DatabaseTasks).to receive(:load_seed)
run_rake_task('geo:db:seed')
end
end
describe 'geo:db:_dump' do
it 'dumps the schema' do
allow(Gitlab::Geo::DatabaseTasks).to receive(:dump_schema_after_migration?).and_return(true)
expect(Rake::Task['geo:db:schema:dump']).to receive(:invoke)
run_rake_task('geo:db:_dump')
end
end
describe 'geo:db:abort_if_pending_migrations' do
it 'raises an error if there are pending migrations' do
pending_migration = double('pending migration', version: 12, name: 'Test')
allow(Gitlab::Geo::DatabaseTasks).to receive(:pending_migrations).and_return([pending_migration])
expect { run_rake_task('geo:db:abort_if_pending_migrations') }.to raise_error(%{Run `rake geo:db:migrate` to update your database then try again.})
end
end
describe 'geo:db:schema_load' do
it 'loads schema file into database' do
allow(ENV).to receive(:[]).with('SCHEMA')
expect(Gitlab::Geo::DatabaseTasks).to receive(:load_schema_current).with(:sql, ENV['SCHEMA'])
run_rake_task('geo:db:schema:load')
end
end
describe 'geo:db:schema_dump' do
it 'creates a clean structure.sql file', :reestablished_active_record_base do
expect(Gitlab::Geo::DatabaseTasks::Schema).to receive(:dump)
expect(Rake::Task['gitlab:db:clean_structure_sql']).to receive(:invoke)
run_rake_task('geo:db:schema:dump')
end
end
describe 'geo:db:migrate:up' do
it 'runs up method for given migration' do
expect(Gitlab::Geo::DatabaseTasks::Migrate).to receive(:up)
expect(Rake::Task['geo:db:_dump']).to receive(:invoke)
run_rake_task('geo:db:migrate:up')
end
end
describe 'geo:db:migrate:down' do
it 'runs down method for given migration' do
expect(Gitlab::Geo::DatabaseTasks::Migrate).to receive(:down)
expect(Rake::Task['geo:db:_dump']).to receive(:invoke)
run_rake_task('geo:db:migrate:down')
end
end
describe 'geo:db:migrate:redo' do
context 'without env var set' do
it 'does not run migrations' do
expect(Rake::Task['geo:db:migrate:down']).not_to receive(:invoke)
expect(Rake::Task['geo:db:rollback']).to receive(:invoke)
run_rake_task('geo:db:migrate:redo')
end
end
context 'with env var set' do
it 'does run migrations' do
allow(ENV).to receive(:[]).with('VERSION').and_return(1)
expect(Rake::Task['geo:db:migrate:down']).to receive(:invoke)
expect(Rake::Task['geo:db:rollback']).not_to receive(:invoke)
run_rake_task('geo:db:migrate:redo')
end end
end end
end end
describe 'geo:db:migrate:status' do
it 'displays migration status' do
expect(Gitlab::Geo::DatabaseTasks::Migrate).to receive(:status)
run_rake_task('geo:db:migrate:status')
end
end
describe 'geo:db:test:prepare' do
it 'check for pending migrations and load schema in test environment' do
expect(Rake::Task['geo:db:test:load']).to receive(:invoke)
run_rake_task('geo:db:test:prepare')
end
end
describe 'geo:db:test:load' do
it 'recreates database in test environment' do
expect(Gitlab::Geo::DatabaseTasks::Test).to receive(:load)
expect(Gitlab::Geo::DatabaseTasks::Test).to receive(:purge)
run_rake_task('geo:db:test:load')
end
end
describe 'geo:db:test:purge' do
it 'empties database in test environment' do
expect(Gitlab::Geo::DatabaseTasks::Test).to receive(:purge)
run_rake_task('geo:db:test:purge')
end
end
describe 'geo:set_primary_node' do describe 'geo:set_primary_node' do
before do before do
stub_config_setting(url: 'https://example.com:1234/relative_part') stub_config_setting(url: 'https://example.com:1234/relative_part')
......
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