Skip to content

Commit

Permalink
chore: Let db adapter to decide how to handle database target and sou…
Browse files Browse the repository at this point in the history
…rce names.
  • Loading branch information
nashby committed Mar 13, 2024
1 parent 88ddc16 commit 52bf258
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
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

0 comments on commit 52bf258

Please sign in to comment.