Skip to content

Commit ee6c416

Browse files
committed
perf: 优化magic-dash-pro模板初始化命令功能逻辑
1 parent 4464a40 commit ee6c416

File tree

1 file changed

+106
-50
lines changed
  • magic_dash/templates/magic-dash-pro/models

1 file changed

+106
-50
lines changed

magic_dash/templates/magic-dash-pro/models/init_db.py

Lines changed: 106 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ def check_rsa_keys_exist():
6363
)
6464

6565

66+
def check_table_exists(table_model):
67+
"""检查数据表是否已存在"""
68+
69+
return table_model.table_exists()
70+
71+
72+
def check_table_has_data(table_model):
73+
"""检查数据表是否已有数据"""
74+
75+
with db.connection_context():
76+
return table_model.select().count() > 0
77+
78+
6679
# 统一的questionary样式
6780
custom_style = questionary.Style(
6881
[
@@ -90,11 +103,10 @@ def check_rsa_keys_exist():
90103
admin_created = False
91104

92105
# 0. RSA密钥对生成
93-
console.print("\n[dim]请确认以下安全密钥操作:[/dim]\n")
94-
95106
# 检查是否已存在RSA密钥对
96107
if check_rsa_keys_exist():
97108
# 已存在密钥,询问是否覆盖
109+
console.print("\n[dim]请确认以下安全密钥操作:[/dim]\n")
98110
confirm_override = questionary.confirm(
99111
f"检测到已存在RSA密钥对文件({BaseConfig.rsa_private_key_path}{BaseConfig.rsa_public_key_path}),是否重新生成并覆盖?",
100112
default=False,
@@ -126,60 +138,101 @@ def check_rsa_keys_exist():
126138
)
127139
)
128140
else:
129-
# 不存在密钥,询问是否生成
130-
confirm_generate = questionary.confirm(
131-
"当前项目根目录中不存在RSA密钥对文件,是否生成新的RSA密钥对?",
132-
default=True,
141+
# 不存在密钥,直接生成
142+
console.print(
143+
"\n[dim]检测到当前项目根目录中不存在RSA密钥对文件,正在自动生成...[/dim]"
144+
)
145+
try:
146+
generate_rsa_key_pair()
147+
executed_operations.append(
148+
(
149+
"RSA密钥对",
150+
f"已自动生成\n 私钥: {BaseConfig.rsa_private_key_path}\n 公钥: {BaseConfig.rsa_public_key_path}",
151+
"yellow",
152+
"green",
153+
)
154+
)
155+
except Exception as e:
156+
executed_operations.append(
157+
("RSA密钥对", f"生成失败: {str(e)}", "yellow", "red")
158+
)
159+
160+
# 1&2. 预先检查部门和用户表状态
161+
departments_exists = check_table_exists(Departments)
162+
departments_has_data = departments_exists and check_table_has_data(Departments)
163+
users_exists = check_table_exists(Users)
164+
users_has_data = users_exists and check_table_has_data(Users)
165+
166+
# 判断是否需要用户交互(表存在且有数据时需要询问)
167+
need_user_confirm = (departments_exists and departments_has_data) or (
168+
users_exists and users_has_data
169+
)
170+
171+
if need_user_confirm:
172+
console.print("\n[dim]请确认以下数据库操作:[/dim]\n")
173+
174+
# 处理部门信息表
175+
if departments_exists and departments_has_data:
176+
# 表存在且有数据,询问是否重置
177+
confirm_departments = questionary.confirm(
178+
"检测到部门信息表已存在且有数据,是否重置?",
179+
default=False,
133180
style=custom_style,
134181
).ask()
135182

136-
if confirm_generate:
183+
if confirm_departments:
137184
try:
138-
generate_rsa_key_pair()
139-
executed_operations.append(
140-
(
141-
"RSA密钥对",
142-
f"已生成\n 私钥: {BaseConfig.rsa_private_key_path}\n 公钥: {BaseConfig.rsa_public_key_path}",
143-
"yellow",
144-
"green",
145-
)
146-
)
185+
Departments.truncate_departments(execute=True)
186+
executed_operations.append(("部门信息表", "已重置", "yellow", "green"))
147187
except Exception as e:
148188
executed_operations.append(
149-
("RSA密钥对", f"生成失败: {str(e)}", "yellow", "red")
189+
("部门信息表", f"重置失败: {str(e)}", "yellow", "red")
150190
)
151191
else:
152-
executed_operations.append(("RSA密钥对", "已跳过生成", "dim", "dim"))
153-
154-
# 1. 询问是否重置部门表
155-
console.print("\n[dim]请确认以下数据库操作:[/dim]\n")
156-
confirm_departments = questionary.confirm(
157-
"是否重置部门信息表?",
158-
default=False,
159-
style=custom_style,
160-
).ask()
161-
162-
if confirm_departments:
163-
try:
164-
Departments.truncate_departments(execute=True)
165-
executed_operations.append(("部门信息表", "已重置", "yellow", "green"))
166-
except Exception as e:
167-
executed_operations.append(
168-
("部门信息表", f"重置失败: {str(e)}", "yellow", "red")
169-
)
192+
executed_operations.append(("部门信息表", "已跳过", "dim", "dim"))
170193
else:
171-
executed_operations.append(("部门信息表", "已跳过", "dim", "dim"))
194+
# 表不存在或没有数据,跳过(表已在顶部创建)
195+
if not departments_exists:
196+
executed_operations.append(("部门信息表", "已自动创建", "yellow", "green"))
197+
else:
198+
executed_operations.append(("部门信息表", "表已存在但无数据", "dim", "dim"))
172199

173-
# 2. 询问是否重置用户表
174-
confirm_users = questionary.confirm(
175-
"是否重置用户信息表?",
176-
default=False,
177-
style=custom_style,
178-
).ask()
200+
# 处理用户信息表
201+
if users_exists and users_has_data:
202+
# 表存在且有数据,询问是否重置
203+
confirm_users = questionary.confirm(
204+
"检测到用户信息表已存在且有数据,是否重置并初始化管理员账号?",
205+
default=False,
206+
style=custom_style,
207+
).ask()
208+
209+
if confirm_users:
210+
try:
211+
Users.truncate_users(execute=True)
212+
213+
# 初始化管理员用户
214+
Users.add_user(
215+
user_id="admin",
216+
user_name="admin",
217+
password_hash=generate_password_hash("admin123"),
218+
user_role=AuthConfig.admin_role,
219+
)
179220

180-
if confirm_users:
221+
admin_created = True
222+
executed_operations.append(
223+
("用户信息表", "已重置 + 管理员初始化", "yellow", "green")
224+
)
225+
except Exception as e:
226+
executed_operations.append(
227+
("用户信息表", f"重置失败: {str(e)}", "yellow", "red")
228+
)
229+
else:
230+
executed_operations.append(("用户信息表", "已跳过", "dim", "dim"))
231+
else:
232+
# 表不存在或没有数据,自动初始化
181233
try:
182-
Users.truncate_users(execute=True)
234+
if users_exists:
235+
Users.truncate_users(execute=True)
183236

184237
# 初始化管理员用户
185238
Users.add_user(
@@ -190,15 +243,18 @@ def check_rsa_keys_exist():
190243
)
191244

192245
admin_created = True
193-
executed_operations.append(
194-
("用户信息表", "已重置 + 管理员初始化", "yellow", "green")
195-
)
246+
if not users_exists:
247+
executed_operations.append(
248+
("用户信息表", "已自动创建 + 管理员初始化", "yellow", "green")
249+
)
250+
else:
251+
executed_operations.append(
252+
("用户信息表", "已自动初始化管理员账号", "yellow", "green")
253+
)
196254
except Exception as e:
197255
executed_operations.append(
198-
("用户信息表", f"重置失败: {str(e)}", "yellow", "red")
256+
("用户信息表", f"初始化失败: {str(e)}", "yellow", "red")
199257
)
200-
else:
201-
executed_operations.append(("用户信息表", "已跳过", "dim", "dim"))
202258

203259
# 显示操作结果汇总
204260
console.print("\n")

0 commit comments

Comments
 (0)