Spaces:
Sleeping
Sleeping
“shubhamdhamal” commited on
Commit ·
ad936e8
1
Parent(s): e39db35
Drop orphaned constraints before creating tables
Browse files
start.sh
CHANGED
|
@@ -19,6 +19,7 @@ python -c "
|
|
| 19 |
from web_app import create_app
|
| 20 |
from web_app.models import db, User
|
| 21 |
from config import Config
|
|
|
|
| 22 |
|
| 23 |
print(f'SECRET_KEY set: {bool(Config.SECRET_KEY)}')
|
| 24 |
print(f'IS_PRODUCTION: {Config.IS_PRODUCTION}')
|
|
@@ -33,27 +34,41 @@ with app.app_context():
|
|
| 33 |
print(f'✅ Database working ({user_count} users)')
|
| 34 |
except Exception as check_error:
|
| 35 |
print(f'Database needs setup: {str(check_error)[:100]}')
|
| 36 |
-
db.session.rollback()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
try:
|
| 39 |
# Try to create tables
|
| 40 |
db.create_all()
|
| 41 |
print('✅ Database schema created!')
|
| 42 |
except Exception as create_error:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
# If error is just 'already exists', check if database actually works
|
| 47 |
-
if 'already exists' in error_msg.lower():
|
| 48 |
-
print('⚠️ Create returned already exists error, testing database...')
|
| 49 |
-
try:
|
| 50 |
-
user_count = User.query.count()
|
| 51 |
-
print(f'✅ Database is functional ({user_count} users) - ignoring duplicate error')
|
| 52 |
-
except Exception as test_error:
|
| 53 |
-
print(f'❌ Database not functional: {test_error}')
|
| 54 |
-
raise
|
| 55 |
-
else:
|
| 56 |
-
raise
|
| 57 |
"
|
| 58 |
|
| 59 |
echo "Starting gunicorn server..."
|
|
|
|
| 19 |
from web_app import create_app
|
| 20 |
from web_app.models import db, User
|
| 21 |
from config import Config
|
| 22 |
+
from sqlalchemy import text
|
| 23 |
|
| 24 |
print(f'SECRET_KEY set: {bool(Config.SECRET_KEY)}')
|
| 25 |
print(f'IS_PRODUCTION: {Config.IS_PRODUCTION}')
|
|
|
|
| 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 |
|
| 74 |
echo "Starting gunicorn server..."
|