fix: 已有数据库再次安装时接上原库而不是报错
This commit is contained in:
@@ -37,10 +37,10 @@ def install_status() -> dict:
|
||||
@router.post("/install")
|
||||
def install(body: dict) -> dict:
|
||||
try:
|
||||
perform_install(body)
|
||||
already = perform_install(body)
|
||||
except InstallError as exc:
|
||||
raise _bad(str(exc)) from exc
|
||||
return {"ok": True}
|
||||
return {"ok": True, "already": already}
|
||||
|
||||
|
||||
def _bad(message: str, status: int = 400) -> HTTPException:
|
||||
|
||||
+25
-16
@@ -7,8 +7,9 @@ import os
|
||||
import re
|
||||
import secrets
|
||||
|
||||
from sqlalchemy import create_engine, text
|
||||
from sqlalchemy import create_engine, select, text
|
||||
from sqlalchemy.engine import URL
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from app.config import data_dir, install_file, installed
|
||||
from app.db import reset_engine, session_scope
|
||||
@@ -55,7 +56,7 @@ def ensure_database(host: str, port: int, database: str, username: str, password
|
||||
raise InstallError("无法连接数据库,请检查主机、端口、账号和密码") from exc
|
||||
|
||||
|
||||
def perform_install(body: dict) -> None:
|
||||
def perform_install(body: dict) -> bool:
|
||||
if installed():
|
||||
raise InstallError("已经安装")
|
||||
admin_username = str(body.get("adminUsername") or "").strip()
|
||||
@@ -89,21 +90,29 @@ def perform_install(body: dict) -> None:
|
||||
|
||||
migrate()
|
||||
with session_scope() as session:
|
||||
session.add(
|
||||
AppSetting(
|
||||
id=1,
|
||||
login_mode="local",
|
||||
credential_refresh_seconds=300,
|
||||
worker_concurrency=4,
|
||||
db_host=host,
|
||||
db_port=port,
|
||||
db_name=database,
|
||||
db_user=username,
|
||||
db_password=password,
|
||||
jwt_secret=secrets.token_hex(32),
|
||||
already = session.get(AppSetting, 1) is not None
|
||||
if not already:
|
||||
session.add(
|
||||
AppSetting(
|
||||
id=1,
|
||||
login_mode="local",
|
||||
credential_refresh_seconds=300,
|
||||
worker_concurrency=4,
|
||||
db_host=host,
|
||||
db_port=port,
|
||||
db_name=database,
|
||||
db_user=username,
|
||||
db_password=password,
|
||||
jwt_secret=secrets.token_hex(32),
|
||||
)
|
||||
)
|
||||
)
|
||||
session.add(User(username=admin_username, password_hash=hash_password(admin_password), role="admin"))
|
||||
if session.scalar(select(User).where(User.username == admin_username)) is None:
|
||||
session.add(User(username=admin_username, password_hash=hash_password(admin_password), role="admin"))
|
||||
return already
|
||||
except IntegrityError as exc:
|
||||
install_file().unlink(missing_ok=True)
|
||||
reset_engine()
|
||||
raise InstallError("这个数据库已经安装过") from exc
|
||||
except Exception:
|
||||
install_file().unlink(missing_ok=True)
|
||||
reset_engine()
|
||||
|
||||
@@ -43,6 +43,21 @@ def client():
|
||||
shutil.rmtree(TEST_DATA)
|
||||
|
||||
|
||||
def test_install_again_uses_existing_database(client: TestClient):
|
||||
from app.config import install_file
|
||||
from app.db import reset_engine
|
||||
|
||||
install_file().unlink()
|
||||
reset_engine()
|
||||
again = client.post("/api/install", json={"sqlite": True, "adminUsername": "other", "adminPassword": "other-pass"})
|
||||
assert again.status_code == 200
|
||||
assert again.json()["already"] is True
|
||||
logged = client.post("/api/auth/local", json={"username": "admin", "password": "admin-pass"})
|
||||
assert logged.status_code == 200
|
||||
rejected = client.post("/api/auth/local", json={"username": "other", "password": "other-pass"})
|
||||
assert rejected.status_code == 400
|
||||
|
||||
|
||||
def test_period_bounds():
|
||||
assert check_period(1) == 1
|
||||
assert check_period(31) == 31
|
||||
|
||||
Reference in New Issue
Block a user