Skip to content

Commit e7b58ca

Browse files
committed
chore: refactoring pg sql queries to be fully qualified
1 parent 21f7119 commit e7b58ca

File tree

8 files changed

+31
-30
lines changed

8 files changed

+31
-30
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ with AwsWrapperConnection.connect(
4848
autocommit=True
4949
) as awsconn:
5050
awscursor = awsconn.cursor()
51-
awscursor.execute("SELECT aurora_db_instance_identifier()")
51+
awscursor.execute("SELECT pg_catalog.aurora_db_instance_identifier()")
5252
row = awscursor.fetchone()
5353
print(row)
5454
```

aws_advanced_python_wrapper/database_dialect.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def default_port(self) -> int:
177177

178178
@property
179179
def host_alias_query(self) -> str:
180-
return "SELECT CONCAT(@@hostname, ':', @@port)"
180+
return "SELECT pg_catalog.CONCAT(@@hostname, ':', @@port)"
181181

182182
@property
183183
def server_version_query(self) -> str:
@@ -228,11 +228,11 @@ def default_port(self) -> int:
228228

229229
@property
230230
def host_alias_query(self) -> str:
231-
return "SELECT CONCAT(inet_server_addr(), ':', inet_server_port())"
231+
return "SELECT pg_catalog.CONCAT(pg_catalog.inet_server_addr(), ':', pg_catalog.inet_server_port())"
232232

233233
@property
234234
def server_version_query(self) -> str:
235-
return "SELECT 'version', VERSION()"
235+
return "SELECT 'version', pg_catalog.VERSION()"
236236

237237
@property
238238
def dialect_update_candidates(self) -> Optional[Tuple[DialectCode, ...]]:
@@ -249,7 +249,7 @@ def is_dialect(self, conn: Connection, driver_dialect: DriverDialect) -> bool:
249249
initial_transaction_status: bool = driver_dialect.is_in_transaction(conn)
250250
try:
251251
with closing(conn.cursor()) as cursor:
252-
cursor.execute('SELECT 1 FROM pg_proc LIMIT 1')
252+
cursor.execute('SELECT 1 FROM pg_catalog.pg_proc LIMIT 1')
253253
if cursor.fetchone() is not None:
254254
return True
255255
except Exception:
@@ -329,8 +329,8 @@ def is_blue_green_status_available(self, conn: Connection) -> bool:
329329
class RdsPgDialect(PgDatabaseDialect, BlueGreenDialect):
330330
_EXTENSIONS_QUERY = ("SELECT (setting LIKE '%rds_tools%') AS rds_tools, "
331331
"(setting LIKE '%aurora_stat_utils%') AS aurora_stat_utils "
332-
"FROM pg_settings "
333-
"WHERE name='rds.extensions'")
332+
"FROM pg_catalog.pg_settings "
333+
"WHERE name OPERATOR(pg_catalog.=) 'rds.extensions'")
334334
_DIALECT_UPDATE_CANDIDATES = (DialectCode.AURORA_PG, DialectCode.MULTI_AZ_CLUSTER_PG)
335335

336336
_BG_STATUS_QUERY = (f"SELECT version, endpoint, port, role, status "
@@ -427,24 +427,25 @@ class AuroraPgDialect(PgDatabaseDialect, TopologyAwareDatabaseDialect, AuroraLim
427427
_DIALECT_UPDATE_CANDIDATES: Tuple[DialectCode, ...] = (DialectCode.MULTI_AZ_CLUSTER_PG,)
428428

429429
_EXTENSIONS_QUERY = "SELECT (setting LIKE '%aurora_stat_utils%') AS aurora_stat_utils " \
430-
"FROM pg_settings WHERE name='rds.extensions'"
430+
"FROM pg_catalog.pg_settings WHERE name OPERATOR(pg_catalog.=) 'rds.extensions'"
431431

432-
_HAS_TOPOLOGY_QUERY = "SELECT 1 FROM aurora_replica_status() LIMIT 1"
432+
_HAS_TOPOLOGY_QUERY = "SELECT 1 FROM pg_catalog.aurora_replica_status() LIMIT 1"
433433

434434
_TOPOLOGY_QUERY = \
435-
("SELECT SERVER_ID, CASE WHEN SESSION_ID = 'MASTER_SESSION_ID' THEN TRUE ELSE FALSE END, "
435+
("SELECT SERVER_ID, CASE WHEN SESSION_ID OPERATOR(pg_catalog.=) 'MASTER_SESSION_ID' THEN TRUE ELSE FALSE END, "
436436
"CPU, COALESCE(REPLICA_LAG_IN_MSEC, 0), LAST_UPDATE_TIMESTAMP "
437-
"FROM aurora_replica_status() "
438-
"WHERE EXTRACT(EPOCH FROM(NOW() - LAST_UPDATE_TIMESTAMP)) <= 300 OR SESSION_ID = 'MASTER_SESSION_ID' "
437+
"FROM pg_catalog.aurora_replica_status() "
438+
"WHERE EXTRACT(EPOCH FROM(pg_catalog.NOW() OPERATOR(pg_catalog.-) LAST_UPDATE_TIMESTAMP)) <= 300 "
439+
"OR SESSION_ID OPERATOR(pg_catalog.=) 'MASTER_SESSION_ID' "
439440
"OR LAST_UPDATE_TIMESTAMP IS NULL")
440441

441-
_HOST_ID_QUERY = "SELECT aurora_db_instance_identifier()"
442-
_IS_READER_QUERY = "SELECT pg_is_in_recovery()"
443-
_LIMITLESS_ROUTER_ENDPOINT_QUERY = "SELECT router_endpoint, load FROM aurora_limitless_router_endpoints()"
442+
_HOST_ID_QUERY = "SELECT pg_catalog.aurora_db_instance_identifier()"
443+
_IS_READER_QUERY = "SELECT pg_catalog.pg_is_in_recovery()"
444+
_LIMITLESS_ROUTER_ENDPOINT_QUERY = "SELECT router_endpoint, load FROM pg_catalog.aurora_limitless_router_endpoints()"
444445

445446
_BG_STATUS_QUERY = (f"SELECT version, endpoint, port, role, status "
446-
f"FROM get_blue_green_fast_switchover_metadata('aws_advanced_python_wrapper-{DriverInfo.DRIVER_VERSION}')")
447-
_BG_STATUS_EXISTS_QUERY = "SELECT 'get_blue_green_fast_switchover_metadata'::regproc"
447+
f"FROM pg_catalog.get_blue_green_fast_switchover_metadata('aws_advanced_python_wrapper-{DriverInfo.DRIVER_VERSION}')")
448+
_BG_STATUS_EXISTS_QUERY = "SELECT 'pg_catalog.get_blue_green_fast_switchover_metadata'::regproc"
448449

449450
@property
450451
def dialect_update_candidates(self) -> Optional[Tuple[DialectCode, ...]]:
@@ -560,7 +561,7 @@ class MultiAzClusterPgDialect(PgDatabaseDialect, TopologyAwareDatabaseDialect):
560561
_WRITER_HOST_QUERY = \
561562
"SELECT multi_az_db_cluster_source_dbi_resource_id FROM rds_tools.multi_az_db_cluster_source_dbi_resource_id()"
562563
_HOST_ID_QUERY = "SELECT dbi_resource_id FROM rds_tools.dbi_resource_id()"
563-
_IS_READER_QUERY = "SELECT pg_is_in_recovery()"
564+
_IS_READER_QUERY = "SELECT pg_catalog.pg_is_in_recovery()"
564565
_exception_handler: Optional[ExceptionHandler] = None
565566

566567
@property

docs/examples/PGLimitless.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
plugins="limitless",
2727
autocommit=True
2828
) as awsconn, awsconn.cursor() as awscursor:
29-
awscursor.execute("SELECT * FROM aurora_db_instance_identifier()")
29+
awscursor.execute("SELECT * FROM pg_catalog.aurora_db_instance_identifier()")
3030

3131
res = awscursor.fetchone()
3232
print(res)

docs/examples/PGOktaAuthentication.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
db_user="john",
3333
autocommit=True
3434
) as awsconn, awsconn.cursor() as awscursor:
35-
awscursor.execute("SELECT * FROM aurora_db_instance_identifier()")
35+
awscursor.execute("SELECT * FROM pg_catalog.aurora_db_instance_identifier()")
3636

3737
res = awscursor.fetchone()
3838
print(res)

docs/examples/PGSecretsManager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
secrets_manager_region="us-east-2",
2828
plugins="aws_secrets_manager"
2929
) as awsconn, awsconn.cursor() as cursor:
30-
cursor.execute("SELECT aurora_db_instance_identifier()")
30+
cursor.execute("SELECT pg_catalog.aurora_db_instance_identifier()")
3131
for record in cursor.fetchone():
3232
print(record)

tests/integration/container/test_blue_green_deployment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class TestBlueGreenDeployment:
8484
"FROM mysql.rds_topology")
8585
PG_AURORA_BG_STATUS_QUERY = \
8686
("SELECT id, SPLIT_PART(endpoint, '.', 1) as hostId, endpoint, port, role, status, version "
87-
"FROM get_blue_green_fast_switchover_metadata('aws_jdbc_driver')")
87+
"FROM pg_catalog.get_blue_green_fast_switchover_metadata('aws_jdbc_driver')")
8888
PG_RDS_BG_STATUS_QUERY = \
8989
(f"SELECT id, SPLIT_PART(endpoint, '.', 1) as hostId, endpoint, port, role, status, version "
9090
f"FROM rds_tools.show_topology('aws_jdbc_driver-{DriverInfo.DRIVER_VERSION}')")
@@ -686,7 +686,7 @@ def wrapper_blue_executing_connectivity_monitor(
686686
if engine == DatabaseEngine.MYSQL:
687687
query = "SELECT sleep(5)"
688688
elif engine == DatabaseEngine.PG:
689-
query = "SELECT pg_sleep(5)"
689+
query = "SELECT pg_catalog.pg_sleep(5)"
690690
else:
691691
pytest.fail(f"Unsupported database engine: {engine}")
692692

tests/integration/container/test_failover_performance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def _measure_performance(
206206
sleep_delay_sec: int,
207207
props: Properties,
208208
data: PerfStatMonitoring):
209-
query: str = "SELECT pg_sleep(600)"
209+
query: str = "SELECT pg_catalog.pg_sleep(600)"
210210
downtime: AtomicInt = AtomicInt()
211211
elapsed_times: List[int] = []
212212

tests/integration/container/utils/rds_test_utility.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def _query_aurora_instance_id(self, conn: Connection, engine: DatabaseEngine) ->
256256
if engine == DatabaseEngine.MYSQL:
257257
sql = "SELECT @@aurora_server_id"
258258
elif engine == DatabaseEngine.PG:
259-
sql = "SELECT aurora_db_instance_identifier()"
259+
sql = "SELECT pg_catalog.aurora_db_instance_identifier()"
260260
else:
261261
raise UnsupportedOperationError(engine.value)
262262

@@ -392,18 +392,18 @@ def _get_aurora_topology_sql(self, engine: DatabaseEngine) -> str:
392392
return ("SELECT SERVER_ID, SESSION_ID FROM information_schema.replica_host_status "
393393
"ORDER BY IF(SESSION_ID = 'MASTER_SESSION_ID', 0, 1)")
394394
elif engine == DatabaseEngine.PG:
395-
return ("SELECT SERVER_ID, SESSION_ID FROM aurora_replica_status() "
396-
"ORDER BY CASE WHEN SESSION_ID = 'MASTER_SESSION_ID' THEN 0 ELSE 1 END")
395+
return ("SELECT SERVER_ID, SESSION_ID FROM pg_catalog.aurora_replica_status() "
396+
"ORDER BY CASE WHEN SESSION_ID OPERATOR(pg_catalog.=) 'MASTER_SESSION_ID' THEN 0 ELSE 1 END")
397397
else:
398398
raise UnsupportedOperationError(engine.value)
399399

400400
def _get_multi_az_topology_sql(self, engine: DatabaseEngine, writer_id) -> str:
401401
if engine == DatabaseEngine.MYSQL:
402-
return f"SELECT id, endpoint, port FROM mysql.rds_topology ORDER BY id='{writer_id}' DESC"
402+
return f"SELECT id, endpoint, port FROM mysql.rds_topology ORDER BY id = '{writer_id}' DESC"
403403
elif engine == DatabaseEngine.PG:
404404
return (f"SELECT id, endpoint, port "
405405
f"FROM rds_tools.show_topology('aws_python_driver-{DriverInfo.DRIVER_VERSION}')"
406-
f"ORDER BY id='{writer_id}' DESC")
406+
f"ORDER BY id OPERATOR(pg_catalog.=) '{writer_id}' DESC")
407407
else:
408408
raise UnsupportedOperationError(engine.value)
409409

@@ -536,7 +536,7 @@ def temporary_failure():
536536
def get_sleep_sql(self, seconds: float) -> str:
537537
engine = TestEnvironment.get_current().get_engine()
538538
if engine == DatabaseEngine.PG:
539-
return f"SELECT pg_sleep({seconds})"
539+
return f"SELECT pg_catalog.pg_sleep({seconds})"
540540
elif engine == DatabaseEngine.MYSQL:
541541
return f"SELECT SLEEP({seconds})"
542542
else:

0 commit comments

Comments
 (0)