This commit is contained in:
wenfp
2026-07-27 16:36:33 +08:00
parent 53f48de2b7
commit 7f819cd775
19 changed files with 623 additions and 173 deletions
+92
View File
@@ -150,6 +150,41 @@ def ensure_product_schema(connection: sqlite3.Connection) -> None:
connection.execute(f"ALTER TABLE products ADD COLUMN {column_name} {definition}")
def ensure_ad_slot_schema(connection: sqlite3.Connection) -> None:
columns = {
row["name"] if isinstance(row, sqlite3.Row) else row[1]
for row in connection.execute("PRAGMA table_info(ad_slots)").fetchall()
}
additions = {
"name": "TEXT",
"code": "TEXT",
"description": "TEXT",
"title": "TEXT",
"image_url": "TEXT",
"target_type": "TEXT NOT NULL DEFAULT 'link'",
"target_url": "TEXT",
"product_id": "INTEGER",
"sort_order": "INTEGER NOT NULL DEFAULT 0",
"is_active": "INTEGER NOT NULL DEFAULT 1",
}
for column_name, definition in additions.items():
if column_name not in columns:
connection.execute(f"ALTER TABLE ad_slots ADD COLUMN {column_name} {definition}")
connection.execute("UPDATE ad_slots SET name = title WHERE (name IS NULL OR name = '') AND title IS NOT NULL")
connection.execute("UPDATE ad_slots SET name = '广告位' || id WHERE name IS NULL OR name = ''")
connection.execute("UPDATE ad_slots SET code = 'slot_' || id WHERE code IS NULL OR code = ''")
connection.execute("UPDATE ad_slots SET sort_order = 0 WHERE sort_order IS NULL")
connection.execute("UPDATE ad_slots SET is_active = 1 WHERE is_active IS NULL")
connection.execute(
"""
CREATE UNIQUE INDEX IF NOT EXISTS idx_ad_slots_temple_code_unique
ON ad_slots(temple_id, code)
WHERE code IS NOT NULL AND code != ''
"""
)
def init_db() -> None:
DATABASE_PATH.parent.mkdir(parents=True, exist_ok=True)
@@ -191,6 +226,63 @@ def init_db() -> None:
"""
)
ensure_product_schema(connection)
connection.execute(
"""
CREATE TABLE IF NOT EXISTS feature_icons (
id INTEGER PRIMARY KEY AUTOINCREMENT,
temple_id INTEGER NOT NULL,
title TEXT NOT NULL,
icon_url TEXT NOT NULL,
target_type TEXT NOT NULL DEFAULT 'link',
target_url TEXT,
product_id INTEGER,
sort_order INTEGER NOT NULL DEFAULT 0,
is_active INTEGER NOT NULL DEFAULT 1,
FOREIGN KEY (temple_id) REFERENCES temples(id),
FOREIGN KEY (product_id) REFERENCES products(id)
)
"""
)
connection.execute(
"""
CREATE TABLE IF NOT EXISTS ad_slots (
id INTEGER PRIMARY KEY AUTOINCREMENT,
temple_id INTEGER NOT NULL,
name TEXT,
code TEXT,
description TEXT,
title TEXT,
image_url TEXT,
target_type TEXT NOT NULL DEFAULT 'link',
target_url TEXT,
product_id INTEGER,
sort_order INTEGER NOT NULL DEFAULT 0,
is_active INTEGER NOT NULL DEFAULT 1,
FOREIGN KEY (temple_id) REFERENCES temples(id),
FOREIGN KEY (product_id) REFERENCES products(id)
)
"""
)
ensure_ad_slot_schema(connection)
connection.execute(
"""
CREATE TABLE IF NOT EXISTS ads (
id INTEGER PRIMARY KEY AUTOINCREMENT,
temple_id INTEGER NOT NULL,
slot_id INTEGER NOT NULL,
title TEXT NOT NULL,
image_url TEXT NOT NULL,
target_type TEXT NOT NULL DEFAULT 'link',
target_url TEXT,
product_id INTEGER,
sort_order INTEGER NOT NULL DEFAULT 0,
is_active INTEGER NOT NULL DEFAULT 1,
FOREIGN KEY (temple_id) REFERENCES temples(id),
FOREIGN KEY (slot_id) REFERENCES ad_slots(id),
FOREIGN KEY (product_id) REFERENCES products(id)
)
"""
)
connection.execute(
"""
CREATE TABLE IF NOT EXISTS ritual_services (