"""Run inside API container: migrations, constraints, rollback and password storage.""" import sys import uuid sys.path.insert(0, '/app/src') import psycopg from db import connect, migrate def counts(): with connect() as db: return {name: db.execute('SELECT count(*) AS n FROM ' + name).fetchone()['n'] for name in ('users', 'chats', 'chat_members', 'messages', 'sessions', 'message_keys', 'attachments', 'schema_migrations')} before = counts() migrate() migrate() assert counts() == before with connect() as db: assert db.execute('SELECT count(*) AS n FROM users').fetchone()['n'] > 0 assert db.execute('SELECT 1 FROM users WHERE octet_length(password_salt) != 16 OR octet_length(password_hash) != 64').fetchone() is None assert db.execute('SELECT 1 FROM sessions WHERE octet_length(token_hash) != 32').fetchone() is None chat_id = uuid.uuid4() try: with connect() as db: db.execute('INSERT INTO chats (id,title) VALUES (%s,%s)', (chat_id, 'rollback check')) db.execute('INSERT INTO chat_members VALUES (%s,%s)', (chat_id, uuid.uuid4())) except psycopg.errors.ForeignKeyViolation: pass else: raise AssertionError('Foreign key did not reject missing member') with connect() as db: assert db.execute('SELECT 1 FROM chats WHERE id=%s', (chat_id,)).fetchone() is None assert counts() == before print('PASS: repeated migrations preserve row counts; scrypt/token digests; FK and transaction rollback')