-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_db.py
More file actions
executable file
·80 lines (67 loc) · 2.43 KB
/
Copy pathinit_db.py
File metadata and controls
executable file
·80 lines (67 loc) · 2.43 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
79
80
#!/usr/bin/env python3
"""
数据库初始化脚本
Database Initialization Script
"""
import sys
import os
# 添加项目路径
sys.path.insert(0, os.path.dirname(__file__))
from app import create_app, db
def init_database():
"""初始化数据库"""
app, socketio = create_app('development')
with app.app_context():
print("🔄 正在初始化数据库...")
# 创建所有表
db.create_all()
print("✅ 表结构已创建")
# 导入模型以触发初始化
from models import Role, AlarmType, AlarmLevel, User
# 初始化角色
Role.init_roles()
print("✅ 角色已初始化 (admin, manager, operator, maintenance)")
# 初始化报警类型
AlarmType.init_types()
print("✅ 报警类型已初始化 (photo, smoke, crowd, walk)")
# 初始化报警级别
AlarmLevel.init_levels()
print("✅ 报警级别已初始化 (critical, high, medium, low)")
# 创建默认管理员
admin_role = Role.query.filter_by(name='admin').first()
if admin_role:
admin = User.query.filter_by(username='admin').first()
if not admin:
admin = User(
username='admin',
real_name='系统管理员',
email='admin@cinema.com',
phone='13800138000',
role_id=admin_role.id,
status=1
)
admin.set_password('admin123')
db.session.add(admin)
db.session.commit()
print("✅ 默认管理员已创建 (admin/admin123)")
else:
print("⚠️ 管理员已存在")
print("\n" + "="*40)
print("✨ 数据库初始化完成!")
print("="*40)
print("\n访问信息:")
print(" 地址: http://localhost:9500")
print(" 账号: admin")
print(" 密码: admin123")
print("\n启动服务:")
print(" python3 app.py")
if __name__ == '__main__':
try:
init_database()
except Exception as e:
print(f"\n❌ 初始化失败: {e}")
print("\n请检查:")
print(" 1. MySQL 是否已启动")
print(" 2. 数据库配置是否正确 (config.py)")
print(" 3. 数据库是否存在 (cinema_detection)")
sys.exit(1)