-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreset_db_user.py
61 lines (49 loc) · 1.81 KB
/
reset_db_user.py
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
from flask import Flask
from extensions import db
from models.auth_model import User # Assuming the User model is in models.py
import logging
def reset_database(app):
"""
Reset the database by dropping all tables and recreating them.
Optionally add initial data if needed.
"""
try:
# Drop all tables
with app.app_context():
logging.info("Dropping all database tables...")
db.drop_all()
# Recreate all tables
logging.info("Creating all database tables...")
db.create_all()
# Optionally add initial admin user
admin_user = User(
username="admin",
fullname="Administrator",
email="admin@example.com",
is_active=True,
role='admin'
)
admin_user.set_password("admin123") # Remember to change this in production
db.session.add(admin_user)
db.session.commit()
logging.info("Database reset completed successfully!")
return True
except Exception as e:
logging.error(f"Error resetting database: {str(e)}")
return False
if __name__ == "__main__":
# Create Flask app instance
app = Flask(__name__)
# Configure database URI
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Initialize database with app
db.init_app(app)
# Setup logging
logging.basicConfig(level=logging.INFO)
# Reset database
success = reset_database(app)
if success:
print("Database reset completed successfully!")
else:
print("Database reset failed. Check the logs for details.")