-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix database connection using a socket on CI
- Loading branch information
Showing
5 changed files
with
106 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module ConnectionHelper | ||
def self.connection_for(dbname) | ||
require "pg" | ||
|
||
options = { | ||
:host => ENV["POSTGRESQL_HOST"], | ||
:user => ENV["POSTGRESQL_USER"], | ||
:password => ENV["POSTGRESQL_PASSWORD"], | ||
:dbname => dbname | ||
}.compact | ||
|
||
PG::Connection.new(options) | ||
end | ||
|
||
def self.source_database_connection | ||
@source_database_connection ||= connection_for("logical_test").tap do |c| | ||
c.set_notice_receiver { |r| nil } | ||
end | ||
end | ||
|
||
def self.target_database_connection | ||
@target_database_connection ||= connection_for("logical_test_target").tap do |c| | ||
c.set_notice_receiver { |r| nil } | ||
end | ||
end | ||
|
||
def self.with_each_connection | ||
[source_database_connection, target_database_connection].each do |conn| | ||
yield conn | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require_relative "connection_helper" | ||
|
||
module DatabaseHelper | ||
def self.tables | ||
%w(table1 table2 table3 table4) | ||
end | ||
|
||
def self.create_tables | ||
ConnectionHelper.with_each_connection do |conn| | ||
tables.each do |t| | ||
conn.async_exec(<<-SQL) | ||
CREATE TABLE IF NOT EXISTS #{t} ( | ||
id SERIAL PRIMARY KEY, | ||
data VARCHAR(50) | ||
) | ||
SQL | ||
end | ||
end | ||
end | ||
|
||
def self.drop_tables | ||
ConnectionHelper.with_each_connection do |conn| | ||
tables.each { |t| conn.async_exec("DROP TABLE IF EXISTS #{t}") } | ||
end | ||
end | ||
|
||
def self.drop_subscriptions | ||
conn = ConnectionHelper.target_database_connection | ||
# Subscriptions are visible from all databases in the cluster so we need to specify only the subs from the target database. | ||
conn.async_exec("SELECT subname::TEXT FROM pg_subscription AS sub JOIN pg_database ON sub.subdbid = pg_database.oid WHERE pg_database.datname = current_database()").values.flatten.each do |s| | ||
conn.async_exec("ALTER subscription #{s} DISABLE") | ||
conn.async_exec("ALTER subscription #{s} SET (slot_name = NONE)") | ||
conn.async_exec("DROP SUBSCRIPTION #{s}") | ||
end | ||
end | ||
|
||
def self.drop_publications | ||
conn = ConnectionHelper.source_database_connection | ||
conn.async_exec("SELECT pubname::TEXT from pg_publication").values.flatten.each do |p| | ||
conn.async_exec("DROP PUBLICATION #{p}") | ||
end | ||
end | ||
|
||
def self.drop_replication_slots | ||
conn = ConnectionHelper.source_database_connection | ||
# replication_slots are visible from all databases in the cluster so we need to specify only the slots from the source database. | ||
conn.async_exec("SELECT slot_name::TEXT FROM pg_replication_slots WHERE slot_type = 'logical' AND NOT active AND database = current_database()").values.flatten.each do |slot| | ||
conn.async_exec("SELECT pg_drop_replication_slot('#{slot}')") | ||
end | ||
end | ||
|
||
def self.with_clean_environment | ||
yield | ||
ensure | ||
drop_subscriptions | ||
drop_publications | ||
drop_replication_slots | ||
end | ||
end |