Skip to content

Commit 85dd1df

Browse files
committed
Fix database connection using a socket on CI
1 parent 14b3869 commit 85dd1df

File tree

5 files changed

+103
-82
lines changed

5 files changed

+103
-82
lines changed

.github/workflows/ci.yaml

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
env:
1919
POSTGRESQL_USER: root
2020
POSTGRESQL_PASSWORD: smartvm
21-
POSTGRESQL_DATABASE: vmdb_production
21+
POSTGRESQL_DATABASE: temp
2222
options: >-
2323
--name postgres
2424
--volume /tmp/postgresql-cfg/:/opt/app-root/src/postgresql-cfg/
@@ -28,6 +28,10 @@ jobs:
2828
--health-retries 5
2929
ports:
3030
- 5432:5432
31+
env:
32+
POSTGRESQL_HOST: localhost
33+
POSTGRESQL_USER: root
34+
POSTGRESQL_PASSWORD: smartvm
3135
steps:
3236
- uses: actions/checkout@v2
3337
- name: Override postgres settings
@@ -41,6 +45,8 @@ jobs:
4145
with:
4246
ruby-version: ${{ matrix.ruby-version }}
4347
bundler-cache: true
48+
- name: Set up tests
49+
run: bundle exec rake spec:setup
4450
- name: Run tests
4551
run: bundle exec rake
4652
env:

Rakefile

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
require "bundler/gem_tasks"
22
require "rspec/core/rake_task"
33

4+
require_relative "spec/support/connection_helper"
5+
46
def create_database(dbname)
5-
require "pg"
6-
c = PG::Connection.new(:dbname => "postgres")
7+
c = ConnectionHelper.connection_for("postgres")
78
c.async_exec("CREATE DATABASE #{dbname}")
89
rescue PG::DuplicateDatabase => err
910
raise unless err.message =~ /already exists/
1011
end
1112

1213
def drop_database(dbname)
13-
require "pg"
14-
c = PG::Connection.new(:dbname => "postgres")
14+
c = ConnectionHelper.connection_for("postgres")
1515
c.async_exec("DROP DATABASE #{dbname}")
1616
rescue PG::InvalidCatalogName => err
1717
raise unless err.message =~ /does not exist/

spec/spec_helper.rb

+1-77
Original file line numberDiff line numberDiff line change
@@ -4,80 +4,4 @@
44
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
55
require "pg-logical_replication"
66

7-
module ConnectionHelper
8-
def self.source_database_connection
9-
@source_database_connection ||= PG::Connection.new(:dbname => "logical_test").tap do |c|
10-
c.set_notice_receiver { |r| nil }
11-
end
12-
end
13-
14-
def self.target_database_connection
15-
@target_database_connection ||= PG::Connection.new(:dbname => "logical_test_target").tap do |c|
16-
c.set_notice_receiver { |r| nil }
17-
end
18-
end
19-
20-
def self.with_each_connection
21-
[source_database_connection, target_database_connection].each do |conn|
22-
yield conn
23-
end
24-
end
25-
end
26-
27-
module DatabaseHelper
28-
def self.tables
29-
%w(table1 table2 table3 table4)
30-
end
31-
32-
def self.create_tables
33-
ConnectionHelper.with_each_connection do |conn|
34-
tables.each do |t|
35-
conn.async_exec(<<-SQL)
36-
CREATE TABLE IF NOT EXISTS #{t} (
37-
id SERIAL PRIMARY KEY,
38-
data VARCHAR(50)
39-
)
40-
SQL
41-
end
42-
end
43-
end
44-
45-
def self.drop_tables
46-
ConnectionHelper.with_each_connection do |conn|
47-
tables.each { |t| conn.async_exec("DROP TABLE IF EXISTS #{t}") }
48-
end
49-
end
50-
51-
def self.drop_subscriptions
52-
conn = ConnectionHelper.target_database_connection
53-
# Subscriptions are visible from all databases in the cluster so we need to specify only the subs from the target database.
54-
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|
55-
conn.async_exec("ALTER subscription #{s} DISABLE")
56-
conn.async_exec("ALTER subscription #{s} SET (slot_name = NONE)")
57-
conn.async_exec("DROP SUBSCRIPTION #{s}")
58-
end
59-
end
60-
61-
def self.drop_publications
62-
conn = ConnectionHelper.source_database_connection
63-
conn.async_exec("SELECT pubname::TEXT from pg_publication").values.flatten.each do |p|
64-
conn.async_exec("DROP PUBLICATION #{p}")
65-
end
66-
end
67-
68-
def self.drop_replication_slots
69-
conn = ConnectionHelper.source_database_connection
70-
# replication_slots are visible from all databases in the cluster so we need to specify only the slots from the source database.
71-
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|
72-
conn.async_exec("SELECT pg_drop_replication_slot('#{slot}')")
73-
end
74-
end
75-
76-
def self.with_clean_environment
77-
yield
78-
ensure
79-
drop_subscriptions
80-
drop_publications
81-
drop_replication_slots
82-
end
83-
end
7+
Dir[File.join(__dir__, "support/**/*.rb")].each { |f| require f }

