From c8801fd20f6d85e7cf204d9327b60682c8aaa86f Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 4 Sep 2024 15:45:27 -0700 Subject: [PATCH] fix: update of test seeder entity sequences --- backend/lcfs/db/seeders/test_seeder.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/backend/lcfs/db/seeders/test_seeder.py b/backend/lcfs/db/seeders/test_seeder.py index 9c8b36bef..4e9ea319f 100644 --- a/backend/lcfs/db/seeders/test_seeder.py +++ b/backend/lcfs/db/seeders/test_seeder.py @@ -2,6 +2,7 @@ import asyncio from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession from sqlalchemy.orm import sessionmaker +from sqlalchemy import text from lcfs.db.seeders.test.test_admin_adjustment_seeder import seed_test_admin_adjustments from lcfs.settings import settings @@ -13,6 +14,24 @@ logger = logging.getLogger(__name__) +async def update_sequences(session: AsyncSession): + """ + Function to update sequences for all tables after seeding. + """ + sequences = { + 'organization': 'organization_id', + 'user_profile': 'user_profile_id', + 'user_role': 'user_role_id', + 'transaction': 'transaction_id', + 'admin_adjustment': 'admin_adjustment_id', + # Add other tables and their primary key columns as needed + } + + for table, column in sequences.items(): + sequence_name = f"{table}_{column}_seq" + max_value_query = text(f"SELECT setval('{sequence_name}', COALESCE((SELECT MAX({column}) + 1 FROM {table}), 1), false)") + await session.execute(max_value_query) + async def seed_test(session: AsyncSession): """ Function to seed the database with test data. @@ -23,7 +42,11 @@ async def seed_test(session: AsyncSession): await seed_test_transactions(session) await seed_test_admin_adjustments(session) # await seed_test_transfers(session) + + # Update sequences after all seeders have run + await update_sequences(session) + logger.info("Test database seeding completed successfully.") if __name__ == "__main__": - asyncio.run(seed_test()) + asyncio.run(seed_test()) \ No newline at end of file