Skip to content

Commit 31bf2fd

Browse files
Copilotjason810496
andcommitted
Fix detach_archive test cleanup to properly handle detached tables
Co-authored-by: jason810496 <[email protected]>
1 parent 3bcceb0 commit 31bf2fd

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

tests/test_queue.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,12 @@ def test_set_vt_not_exist(pgmq_setup_teardown: PGMQ_WITH_QUEUE):
450450

451451

452452
# Tests for detach_archive method
453-
def test_detach_archive(pgmq_setup_teardown: PGMQ_WITH_QUEUE, db_session):
453+
@pgmq_deps
454+
def test_detach_archive(pgmq_fixture, db_session):
454455
"""Test detach_archive method - detaches archive table from queue."""
455-
pgmq, queue_name = pgmq_setup_teardown
456+
pgmq: PGMQueue = pgmq_fixture
457+
queue_name = f"test_queue_{uuid.uuid4().hex}"
458+
pgmq.create_queue(queue_name)
456459
msg = MSG
457460
msg_id = pgmq.send(queue_name, msg)
458461
pgmq.archive(queue_name, msg_id)
@@ -465,18 +468,27 @@ def test_detach_archive(pgmq_setup_teardown: PGMQ_WITH_QUEUE, db_session):
465468
assert archived_msg is not None
466469
assert archived_msg.msg_id == msg_id
467470

468-
# Cleanup: Manually drop the detached archive table
469-
# The fixture teardown will handle the queue itself
471+
# Cleanup: Drop the archive and queue tables
472+
# After detaching, the archive is no longer part of the extension
473+
# We need to drop both tables manually by first removing them from the extension
470474
if pgmq.is_async:
471475
import asyncio
472476
async def cleanup():
473477
async with pgmq.session_maker() as session:
478+
# Drop archive table (already detached)
474479
await session.execute(text(f"DROP TABLE IF EXISTS pgmq.a_{queue_name} CASCADE;"))
480+
# Detach and drop queue table
481+
await session.execute(text(f"ALTER EXTENSION pgmq DROP TABLE pgmq.q_{queue_name};"))
482+
await session.execute(text(f"DROP TABLE IF EXISTS pgmq.q_{queue_name} CASCADE;"))
475483
await session.commit()
476484
pgmq.loop.run_until_complete(cleanup())
477485
else:
478486
with pgmq.session_maker() as session:
487+
# Drop archive table (already detached)
479488
session.execute(text(f"DROP TABLE IF EXISTS pgmq.a_{queue_name} CASCADE;"))
489+
# Detach and drop queue table
490+
session.execute(text(f"ALTER EXTENSION pgmq DROP TABLE pgmq.q_{queue_name};"))
491+
session.execute(text(f"DROP TABLE IF EXISTS pgmq.q_{queue_name} CASCADE;"))
480492
session.commit()
481493

482494

0 commit comments

Comments
 (0)