fix db init

This commit is contained in:
jxxghp
2024-07-20 08:47:26 +08:00
parent b949969b10
commit 0f8c2d3fc9
3 changed files with 51 additions and 30 deletions

View File

@@ -1,13 +1,8 @@
import random
import string
from alembic.command import upgrade
from alembic.config import Config
from app.core.config import settings
from app.core.security import get_password_hash
from app.db import Engine, SessionFactory, Base
from app.db.models import *
from app.db import Engine, Base
from app.log import logger
@@ -19,27 +14,6 @@ def init_db():
Base.metadata.create_all(bind=Engine)
def init_super_user():
"""
初始化超级管理员
"""
# 初始化超级管理员
with SessionFactory() as db:
_user = User.get_by_name(db=db, name=settings.SUPERUSER)
if not _user:
# 定义包含数字、大小写字母的字符集合
characters = string.ascii_letters + string.digits
# 生成随机密码
random_password = ''.join(random.choice(characters) for _ in range(16))
logger.info(f"【超级管理员初始密码】{random_password} 请登录系统后在设定中修改。 注:该密码只会显示一次,请注意保存。")
_user = User(
name=settings.SUPERUSER,
hashed_password=get_password_hash(random_password),
is_superuser=True,
)
_user.create(db)
def update_db():
"""
更新数据库

View File

@@ -21,7 +21,7 @@ if SystemUtils.is_frozen():
from app.core.config import settings, global_vars
from app.core.module import ModuleManager
from app.core.plugin import PluginManager
from app.db.init import init_db, update_db, init_super_user
from app.db.init import init_db, update_db
from app.helper.thread import ThreadHelper
from app.helper.display import DisplayHelper
from app.helper.resource import ResourceHelper
@@ -210,8 +210,6 @@ def start_module():
"""
启动模块
"""
# 初始化超级管理员
init_super_user()
# 虚拟显示
DisplayHelper()
# 站点管理

View File

@@ -0,0 +1,49 @@
"""2.0.0
Revision ID: 294b007932ef
Revises:
Create Date: 2024-07-20 08:43:40.741251
"""
import random
import string
from app.core.config import settings
from app.core.security import get_password_hash
from app.db import SessionFactory
from app.db.models import *
from app.log import logger
# revision identifiers, used by Alembic.
revision = '294b007932ef'
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
"""
v2.0.0 数据库初始化
"""
# 初始化超级管理员
with SessionFactory() as db:
_user = User.get_by_name(db=db, name=settings.SUPERUSER)
if not _user:
# 定义包含数字、大小写字母的字符集合
characters = string.ascii_letters + string.digits
# 生成随机密码
random_password = ''.join(random.choice(characters) for _ in range(16))
logger.info(
f"【超级管理员初始密码】{random_password} 请登录系统后在设定中修改。 注:该密码只会显示一次,请注意保存。")
_user = User(
name=settings.SUPERUSER,
hashed_password=get_password_hash(random_password),
email="admin@movie-pilot.org",
is_superuser=True,
)
_user.create(db)
def downgrade() -> None:
pass