schema_patch.py
5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from __future__ import annotations
from sqlalchemy import text
def ensure_runtime_schema(engine) -> None:
if not str(engine.url).startswith("sqlite"):
return
with engine.begin() as conn:
table_names = {
row[0]
for row in conn.execute(text("SELECT name FROM sqlite_master WHERE type='table'"))
}
if "wecom_customer_position_groups" not in table_names:
conn.execute(
text(
"""
CREATE TABLE wecom_customer_position_groups (
id INTEGER PRIMARY KEY,
customer_contact_id VARCHAR(191) NOT NULL,
customer_name VARCHAR(255),
bot_code VARCHAR(191) NOT NULL,
bot_id VARCHAR(128) NOT NULL,
qrcode_id INTEGER NOT NULL,
qrcode_name VARCHAR(255),
channel_code VARCHAR(100) NOT NULL,
position_id INTEGER NOT NULL,
position_code VARCHAR(100) NOT NULL,
position_name VARCHAR(255) NOT NULL,
group_contact_id VARCHAR(191),
group_name VARCHAR(255),
group_status VARCHAR(32) NOT NULL DEFAULT 'pending',
create_task_id VARCHAR(191),
create_status VARCHAR(32) NOT NULL DEFAULT 'pending',
member_snapshot JSON,
dispatch_enabled BOOLEAN NOT NULL DEFAULT 1,
last_message_sync_at DATETIME,
is_deleted BOOLEAN NOT NULL DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
)
"""
)
)
conn.execute(text("CREATE UNIQUE INDEX uk_wecom_customer_position_group ON wecom_customer_position_groups(customer_contact_id, position_id)"))
conn.execute(text("CREATE INDEX idx_wcpg_bot_group ON wecom_customer_position_groups(bot_code, group_contact_id)"))
if "wecom_job_configs" not in table_names:
conn.execute(
text(
"""
CREATE TABLE wecom_job_configs (
id INTEGER PRIMARY KEY,
job_key VARCHAR(64) NOT NULL UNIQUE,
enabled BOOLEAN NOT NULL DEFAULT 1,
last_run_at DATETIME,
last_status VARCHAR(32),
last_error TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
)
"""
)
)
if "official_tabs" in table_names:
official_tab_columns = {
row[1]
for row in conn.execute(text("PRAGMA table_info(official_tabs)"))
}
if "main_title" not in official_tab_columns:
conn.execute(
text("ALTER TABLE official_tabs ADD COLUMN main_title VARCHAR(200)")
)
if "sub_title" not in official_tab_columns:
conn.execute(
text("ALTER TABLE official_tabs ADD COLUMN sub_title VARCHAR(500)")
)
if "ai_agents" in table_names:
ai_agent_columns = {
row[1]
for row in conn.execute(text("PRAGMA table_info(ai_agents)"))
}
if "managed_user_id" not in ai_agent_columns:
conn.execute(
text("ALTER TABLE ai_agents ADD COLUMN managed_user_id INTEGER")
)
conn.execute(
text(
"CREATE INDEX IF NOT EXISTS ix_ai_agents_managed_user_id "
"ON ai_agents(managed_user_id)"
)
)
if "site_display_status" not in ai_agent_columns:
conn.execute(
text(
"ALTER TABLE ai_agents ADD COLUMN site_display_status "
"VARCHAR(20) NOT NULL DEFAULT 'enable'"
)
)
conn.execute(
text(
"CREATE INDEX IF NOT EXISTS ix_ai_agents_site_display_status "
"ON ai_agents(site_display_status)"
)
)
if "summary" not in ai_agent_columns:
conn.execute(
text("ALTER TABLE ai_agents ADD COLUMN summary VARCHAR(500)")
)
if "official_site_agent_display" in table_names:
display_columns = {
row[1]
for row in conn.execute(text("PRAGMA table_info(official_site_agent_display)"))
}
if "media_url" not in display_columns:
conn.execute(
text("ALTER TABLE official_site_agent_display ADD COLUMN media_url VARCHAR(500)")
)
if "media_type" not in display_columns:
conn.execute(
text("ALTER TABLE official_site_agent_display ADD COLUMN media_type VARCHAR(10)")
)
if "icon_url" not in display_columns:
conn.execute(
text("ALTER TABLE official_site_agent_display ADD COLUMN icon_url VARCHAR(500)")
)
if "speech_title" not in display_columns:
conn.execute(
text("ALTER TABLE official_site_agent_display ADD COLUMN speech_title VARCHAR(100)")
)
if "speech_content" not in display_columns:
conn.execute(
text("ALTER TABLE official_site_agent_display ADD COLUMN speech_content VARCHAR(200)")
)