Skip to content

Commit

Permalink
fix: update of test seeder entity sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Sep 4, 2024
1 parent 32fad78 commit c8801fd
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion backend/lcfs/db/seeders/test_seeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand All @@ -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())

0 comments on commit c8801fd

Please sign in to comment.