Files
2026-10-09 16:11:06 +03:00

39 lines
1.8 KiB
Python

import unittest
from uuid import uuid4
from sqlalchemy import create_engine, inspect, select
from sqlalchemy.schema import CreateSchema, DropSchema
from scripts.cache_experiment import NoCache, make_store
from src.database import create_db_engine, migrate
from src.models import SchemaMigration
class MigrationTests(unittest.TestCase):
def test_migrate_empty_schema_and_preserve_orm_data_on_repeat(self):
engine = create_db_engine()
schema = 'orm_test_' + uuid4().hex
isolated = create_engine(engine.url, connect_args={
'connect_timeout': 2, 'options': f'-c search_path={schema} -c statement_timeout=3000'})
try:
with engine.begin() as conn:
conn.execute(CreateSchema(schema))
migrate(isolated)
store = make_store(isolated, NoCache())
user = store.create_user('migration_user', 'Migration', 'Synthetic-password-123')
chat = store.create_chat(user['id'], 'Migration', [user['id']])
message = store.create_message(chat['id'], user['id'], 'must survive migration repeat')
migrate(isolated)
self.assertEqual(store.list_messages(chat['id'], user['id'], 100, None)['items'], [message])
with isolated.connect() as conn:
self.assertEqual(set(conn.scalars(select(SchemaMigration.version))),
{'001_initial.sql', '002_history_version.sql'})
indexes = {index['name'] for index in inspect(isolated).get_indexes('messages')}
self.assertIn('messages_history_idx', indexes)
self.assertEqual(len(inspect(isolated).get_foreign_keys('messages')), 2)
finally:
isolated.dispose()
with engine.begin() as conn:
conn.execute(DropSchema(schema, cascade=True, if_exists=True))
engine.dispose()