Skip to content
Snippets Groups Projects
Commit 334c788e authored by Rutger Wessels's avatar Rutger Wessels
Browse files

Add class for switching to Decomposed DB setup

parent 1e639b8f
No related branches found
No related tags found
No related merge requests found
......@@ -31,7 +31,8 @@ def connection
private
def configure_model(name)
source_model = Gitlab::Database.database_base_models_with_gitlab_shared[name]
source_model = Gitlab::Database.database_base_models_with_gitlab_shared[name] ||
Gitlab::Database.database_base_models_with_gitlab_shared['main']
@model = backup_model_for(name)
......
# frozen_string_literal: true
module Gitlab
module Database
module Decomposition
class Migrate
def initialize
@main_dump_location = File.join(Gitlab.config.backup.path, 'migration')
end
def process!
dump_main_db
import_dump_to_ci_db
FileUtils.remove_entry_secure(@main_dump_location, true)
end
private
def with_transient_pg_env(extended_env)
ENV.merge!(extended_env)
result = yield
ENV.reject! { |k, _| extended_env.key?(k) }
result
end
def import_dump_to_ci_db
ci_database_name = "#{main_config.dig(:activerecord, :database)}_ci"
with_transient_pg_env(ci_config[:pg_env]) do
restore_args = ["--jobs=4", "--dbname=#{ci_database_name}"]
Open3.capture2e('pg_restore', *restore_args, @main_dump_location)
end
end
def dump_main_db
with_transient_pg_env(main_config[:pg_env]) do
args = ['--format=d', '--jobs=4', "--file=#{@main_dump_location}"]
Open3.capture2e('pg_dump', *args, main_config.dig(:activerecord, :database))
end
end
def main_config
@main_config ||= ::Backup::DatabaseModel.new('main').config
end
def ci_config
@ci_config ||= ::Backup::DatabaseModel.new('ci').config
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Database::Decomposition::Migrate, :delete, query_analyzers: false, feature_category: :cell do
let(:ci_database_name) do
config = ActiveRecord::Base.configurations.find_db_config(Rails.env).configuration_hash
"#{config[:database]}_ci"
end
let(:ci_connection) do
database_model = self.class.const_set(:TestCiApplicationRecord, Class.new(ApplicationRecord))
database_model.establish_connection(
ActiveRecord::DatabaseConfigurations::HashConfig.new(
ActiveRecord::Base.connection_db_config.env_name,
'ci',
ActiveRecord::Base.connection_db_config.configuration_hash.dup.merge(database: ci_database_name)
)
)
Gitlab::Database::LoadBalancing::Setup.new(database_model).setup
database_model.connection
end
let!(:milestone) { create(:milestone) }
let!(:ci_pipeline) { create(:ci_pipeline) }
before do
skip_if_database_exists(:ci)
# Database `ci` is not configured. But it can still exist. So drop and create it
ActiveRecord::Base.connection.execute("DROP DATABASE IF EXISTS #{ci_database_name}")
ActiveRecord::Base.connection.execute("CREATE DATABASE #{ci_database_name}")
end
after do
Milestone.delete_all
Ci::Pipeline.delete_all
end
describe '#process!' do
subject(:process) { described_class.new.process! }
it 'copies main database to ci database' do
process
ci_milestones = ci_connection.execute("SELECT COUNT(*) FROM milestones").getvalue(0, 0)
ci_pipelines = ci_connection.execute("SELECT COUNT(*) FROM ci_pipelines").getvalue(0, 0)
expect(ci_milestones).to be(Milestone.count)
expect(ci_pipelines).to be(Ci::Pipeline.count)
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment