“shubhamdhamal” commited on
Commit
b217afb
·
1 Parent(s): ad936e8

Nuclear option - DROP SCHEMA CASCADE to completely reset database

Browse files
Files changed (1) hide show
  1. start.sh +17 -28
start.sh CHANGED
@@ -34,40 +34,29 @@ with app.app_context():
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
- db.session.rollback()
38
 
39
- # Clean up orphaned constraints before creating tables
40
  try:
41
- print('Checking for orphaned constraints...')
42
  with db.engine.connect() as conn:
43
- # Find all constraints
44
- result = conn.execute(text(\"\"\"
45
- SELECT constraint_name, table_name
46
- FROM information_schema.table_constraints
47
- WHERE constraint_schema = 'public'
48
- AND constraint_type = 'UNIQUE'
49
- \"\"\"))
50
- constraints = result.fetchall()
51
 
52
- for constraint_name, table_name in constraints:
53
- print(f' Found constraint: {constraint_name} on {table_name}')
54
- try:
55
- # Try to drop the constraint
56
- conn.execute(text(f'ALTER TABLE IF EXISTS {table_name} DROP CONSTRAINT IF EXISTS {constraint_name}'))
57
- conn.commit()
58
- print(f' Dropped constraint: {constraint_name}')
59
- except Exception as drop_error:
60
- print(f' Could not drop {constraint_name}: {drop_error}')
61
- conn.rollback()
62
- except Exception as cleanup_error:
63
- print(f'Constraint cleanup failed: {cleanup_error}')
64
-
65
- try:
66
- # Try to create tables
67
  db.create_all()
68
- print('✅ Database schema created!')
 
 
 
 
 
69
  except Exception as create_error:
70
- print(f'❌ Create failed: {create_error}')
71
  raise
72
  "
73
 
 
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
 
38
+ # Nuclear option: drop and recreate entire public schema
39
  try:
40
+ print('Performing complete database reset...')
41
  with db.engine.connect() as conn:
42
+ conn.execute(text('DROP SCHEMA public CASCADE'))
43
+ conn.commit()
44
+ print(' Schema dropped')
 
 
 
 
 
45
 
46
+ conn.execute(text('CREATE SCHEMA public'))
47
+ conn.commit()
48
+ print(' Schema recreated')
49
+
50
+ # Now create all tables
 
 
 
 
 
 
 
 
 
 
51
  db.create_all()
52
+ print('✅ Database schema created successfully!')
53
+
54
+ # Verify it works
55
+ user_count = User.query.count()
56
+ print(f'✅ Verified: Database has {user_count} users')
57
+
58
  except Exception as create_error:
59
+ print(f'❌ Database reset failed: {create_error}')
60
  raise
61
  "
62