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

Manually drop tables and constraints individually before recreate

Browse files
Files changed (1) hide show
  1. start.sh +27 -19
start.sh CHANGED
@@ -40,32 +40,40 @@ with app.app_context():
40
  print(f'✅ Database already initialized ({user_count} users, {len(tables)} tables)')
41
  else:
42
  if tables:
43
- print(f'Incomplete database found with tables: {tables}')
44
- print('Performing clean drop with CASCADE...')
45
- # Drop all tables with CASCADE to remove constraints
46
  with db.engine.connect() as conn:
47
- conn.execute(text('DROP SCHEMA public CASCADE'))
48
- conn.execute(text('CREATE SCHEMA public'))
49
- conn.commit()
50
- print('Schema cleaned successfully')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  else:
52
  print('Empty database - creating schema...')
53
 
54
  db.create_all()
55
  print('✅ Database schema created successfully!')
56
  except Exception as e:
57
- print(f'Database error: {str(e)[:150]}')
58
- try:
59
- print('Attempting CASCADE cleanup and recreate...')
60
- with db.engine.connect() as conn:
61
- conn.execute(text('DROP SCHEMA public CASCADE'))
62
- conn.execute(text('CREATE SCHEMA public'))
63
- conn.commit()
64
- db.create_all()
65
- print('✅ Database recreated successfully!')
66
- except Exception as recreate_error:
67
- print(f'⚠️ Could not recreate database: {recreate_error}')
68
- raise
69
  "
70
 
71
  echo "Starting gunicorn server..."
 
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..."