Skip to content

Commit c6af61a

Browse files
Implement pre and post scenario SQL execution in subscription ResQL tests to manage publication lifecycle. pgadmin-org#8932
1 parent 04024c7 commit c6af61a

4 files changed

Lines changed: 11 additions & 57 deletions

File tree

web/pgadmin/browser/server_groups/servers/databases/schemas/tables/templates/tables/sql/12_plus/create.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
add empty bracket with table name
1414
#}
1515
{% set empty_bracket = ""%}
16-
{% if data.coll_inherits|length == 0 and data.columns|length == 0 and not data.typname and not data.like_relation and data.primary_key|length == 0 and data.unique_constraint|length == 0 and data.foreign_key|length == 0 and data.check_constraint|length == 0 and data.exclude_constraint|length == 0 %}
16+
{% if not (data.coll_inherits or data.columns or data.typname or data.like_relation or data.primary_key or data.unique_constraint or data.foreign_key or data.check_constraint or data.exclude_constraint) %}
1717
{% set empty_bracket = "\n(\n)"%}
1818
{% endif %}
1919
{% set with_clause = false%}

web/pgadmin/browser/server_groups/servers/databases/subscriptions/tests/17_plus/test.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"endpoint": "NODE-publication.obj",
3535
"sql_endpoint": "NODE-publication.sql_id",
3636
"msql_endpoint": "NODE-publication.msql",
37+
"precondition_sql": "SELECT 1 FROM pg_replication_slots WHERE slot_name = 'test_create_subscription' UNION ALL SELECT 1 WHERE NOT EXISTS (SELECT 1 FROM pg_replication_slots WHERE slot_name = 'test_create_subscription') AND pg_create_logical_replication_slot('test_create_subscription', 'pgoutput', false) IS NOT NULL LIMIT 1;",
3738
"data": {
3839
"name": "test_publication",
3940
"evnt_insert": true,
@@ -116,7 +117,8 @@
116117
"endpoint": "NODE-subscription.delete_id",
117118
"data": {
118119
"name": "test_create_subscription"
119-
}
120+
},
121+
"post_scenario_sql": "SELECT 1 WHERE EXISTS (SELECT 1 FROM pg_replication_slots WHERE slot_name = 'test_create_subscription') AND (SELECT pg_drop_replication_slot('test_create_subscription')) IS NOT NULL UNION ALL SELECT 1 WHERE NOT EXISTS (SELECT 1 FROM pg_replication_slots WHERE slot_name = 'test_create_subscription') LIMIT 1;"
120122
}
121123
]
122124
}

web/pgadmin/browser/server_groups/servers/databases/subscriptions/tests/18_plus/test.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"endpoint": "NODE-subscription.obj",
77
"sql_endpoint": "NODE-subscription.sql_id",
88
"msql_endpoint": "NODE-subscription.msql",
9+
"precondition_sql": "SELECT 1 FROM pg_replication_slots WHERE slot_name = 'test_create_subscription' UNION ALL SELECT 1 WHERE NOT EXISTS (SELECT 1 FROM pg_replication_slots WHERE slot_name = 'test_create_subscription') AND pg_create_logical_replication_slot('test_create_subscription', 'pgoutput', false) IS NOT NULL LIMIT 1;",
910
"data": {
1011
"name": "test_create_subscription",
1112
"subowner": "postgres",
@@ -66,7 +67,8 @@
6667
"endpoint": "NODE-subscription.delete_id",
6768
"data": {
6869
"name": "test_create_subscription"
69-
}
70+
},
71+
"post_scenario_sql": "SELECT 1 WHERE EXISTS (SELECT 1 FROM pg_replication_slots WHERE slot_name = 'test_create_subscription') AND (SELECT pg_drop_replication_slot('test_create_subscription')) IS NOT NULL UNION ALL SELECT 1 WHERE NOT EXISTS (SELECT 1 FROM pg_replication_slots WHERE slot_name = 'test_create_subscription') LIMIT 1;"
7072
}
7173
]
7274
}

web/regression/re_sql/tests/test_resql.py

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -95,35 +95,6 @@ def setUp(self):
9595
# Added line break after scenario name
9696
print("")
9797

98-
# Create replication slot if it does not exist for the
99-
# RESQL test-cases of Subscriptions for PGv17 and above
100-
if self.server_information['server_version'] >= 170000:
101-
try:
102-
self.get_db_connection()
103-
pg_cursor = self.connection.cursor()
104-
pg_cursor.execute("""
105-
SELECT 1 FROM pg_replication_slots
106-
WHERE slot_name = 'test_create_subscription'
107-
""")
108-
exists = pg_cursor.fetchone()
109-
if not exists:
110-
pg_cursor.execute("""
111-
SELECT pg_create_logical_replication_slot(
112-
'test_create_subscription',
113-
'pgoutput',
114-
failover := false
115-
);
116-
""")
117-
self.connection.commit()
118-
print("Replication slot "
119-
"'test_create_subscription' created.")
120-
else:
121-
print("Replication slot 'test_create_subscription' "
122-
"already exists.")
123-
pg_cursor.close()
124-
except Exception as e:
125-
print("Could not create replication slot: ", e)
126-
12798
def runTest(self):
12899
""" Create the module list on which reverse engineeredsql test
129100
cases will be executed."""
@@ -182,30 +153,6 @@ def runTest(self):
182153
# Check the final status of the test case
183154
self.assertEqual(self.final_test_status, True)
184155

185-
def tearDown(self):
186-
# Drop the replication slot created for the RESQL test-cases of
187-
# Subscriptions, if it exists before disconnecting for PGv17 and above
188-
if self.server_information['server_version'] >= 170000:
189-
try:
190-
self.get_db_connection()
191-
pg_cursor = self.connection.cursor()
192-
pg_cursor.execute("""
193-
SELECT 1 FROM pg_replication_slots
194-
WHERE slot_name = 'test_create_subscription'
195-
""")
196-
exists = pg_cursor.fetchone()
197-
if exists:
198-
pg_cursor.execute("""
199-
SELECT
200-
pg_drop_replication_slot('test_create_subscription');
201-
""")
202-
self.connection.commit()
203-
print("Replication slot "
204-
"'test_create_subscription' dropped.")
205-
pg_cursor.close()
206-
except Exception as e:
207-
print("Could not drop replication slot: ", e)
208-
209156
database_utils.disconnect_database(
210157
self, self.server_information['server_id'],
211158
self.server_information['db_id'])
@@ -653,7 +600,10 @@ def check_precondition(self, precondition_sql, use_test_config_db_conn):
653600
try:
654601
pg_cursor.execute(precondition_sql)
655602
precondition_result = pg_cursor.fetchone()
656-
if len(precondition_result) >= 1 and precondition_result[0] == '1':
603+
if ((len(precondition_result) >= 1 and
604+
precondition_result[0] == '1') or
605+
(isinstance(precondition_result, tuple) and
606+
precondition_result[0] == 1)):
657607
precondition_flag = True
658608
except Exception as e:
659609
traceback.print_exc()

0 commit comments

Comments
 (0)