Skip to content

Commit

Permalink
Feature/add db dir config and refactor (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pysis868 authored Nov 17, 2023
1 parent e746e43 commit 4a542f2
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 89 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ schema.rb
spec/tmp
pkg
Gemfile.lock
tmp
1 change: 1 addition & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,4 @@ Contributors
- [Mithun James](https://github.com/drtechie)
- [Sarah Ridge](https://github.com/smridge)
- [John Bachir](https://jjb.cc)
- [Pysis](https://github.com/Pysis868)
115 changes: 72 additions & 43 deletions lib/standalone_migrations/configurator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require 'yaml'

module StandaloneMigrations

class InternalConfigurationsProxy

attr_reader :configurations
Expand Down Expand Up @@ -32,70 +31,100 @@ def self.environments_config
end

def initialize(options = {})
defaults = {
:config => "db/config.yml",
:migrate_dir => "db/migrate",
:root => Pathname.pwd,
:seeds => "db/seeds.rb",
}
@options = load_from_file(defaults.dup) || defaults.merge(options)
options = options.dup
@schema = options.delete('schema')
@config_overrides = defaults
c_os['paths'].merge!(options.delete('paths') || {})
c_os.merge!(options)

ENV['SCHEMA'] ||= schema if schema
Rails.application.config.root = root
Rails.application.config.paths["config/database"] = config
Rails.application.config.paths["db/migrate"] = migrate_dir
Rails.application.config.paths["db/seeds.rb"] = seeds
end
load_from_file

ENV['SCHEMA'] ||= @schema if @schema
rac = Rails.application.config

def config
@options[:config]
rac.root = c_os['root']
c_os['paths'].each do |path, value|
rac.paths[path] = value
end
end

def migrate_dir
@options[:migrate_dir]
def config_for_all
Configurator.load_configurations.dup
end

def root
@options[:root]
def config_for(environment)
config_for_all[environment.to_s]
end

def seeds
@options[:seeds]
def c_os
config_overrides
end
def config_overrides
@config_overrides
end

def schema
@options[:schema]
def c_o_p_m
config_override_path_mappings
end
def config_override_path_mappings
{
'config/database' => {
'config_key_path' => ['config', 'database'],
'default' => 'db/config.yml'
},
'db' => {
'config_key_path' => ['db' , 'dir' ],
'default' => 'db'
},
'db/migrate' => {
'config_key_path' => ['db' , 'migrate' ],
'default' => 'db/migrate'
},
'db/seeds.rb' => {
'config_key_path' => ['db' , 'seeds' ],
'default' => 'db/seeds.rb'
},
}
end

def config_for_all
Configurator.load_configurations.dup
def defaults
{
'paths' => c_o_p_m.map do |path, value|
[ path, value['default'] ]
end.to_h,
'root' => Pathname.pwd,
}
end

def config_for(environment)
config_for_all[environment.to_s]
def schema
@schema
end

private

def configuration_file
if !ENV['DATABASE']
".standalone_migrations"
else
".#{ENV['DATABASE']}.standalone_migrations"
end
".#{ENV['DATABASE']}.standalone_migrations".sub(/^\.\./, '.')
end

def load_from_file(defaults)
def load_from_file
return nil unless File.exist? configuration_file
config = YAML.load( ERB.new(IO.read(configuration_file)).result )
{
:config => config["config"] ? config["config"]["database"] : defaults[:config],
:migrate_dir => (config["db"] || {})["migrate"] || defaults[:migrate_dir],
:root => config["root"] || defaults[:root],
:seeds => (config["db"] || {})["seeds"] || defaults[:seeds],
:schema => (config["db"] || {})["schema"]
}
end
data = YAML.load( ERB.new(IO.read(configuration_file)).result )

@schema = data.dig('db', 'schema')

c_o_paths = c_o_p_m.map do |path, value|
[
path,
data.dig(*value['config_key_path'])
]
end.to_h.select { |key, value| value.present? }

c_o_paths = defaults['paths'].merge(c_o_paths)

@config_overrides = defaults.merge({
'paths' => c_o_paths,
'root' => data.dig('root'),
}.select { |key, value| value.present? })
end
end
end
136 changes: 90 additions & 46 deletions spec/standalone_migrations/configurator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,47 @@ module StandaloneMigrations
end
end

it "does not break / emit an error" do
expect { Configurator.new }.not_to raise_error
end

context "default values when .standalone_configurations is missing" do
let(:configurator) do
Configurator.new
end

it "use config/database.yml" do
expect(configurator.c_os['paths']['config/database']).to eq('db/config.yml')
end

it "use db dir" do
expect(configurator.c_os['paths']['db']).to eq('db')
end

it "use db/migrate dir" do
expect(configurator.c_os['paths']['db/migrate']).to eq('db/migrate')
end

it "use db/seeds.rb" do
expect(configurator.c_os['paths']['db/seeds.rb']).to eq("db/seeds.rb")
end
end

describe "environment yaml configuration loading" do

let(:env_hash_other_db) do
{
"development" => {"adapter" => "mysql2", "database" => "database_name"},
"test" => {"adapter" => "mysql2", "database" => "database_name"},
"production" => {"adapter" => "mysql2", "database" => "database_name"}
"test" => {"adapter" => "mysql2", "database" => "database_name"},
"production" => {"adapter" => "mysql2", "database" => "database_name"}
}
end

around(:each) do |example|
@env_hash = {
"development" => {"adapter" => "sqlite3", "database" => "db/development.sql"},
"test" => {"adapter" => "sqlite3", "database" => "db/test.sql"},
"production" => {"adapter" => "sqlite3", "database" => ":memory:"}
"test" => {"adapter" => "sqlite3", "database" => "db/test.sql" },
"production" => {"adapter" => "sqlite3", "database" => ":memory:" }
}
FileUtils.mkdir_p "db"
File.open("db/config.yml", "w") do |f|
Expand Down Expand Up @@ -88,31 +114,16 @@ module StandaloneMigrations

end

context "default values when .standalone_configurations is missing" do
let(:configurator) do
Configurator.new
end

it "use config/database.yml" do
expect(configurator.config).to eq('db/config.yml')
end

it "use db/migrate dir" do
expect(configurator.migrate_dir).to eq('db/migrate')
end

it "use db/seeds.rb" do
expect(configurator.seeds).to eq("db/seeds.rb")
end
end

context "passing configurations as a parameter" do
let(:args) do
{
:config => "custom/config/database.yml",
:migrate_dir => "custom/db/migrate",
:seeds => "custom/db/seeds.rb",
:schema => "custom/db/schema.rb"
'paths' => {
'config/database' => "custom/config/database.yml" ,
'db' => "db" ,
'db/migrate' => "custom/db/migrate" ,
'db/seeds.rb' => "custom/db/seeds.rb" ,
},
'schema' => "custom/db/schema.rb"
}
end

Expand All @@ -121,19 +132,31 @@ module StandaloneMigrations
end

it "use custom config" do
expect(configurator.config).to eq(args[:config])
expect(configurator.c_os['paths']['config/database']).to(
eq(args['paths']['config/database'])
)
end

it "use custom db dir" do
expect(configurator.c_os['paths']['db']).to(
eq(args['paths']['db'])
)
end

it "use custom migrate dir" do
expect(configurator.migrate_dir).to eq(args[:migrate_dir])
expect(configurator.c_os['paths']['db/migrate']).to(
eq(args['paths']['db/migrate'])
)
end

it "use custom seeds" do
expect(configurator.seeds).to eq(args[:seeds])
expect(configurator.c_os['paths']['db/seeds.rb']).to(
eq(args['paths']['db/seeds.rb'])
)
end

it "use custom schema" do
expect(configurator.schema).to eq(args[:schema])
expect(configurator.schema).to eq(args['schema'])
end

end
Expand All @@ -148,9 +171,10 @@ module StandaloneMigrations
let(:yaml_hash) do
{
"db" => {
"seeds" => "file/db/seeds.rb",
"migrate" => "file/db/migrate",
"schema" => "file/db/schema.rb"
"dir" => "file/db" ,
"migrate" => "file/db/migrate" ,
"seeds" => "file/db/seeds.rb" ,
"schema" => "file/db/schema.rb"
},
"config" => {
"database" => "file/config/database.yml"
Expand All @@ -161,9 +185,10 @@ module StandaloneMigrations
let(:yaml_hash_other_db) do
{
"db" => {
"seeds" => "db2/seeds.rb",
"migrate" => "db2/migrate",
"schema" => "db2/schema.rb"
"dir" => "db2" ,
"migrate" => "db2/migrate" ,
"seeds" => "db2/seeds.rb" ,
"schema" => "db2/schema.rb"
},
"config" => {
"database" => "config/config_other.yml"
Expand All @@ -180,19 +205,25 @@ module StandaloneMigrations
before(:each) do
ENV['DATABASE'] = "other_db"
file_other_db = ".other_db.standalone_migrations"
File.open(file_other_db, "w") { |file| file.write(yaml_hash_other_db.to_yaml) }
File.open(file_other_db, "w") do |file|
file.write(yaml_hash_other_db.to_yaml)
end
end

let(:other_configurator) do
Configurator.new
end

it "look up named dot file" do
expect(other_configurator.config).to eq(yaml_hash_other_db['config']['database'])
expect(other_configurator.c_os['paths']['config/database']).to(
eq(yaml_hash_other_db['config']['database'])
)
end

it "load config from named dot file" do
expect(other_configurator.migrate_dir).to eq('db2/migrate')
expect(other_configurator.c_os['paths']['db/migrate']).to(
eq('db2/migrate')
)
end

after(:all) do
Expand All @@ -215,33 +246,46 @@ module StandaloneMigrations
end

it "use default values for the missing configurations" do
expect(configurator.migrate_dir).to eq('db/migrate')
expect(configurator.c_os['paths']['db']).to(
eq('db' )
)
expect(configurator.c_os['paths']['db/migrate']).to(
eq('db/migrate' )
)
end

it "use custom config from file" do
expect(configurator.config).to eq(yaml_hash["config"]["database"])
expect(configurator.c_os['paths']['config/database']).to(
eq(yaml_hash["config"]["database"])
)
end

it "use custom config value from partial configuration" do
expect(configurator.seeds).to eq(yaml_hash["db"]["seeds"])
expect(configurator.c_os['paths']['db/seeds.rb']).to(
eq(yaml_hash["db"]["seeds"])
)
end

end

it "use custom config from file" do
expect(configurator.config).to eq(yaml_hash["config"]["database"])
expect(configurator.c_os['paths']['config/database']).to(
eq(yaml_hash["config"]["database"])
)
end

it "use custom migrate dir from file" do
expect(configurator.migrate_dir).to eq(yaml_hash["db"]["migrate"])
expect(configurator.c_os['paths']['db/migrate']).to eq(yaml_hash["db"]["migrate"])
end

it "use custom seeds from file" do
expect(configurator.seeds).to eq(yaml_hash["db"]["seeds"])
expect(configurator.c_os['paths']['db/seeds.rb']).to eq(yaml_hash["db"]["seeds"])
end

it "use custom schema from file" do
expect(configurator.schema).to eq(yaml_hash["db"]["schema"])
expect(configurator.schema).to(
eq(yaml_hash["db"]["schema"])
)
end

end
Expand Down

0 comments on commit 4a542f2

Please sign in to comment.