-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset_database.py
More file actions
executable file
·78 lines (62 loc) · 2.85 KB
/
Copy pathreset_database.py
File metadata and controls
executable file
·78 lines (62 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
# filepath: /home/god/cits5505-masters-group37/reset_database.py
"""
Database Reset Tool
************************************************************************************
** WARNING: DEVELOPMENT USE ONLY - DESTRUCTIVE OPERATION **
This script completely resets the database by dropping all tables and recreating them
based on the current models (db.drop_all(), db.create_all()).
IT BYPASSES THE FLASK-MIGRATE/ALEMBIC MIGRATION HISTORY.
USE FLASK-MIGRATE (flask db upgrade/downgrade) FOR SCHEMA EVOLUTION.
This script is for quickly resetting to a clean slate during development if needed,
but be aware that it will WIPE ALL DATA and your database will NOT be in sync
with migration history afterwards. You might need to re-stamp or re-initialize
migrations if you use this and then want to use Flask-Migrate.
************************************************************************************
"""
from app import create_app, db
def reset_database():
"""
Reset the entire database by dropping all tables and recreating them.
This is more drastic than just clearing data, as it will reset any schema
changes or migrations that have been applied.
"""
print("WARNING: This will RESET the ENTIRE database and ALL DATA WILL BE LOST!")
print("This operation is NOT REVERSIBLE!")
confirm = input("Are you sure you want to continue? Type 'RESET' to confirm: ")
if confirm != 'RESET':
print("Operation cancelled.")
return False
app = create_app()
with app.app_context():
try:
# Drop all tables
print("\nDropping all database tables...")
db.drop_all()
print("✓ All tables dropped")
# Recreate all tables
print("\nRecreating database tables...")
db.create_all()
print("✓ All tables recreated")
print(f"\nDatabase has been completely reset: {app.config['SQLALCHEMY_DATABASE_URI']}")
return True
except Exception as e:
print(f"Error resetting database: {e}")
return False
if __name__ == "__main__":
print("\nDatabase Reset Tool")
print("=================\n")
print("This tool will COMPLETELY RESET the database by:")
print("1. Dropping all tables")
print("2. Recreating all tables with empty data")
print("\nUnlike clear_database.py which only removes data but keeps tables,")
print("this will reset the entire database structure.")
choice = input("\nDo you want to continue? [y/N]: ")
if choice.lower() == 'y':
success = reset_database()
if success:
print("\n✓ Database successfully reset!")
else:
print("\n✗ Database reset failed or was cancelled.")
else:
print("Operation cancelled.")