diff --git a/navi/auth/_ddl.py b/navi/auth/_ddl.py index 35a1a29..8850e38 100644 --- a/navi/auth/_ddl.py +++ b/navi/auth/_ddl.py @@ -34,13 +34,38 @@ CREATE INDEX IF NOT EXISTS idx_user_auth_sessions_user_id ON user_auth_sessions (user_id); """ +# Columns added after initial table creation — boot-time migration. +_NAVI_USERS_MIGRATION_COLUMNS = [ + ("username", "TEXT"), + ("first_name", "TEXT"), + ("last_name", "TEXT"), + ("phone", "TEXT"), + ("birth_date", "TEXT"), + ("country", "TEXT"), + ("city", "TEXT"), + ("locale", "TEXT"), +] + async def _ensure_auth_tables() -> None: - """Create auth tables if they don't exist.""" + """Create auth tables if they don't exist and run boot-time migrations.""" from navi.config import settings conn = await asyncpg.connect(settings.database_url) try: await conn.execute(_DDL) + # Migrate existing navi_users tables that lack newer columns + for col_name, col_type in _NAVI_USERS_MIGRATION_COLUMNS: + row = await conn.fetchrow( + """ + SELECT 1 FROM information_schema.columns + WHERE table_name = 'navi_users' AND column_name = $1 + """, + col_name, + ) + if row is None: + await conn.execute( + f'ALTER TABLE navi_users ADD COLUMN {col_name} {col_type}' + ) finally: await conn.close()