Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Let db adapter to decide how to handle database target and source names. #9

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions lib/veksel/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ def self.fork
require 'ostruct'
require 'fileutils'

config = read_config('config/database.yml')[:development]

target_database = config[:database] + Veksel.suffix
db = OpenStruct.new(configuration_hash: config, database: target_database)
Veksel::Commands::Fork.new(db).perform
database_config = read_config('config/database.yml')[:development]
Veksel::Commands::Fork.new(database_config).perform

duration = ((Time.now.to_f - t1) * 1000).to_i
FileUtils.touch('tmp/restart.txt')
Expand Down
9 changes: 5 additions & 4 deletions lib/veksel/commands/fork.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ module Commands
class Fork
attr_reader :pg_cluster, :source_db, :target_db

def initialize(db)
@pg_cluster = PgCluster.new(db.configuration_hash)
@source_db = db.database.sub(%r[#{Veksel.suffix}$], '')
@target_db = db.database
def initialize(database_config)
@pg_cluster = PgCluster.new(database_config)
@source_db = pg_cluster.source_database_name
@target_db = pg_cluster.target_database_name

raise "Source and target databases cannot be the same" if source_db == target_db
end

Expand Down
22 changes: 15 additions & 7 deletions lib/veksel/pg_cluster.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module Veksel
class PgCluster
attr_reader :configuration_hash
attr_reader :database_config

def initialize(configuration_hash)
@configuration_hash = configuration_hash
def initialize(database_config)
@database_config = database_config
end

def target_populated?(dbname)
Expand Down Expand Up @@ -40,6 +40,14 @@ def drop_database(dbname, dry_run: false)
spawn(pg_env, %[dropdb --no-password #{dbname}])
end

def source_database_name
database_config[:database]
end

def target_database_name
database_config[:database] + Veksel.suffix
end

private

def pg_connection_args(dbname)
Expand All @@ -48,10 +56,10 @@ def pg_connection_args(dbname)

def pg_env
{
'PGHOST' => configuration_hash[:host],
'PGPORT' => configuration_hash[:port]&.to_s,
'PGUSER' => configuration_hash[:username],
'PGPASSWORD' => configuration_hash[:password],
'PGHOST' => database_config[:host],
'PGPORT' => database_config[:port]&.to_s,
'PGUSER' => database_config[:username],
'PGPASSWORD' => database_config[:password],
}.compact
end
end
Expand Down