Implement pre and post scenario SQL execution in subscription ResQL tests to manage publication lifecycle. #8932

pull/9214/head
Yogesh Mahajan 2025-10-03 12:12:15 +05:30 committed by GitHub
parent 04024c7f61
commit c6af61afd3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 11 additions and 57 deletions

View File

@ -13,7 +13,7 @@
add empty bracket with table name
#}
{% set empty_bracket = ""%}
{% 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 %}
{% 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) %}
{% set empty_bracket = "\n(\n)"%}
{% endif %}
{% set with_clause = false%}

View File

@ -34,6 +34,7 @@
"endpoint": "NODE-publication.obj",
"sql_endpoint": "NODE-publication.sql_id",
"msql_endpoint": "NODE-publication.msql",
"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;",
"data": {
"name": "test_publication",
"evnt_insert": true,
@ -116,7 +117,8 @@
"endpoint": "NODE-subscription.delete_id",
"data": {
"name": "test_create_subscription"
}
},
"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;"
}
]
}

View File

@ -6,6 +6,7 @@
"endpoint": "NODE-subscription.obj",
"sql_endpoint": "NODE-subscription.sql_id",
"msql_endpoint": "NODE-subscription.msql",
"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;",
"data": {
"name": "test_create_subscription",
"subowner": "postgres",
@ -66,7 +67,8 @@
"endpoint": "NODE-subscription.delete_id",
"data": {
"name": "test_create_subscription"
}
},
"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;"
}
]
}

View File

@ -95,35 +95,6 @@ class ReverseEngineeredSQLTestCases(BaseTestGenerator):
# Added line break after scenario name
print("")
# Create replication slot if it does not exist for the
# RESQL test-cases of Subscriptions for PGv17 and above
if self.server_information['server_version'] >= 170000:
try:
self.get_db_connection()
pg_cursor = self.connection.cursor()
pg_cursor.execute("""
SELECT 1 FROM pg_replication_slots
WHERE slot_name = 'test_create_subscription'
""")
exists = pg_cursor.fetchone()
if not exists:
pg_cursor.execute("""
SELECT pg_create_logical_replication_slot(
'test_create_subscription',
'pgoutput',
failover := false
);
""")
self.connection.commit()
print("Replication slot "
"'test_create_subscription' created.")
else:
print("Replication slot 'test_create_subscription' "
"already exists.")
pg_cursor.close()
except Exception as e:
print("Could not create replication slot: ", e)
def runTest(self):
""" Create the module list on which reverse engineeredsql test
cases will be executed."""
@ -182,30 +153,6 @@ class ReverseEngineeredSQLTestCases(BaseTestGenerator):
# Check the final status of the test case
self.assertEqual(self.final_test_status, True)
def tearDown(self):
# Drop the replication slot created for the RESQL test-cases of
# Subscriptions, if it exists before disconnecting for PGv17 and above
if self.server_information['server_version'] >= 170000:
try:
self.get_db_connection()
pg_cursor = self.connection.cursor()
pg_cursor.execute("""
SELECT 1 FROM pg_replication_slots
WHERE slot_name = 'test_create_subscription'
""")
exists = pg_cursor.fetchone()
if exists:
pg_cursor.execute("""
SELECT
pg_drop_replication_slot('test_create_subscription');
""")
self.connection.commit()
print("Replication slot "
"'test_create_subscription' dropped.")
pg_cursor.close()
except Exception as e:
print("Could not drop replication slot: ", e)
database_utils.disconnect_database(
self, self.server_information['server_id'],
self.server_information['db_id'])
@ -653,7 +600,10 @@ class ReverseEngineeredSQLTestCases(BaseTestGenerator):
try:
pg_cursor.execute(precondition_sql)
precondition_result = pg_cursor.fetchone()
if len(precondition_result) >= 1 and precondition_result[0] == '1':
if ((len(precondition_result) >= 1 and
precondition_result[0] == '1') or
(isinstance(precondition_result, tuple) and
precondition_result[0] == 1)):
precondition_flag = True
except Exception as e:
traceback.print_exc()