spec/support/connection_helper.rb

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module ConnectionHelper
2+
def self.connection_for(dbname)
3+
require "pg"
4+
5+
options = {
6+
:host => ENV["POSTGRESQL_HOST"],
7+
:user => ENV["POSTGRESQL_USER"],
8+
:password => ENV["POSTGRESQL_PASSWORD"],
9+
:dbname => dbname
10+
}.compact
11+
12+
PG::Connection.new(options)
13+
end
14+
15+
def self.source_database_connection
16+
@source_database_connection ||= connection_for("logical_test").tap do |c|
17+
c.set_notice_receiver { |r| nil }
18+
end
19+
end
20+
21+
def self.target_database_connection
22+
@target_database_connection ||= connection_for("logical_test_target").tap do |c|
23+
c.set_notice_receiver { |r| nil }
24+
end
25+
end
26+
27+
def self.with_each_connection
28+
[source_database_connection, target_database_connection].each do |conn|
29+
yield conn
30+
end
31+
end
32+
end

spec/support/database_helper.rb

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require_relative "connection_helper"
2+
3+
module DatabaseHelper
4+
def self.tables
5+
%w(table1 table2 table3 table4)
6+
end
7+
8+
def self.create_tables
9+
ConnectionHelper.with_each_connection do |conn|
10+
tables.each do |t|
11+
conn.async_exec(<<-SQL)
12+
CREATE TABLE IF NOT EXISTS #{t} (
13+
id SERIAL PRIMARY KEY,
14+
data VARCHAR(50)
15+
)
16+
SQL
17+
end
18+
end
19+
end
20+
21+
def self.drop_tables
22+
ConnectionHelper.with_each_connection do |conn|
23+
tables.each { |t| conn.async_exec("DROP TABLE IF EXISTS #{t}") }
24+
end
25+
end
26+
27+
def self.drop_subscriptions
28+
conn = ConnectionHelper.target_database_connection
29+
# Subscriptions are visible from all databases in the cluster so we need to specify only the subs from the target database.
30+
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|
31+
conn.async_exec("ALTER subscription #{s} DISABLE")
32+
conn.async_exec("ALTER subscription #{s} SET (slot_name = NONE)")
33+
conn.async_exec("DROP SUBSCRIPTION #{s}")
34+
end
35+
end
36+
37+
def self.drop_publications
38+
conn = ConnectionHelper.source_database_connection
39+
conn.async_exec("SELECT pubname::TEXT from pg_publication").values.flatten.each do |p|
40+
conn.async_exec("DROP PUBLICATION #{p}")
41+
end
42+
end
43+
44+
def self.drop_replication_slots
45+
conn = ConnectionHelper.source_database_connection
46+
# replication_slots are visible from all databases in the cluster so we need to specify only the slots from the source database.
47+
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|
48+
conn.async_exec("SELECT pg_drop_replication_slot('#{slot}')")
49+
end
50+
end
51+
52+
def self.with_clean_environment
53+
yield
54+
ensure
55+
drop_subscriptions
56+
drop_publications
57+
drop_replication_slots
58+
end
59+
end

0 commit comments

Comments
 (0)