支持已绑定设备追加 NFC 卡

This commit is contained in:
stu2not
2026-06-04 09:55:35 +08:00
parent 482fbf351f
commit 9f43fcb5ae
18 changed files with 646 additions and 92 deletions

View File

@@ -106,6 +106,61 @@ async def _ensure_child_location_address_columns(conn) -> None:
)
async def _ensure_bind_session_card_columns(conn) -> None:
await _ensure_columns(
conn,
table_name="device_bind_sessions",
columns=[
("bind_mode", "bind_mode TINYINT NOT NULL DEFAULT 1 AFTER status"),
("card_uuid", "card_uuid VARCHAR(64) NULL AFTER bind_mode"),
],
)
result = await conn.execute(
text(
"""
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'device_bind_sessions'
AND INDEX_NAME = 'idx_device_bind_sessions_card_uuid'
"""
)
)
if int(result.scalar() or 0) == 0:
await conn.execute(text("ALTER TABLE device_bind_sessions ADD INDEX idx_device_bind_sessions_card_uuid (card_uuid)"))
async def _ensure_cards_allow_multiple_per_device(conn) -> None:
result = await conn.execute(
text(
"""
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'cards'
AND INDEX_NAME = 'idx_cards_device_id'
"""
)
)
if int(result.scalar() or 0) == 0:
await conn.execute(text("ALTER TABLE cards ADD INDEX idx_cards_device_id (device_id)"))
result = await conn.execute(
text(
"""
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'cards'
AND INDEX_NAME = 'uq_cards_device_id'
"""
)
)
if int(result.scalar() or 0) > 0:
await conn.execute(text("ALTER TABLE cards DROP INDEX uq_cards_device_id"))
async def _ensure_device_family_tables(conn) -> None:
await conn.execute(
text(
@@ -222,6 +277,8 @@ async def init_db():
await _ensure_schedule_suppressed_until_column(conn)
await _ensure_device_alarm_location_columns(conn)
await _ensure_child_location_address_columns(conn)
await _ensure_bind_session_card_columns(conn)
await _ensure_cards_allow_multiple_per_device(conn)
await _ensure_device_family_tables(conn)
await engine.dispose()
session_logger.info("system", "database", "数据库表已成功创建")