Class: ActiveRecord::ConnectionAdapters::PostgreSQL::Branched::Adapter

Inherits:
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
  • Object
show all
Defined in:
lib/active_record/connection_adapters/postgresql/branched/adapter.rb

Constant Summary collapse

ADAPTER_NAME =
"PostgreSQL Branched"
SHADOW_BEFORE =
%i[
  add_column
  remove_column
  rename_column
  change_column
  change_column_default
  change_column_null
  change_column_comment
  change_table_comment
  add_index
  remove_index
  rename_index
  add_foreign_key
  remove_foreign_key
  add_check_constraint
  remove_check_constraint
  validate_foreign_key
  validate_check_constraint
  drop_table
  change_table
  bulk_change_table
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAdapter

Returns a new instance of Adapter.



31
32
33
34
35
# File 'lib/active_record/connection_adapters/postgresql/branched/adapter.rb', line 31

def initialize(...)
  super
  @branch_manager = BranchManager.new(self, @config)
  @shadow = Shadow.new(self, @branch_manager.branch_schema) unless @branch_manager.primary_branch?
end

Instance Attribute Details

#branch_managerObject (readonly)

Returns the value of attribute branch_manager.



61
62
63
# File 'lib/active_record/connection_adapters/postgresql/branched/adapter.rb', line 61

def branch_manager
  @branch_manager
end

Instance Method Details

#configure_connectionObject



37
38
39
40
# File 'lib/active_record/connection_adapters/postgresql/branched/adapter.rb', line 37

def configure_connection
  super
  @branch_manager.activate(@shadow)
end

#rename_table(table_name, new_name, **options) ⇒ Object

rename_table needs special handling: the shadow table’s sequences live in public, but Rails’ rename_table tries to rename them using the branch schema. The table and index renames succeed before the sequence rename fails, so we rescue the sequence error.



53
54
55
56
57
58
59
# File 'lib/active_record/connection_adapters/postgresql/branched/adapter.rb', line 53

def rename_table(table_name, new_name, **options)
  @shadow&.call(table_name)
  super
rescue ActiveRecord::StatementInvalid => e
  raise if @branch_manager.primary_branch?
  raise unless e.cause.is_a?(PG::UndefinedTable)
end