“shubhamdhamal” commited on
Commit
a8e65d3
·
1 Parent(s): e9fbc44

Simplify DB init - ignore 'already exists' if database works

Browse files
Files changed (1) hide show
  1. start.sh +22 -45
start.sh CHANGED
@@ -17,10 +17,9 @@ fi
17
  echo "Initializing database..."
18
  python -c "
19
  from web_app import create_app
20
- from web_app.models import db
21
  from config import Config
22
  import sqlalchemy
23
- from sqlalchemy import text
24
 
25
  print(f'SECRET_KEY set: {bool(Config.SECRET_KEY)}')
26
  print(f'IS_PRODUCTION: {Config.IS_PRODUCTION}')
@@ -30,50 +29,28 @@ print(f'Database URI: {Config.SQLALCHEMY_DATABASE_URI[:50]}...')
30
  app = create_app()
31
  with app.app_context():
32
  try:
33
- # Check if users table exists using inspector
34
- inspector = sqlalchemy.inspect(db.engine)
35
- tables = inspector.get_table_names()
36
-
37
- if 'users' in tables:
38
- from web_app.models import User
39
- user_count = User.query.count()
40
- print(f'✅ Database already initialized ({user_count} users, {len(tables)} tables)')
41
- else:
42
- if tables:
43
- print(f'Incomplete database found: {tables}')
44
- print('Manually dropping all tables and constraints...')
45
- with db.engine.connect() as conn:
46
- # Drop each table with CASCADE
47
- for table in tables:
48
- try:
49
- conn.execute(text(f'DROP TABLE IF EXISTS {table} CASCADE'))
50
- conn.commit()
51
- print(f' Dropped: {table}')
52
- except Exception as e:
53
- print(f' Error dropping {table}: {e}')
54
-
55
- # Drop orphaned constraints if any
56
- result = conn.execute(text(\"\"\"
57
- SELECT constraint_name, table_name
58
- FROM information_schema.table_constraints
59
- WHERE constraint_schema = 'public'
60
- \"\"\"))
61
- constraints = result.fetchall()
62
- for constraint_name, table_name in constraints:
63
- try:
64
- conn.execute(text(f'ALTER TABLE {table_name} DROP CONSTRAINT IF EXISTS {constraint_name} CASCADE'))
65
- conn.commit()
66
- except:
67
- pass
68
- print('✅ Cleanup complete')
69
- else:
70
- print('Empty database - creating schema...')
71
-
72
  db.create_all()
73
- print('✅ Database schema created successfully!')
74
- except Exception as e:
75
- print(f'Database error: {str(e)[:200]}')
76
- raise
 
 
 
 
 
 
 
 
 
 
77
  "
78
 
79
  echo "Starting gunicorn server..."
 
17
  echo "Initializing database..."
18
  python -c "
19
  from web_app import create_app
20
+ from web_app.models import db, User
21
  from config import Config
22
  import sqlalchemy
 
23
 
24
  print(f'SECRET_KEY set: {bool(Config.SECRET_KEY)}')
25
  print(f'IS_PRODUCTION: {Config.IS_PRODUCTION}')
 
29
  app = create_app()
30
  with app.app_context():
31
  try:
32
+ # Try to query users table - if this works, DB is fine
33
+ user_count = User.query.count()
34
+ print(f'✅ Database working ({user_count} users)')
35
+ except Exception as check_error:
36
+ print(f'Database needs setup: {str(check_error)[:100]}')
37
+ try:
38
+ # Try to create tables
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  db.create_all()
40
+ print('✅ Database schema created!')
41
+ except Exception as create_error:
42
+ error_msg = str(create_error)
43
+ # If error is just 'already exists', check if database actually works
44
+ if 'already exists' in error_msg.lower():
45
+ print('⚠️ Create returned already exists error, testing database...')
46
+ try:
47
+ user_count = User.query.count()
48
+ print(f'✅ Database is functional ({user_count} users) - ignoring duplicate error')
49
+ except Exception as test_error:
50
+ print(f'❌ Database not functional: {test_error}')
51
+ raise
52
+ else:
53
+ raise
54
  "
55
 
56
  echo "Starting gunicorn server..."