Skip to content

Commit

Permalink
ref!: move db naming conventions to PgCluster
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
* Veksel.prefix has been removed
* Veksel.suffix no longer has a leading underscore
* CLI: suffix command has been removed
  • Loading branch information
theodorton committed Mar 16, 2024
1 parent 4e5aa2f commit c0df9aa
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 24 deletions.
2 changes: 0 additions & 2 deletions bin/veksel
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
require 'veksel/cli'

case ARGV[0]
when 'suffix'
Veksel::CLI.suffix
when 'fork'
Veksel::CLI.fork
end
4 changes: 0 additions & 4 deletions lib/veksel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,5 @@ def skip_fork?
def suffix
Suffix.new(current_branch).to_s
end

def prefix(dbname)
dbname.sub(%r[#{Veksel.suffix}$], '_')
end
end
end
9 changes: 3 additions & 6 deletions lib/veksel/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

module Veksel
module CLI
class << self
def suffix
print Veksel.suffix
end
DbConfig = Struct.new('DbConfig', :configuration_hash)

class << self
def fork
return if Veksel.skip_fork?
t1 = Time.now.to_f

require 'veksel/commands/fork'
require 'psych'
require 'ostruct'
require 'fileutils'

config = read_config('config/database.yml')[:development]
Expand All @@ -23,7 +20,7 @@ def fork
end

target_database = config[:database] + Veksel.suffix
db = OpenStruct.new(configuration_hash: config, database: target_database)
db = DbConfig.new(config)
if Veksel::Commands::Fork.new(db).perform
duration = ((Time.now.to_f - t1) * 1000).to_i
FileUtils.touch('tmp/restart.txt')
Expand Down
4 changes: 2 additions & 2 deletions lib/veksel/commands/clean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ def perform
active_branches = Veksel.active_branches

stale_databases = all_databases.filter do |database|
active_branches.none? { |branch| database.end_with?("_#{branch}") }
active_branches.none? { |branch| database.branch == branch }
end
stale_databases.each do |database|
@adapter.drop_database(database, dry_run: @dry_run)
@adapter.drop_database(database.name, dry_run: @dry_run)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/veksel/commands/fork.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class Fork

def initialize(db)
@adapter = Veksel.adapter_for(db.configuration_hash)
@source_db = db.database.sub(%r[#{Veksel.suffix}$], '')
@target_db = db.database
@source_db = adapter.main_database
@target_db = adapter.db_name_with_suffix(Veksel.suffix)
raise "Source and target databases cannot be the same" if source_db == target_db
end

Expand Down
4 changes: 2 additions & 2 deletions lib/veksel/commands/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def perform

hash = {}
databases.each do |database|
branch = database.sub(adapter.forked_database_prefix, '')
hash[branch] = database
branch = database.branch
hash[branch] = database.name
end

longest_branch_name = hash.keys.max_by(&:length).length
Expand Down
14 changes: 11 additions & 3 deletions lib/veksel/pg_cluster.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Veksel
class PgCluster
Database = Struct.new(:name, :branch)

attr_reader :configuration_hash

def initialize(configuration_hash)
Expand All @@ -11,11 +13,13 @@ def main_database
end

def forked_databases
list_databases(prefix: forked_database_prefix)
list_databases(prefix: forked_database_prefix).map do |name|
Database.new(name, name.sub(forked_database_prefix, ''))
end
end

def forked_database_prefix
"#{main_database}_"
def db_name_with_suffix(suffix)
"#{forked_database_prefix}#{suffix}"
end

def target_populated?(dbname)
Expand Down Expand Up @@ -54,6 +58,10 @@ def drop_database(dbname, dry_run: false)

private

def forked_database_prefix
"#{main_database}_"
end

def pg_connection_args(dbname)
"-d #{dbname} --no-password"
end
Expand Down
6 changes: 4 additions & 2 deletions lib/veksel/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class Railtie < ::Rails::Railtie
next if url.present?
next unless env_name == 'development' || env_name == 'test'
veksel_adapter = Veksel.adapter_for(config, exception: false)
database_name = "#{config[:database]}#{Veksel.suffix}"
next unless veksel_adapter&.target_populated?(database_name)
next unless veksel_adapter

database_name = veksel_adapter.db_name_with_suffix(Veksel.suffix)
next unless veksel_adapter.target_populated?(database_name)

ActiveRecord::DatabaseConfigurations::HashConfig.new(env_name, name, config.merge({
database: database_name,
Expand Down
2 changes: 1 addition & 1 deletion lib/veksel/suffix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def to_s
when *PROTECTED_BRANCHES
""
else
"_#{@branch_name}"
@branch_name
end
end
end
Expand Down

0 comments on commit c0df9aa

Please sign in to comment.