Harshit Ghosh commited on
Commit
04c7cc4
Β·
1 Parent(s): 8e35842

Fix 500: add missing avatar columns via startup migration

Browse files
Files changed (1) hide show
  1. app_new.py +17 -2
app_new.py CHANGED
@@ -227,10 +227,25 @@ logger = logging.getLogger("ich_app")
227
  # ══════════════════════════════════════════════════════════════════════════
228
 
229
  def init_db():
230
- """Initialize database tables"""
231
  with app.app_context():
232
  db.create_all()
233
- logger.info("Database initialized")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
 
235
  # ══════════════════════════════════════════════════════════════════════════
236
  # MODEL & INFERENCE STATE
 
227
  # ══════════════════════════════════════════════════════════════════════════
228
 
229
  def init_db():
230
+ """Initialize database tables and run lightweight column migrations."""
231
  with app.app_context():
232
  db.create_all()
233
+ # Safe column additions for existing deployments.
234
+ # IF NOT EXISTS is supported by PostgreSQL 9.6+ and is a no-op if the column already exists.
235
+ migrations = [
236
+ "ALTER TABLE users ADD COLUMN IF NOT EXISTS avatar_url VARCHAR(500)",
237
+ "ALTER TABLE users ADD COLUMN IF NOT EXISTS avatar_public_id VARCHAR(255)",
238
+ # pending_otps is created by create_all() on first deploy; no ALTER needed.
239
+ ]
240
+ with db.engine.connect() as conn:
241
+ for sql in migrations:
242
+ try:
243
+ conn.execute(db.text(sql))
244
+ except Exception as exc:
245
+ logger.warning("Migration skipped (%s): %s", sql, exc)
246
+ conn.commit()
247
+ logger.info("Database initialized and migrations applied")
248
+
249
 
250
  # ══════════════════════════════════════════════════════════════════════════
251
  # MODEL & INFERENCE STATE