Commit ba43e7aa authored by Kamil Trzciński's avatar Kamil Trzciński Committed by Adam Hegyi

Adapt `SchemaMigrations::Context` to support config

This adds a support for `schema_migrations_path`
in a similar way to `migrations_paths`
for managing database configs.
parent 260c1406
......@@ -61,7 +61,6 @@ development:
adapter: postgresql
encoding: unicode
database: gitlabhq_development_ci
migrations_paths: db/ci_migrate
host: /path/to/gdk/postgresql
pool: 10
prepared_statements: false
......@@ -82,7 +81,6 @@ test: &test
adapter: postgresql
encoding: unicode
database: gitlabhq_test_ci
migrations_paths: db/ci_migrate
host: /path/to/gdk/postgresql
pool: 10
prepared_statements: false
......
......@@ -6,17 +6,14 @@ module Gitlab
class Context
attr_reader :connection
DEFAULT_SCHEMA_MIGRATIONS_PATH = "db/schema_migrations"
def initialize(connection)
@connection = connection
end
def schema_directory
@schema_directory ||=
if ActiveRecord::Base.configurations.primary?(database_name)
File.join(db_dir, 'schema_migrations')
else
File.join(db_dir, "#{database_name}_schema_migrations")
end
@schema_directory ||= Rails.root.join(database_schema_migrations_path).to_s
end
def versions_to_create
......@@ -32,8 +29,8 @@ module Gitlab
@database_name ||= @connection.pool.db_config.name
end
def db_dir
@db_dir ||= Rails.application.config.paths["db"].first
def database_schema_migrations_path
@connection.pool.db_config.configuration_hash[:schema_migrations_path] || DEFAULT_SCHEMA_MIGRATIONS_PATH
end
end
end
......
......@@ -3,7 +3,8 @@
require 'spec_helper'
RSpec.describe Gitlab::Database::SchemaMigrations::Context do
let(:connection) { ActiveRecord::Base.connection }
let(:connection_class) { ActiveRecord::Base }
let(:connection) { connection_class.connection }
let(:context) { described_class.new(connection) }
......@@ -12,13 +13,65 @@ RSpec.describe Gitlab::Database::SchemaMigrations::Context do
expect(context.schema_directory).to eq(File.join(Rails.root, 'db/schema_migrations'))
end
context 'multiple databases' do
let(:connection) { Ci::CiDatabaseRecord.connection }
context 'CI database' do
let(:connection_class) { Ci::CiDatabaseRecord }
it 'returns a directory path that is database specific' do
skip_if_multiple_databases_not_setup
expect(context.schema_directory).to eq(File.join(Rails.root, 'db/ci_schema_migrations'))
expect(context.schema_directory).to eq(File.join(Rails.root, 'db/schema_migrations'))
end
end
context 'multiple databases' do
let(:connection_class) do
Class.new(::ApplicationRecord) do
self.abstract_class = true
def self.name
'Gitlab::Database::SchemaMigrations::Context::TestConnection'
end
end
end
let(:configuration_overrides) { {} }
before do
connection_class.establish_connection(
ActiveRecord::Base
.connection_pool
.db_config
.configuration_hash
.merge(configuration_overrides)
)
end
after do
connection_class.remove_connection
end
context 'when `schema_migrations_path` is configured as string' do
let(:configuration_overrides) do
{ "schema_migrations_path" => "db/ci_schema_migrations" }
end
it 'returns a configured directory path that' do
skip_if_multiple_databases_not_setup
expect(context.schema_directory).to eq(File.join(Rails.root, 'db/ci_schema_migrations'))
end
end
context 'when `schema_migrations_path` is configured as symbol' do
let(:configuration_overrides) do
{ schema_migrations_path: "db/ci_schema_migrations" }
end
it 'returns a configured directory path that' do
skip_if_multiple_databases_not_setup
expect(context.schema_directory).to eq(File.join(Rails.root, 'db/ci_schema_migrations'))
end
end
end
end
......
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