schema_patch.py
23.1 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
from __future__ import annotations
from sqlalchemy import text
from sqlalchemy.engine import make_url
def _rebuild_official_site_agent_display_without_unique(conn) -> None:
conn.execute(text("PRAGMA foreign_keys=OFF"))
conn.execute(
text(
"""
CREATE TABLE IF NOT EXISTS official_site_agent_display_new (
id INTEGER PRIMARY KEY,
tab_id INTEGER NOT NULL,
agent_id INTEGER NOT NULL,
agent_name VARCHAR(50) NOT NULL DEFAULT '',
status VARCHAR(20) NOT NULL DEFAULT 'enable',
summary VARCHAR(500),
icon_url VARCHAR(500),
media_url VARCHAR(500),
media_type VARCHAR(10),
speech_title VARCHAR(100),
speech_content VARCHAR(200),
display_status VARCHAR(20) NOT NULL DEFAULT 'enable',
sort_order INTEGER NOT NULL DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
)
"""
)
)
conn.execute(
text(
"""
INSERT INTO official_site_agent_display_new (
id,
tab_id,
agent_id,
agent_name,
status,
summary,
icon_url,
media_url,
media_type,
speech_title,
speech_content,
display_status,
sort_order,
created_at,
updated_at
)
SELECT
id,
tab_id,
agent_id,
COALESCE(agent_name, ''),
COALESCE(status, 'enable'),
summary,
icon_url,
media_url,
media_type,
speech_title,
speech_content,
COALESCE(display_status, 'enable'),
COALESCE(sort_order, 0),
created_at,
updated_at
FROM official_site_agent_display
"""
)
)
conn.execute(text("DROP TABLE official_site_agent_display"))
conn.execute(text("ALTER TABLE official_site_agent_display_new RENAME TO official_site_agent_display"))
conn.execute(text("CREATE INDEX IF NOT EXISTS ix_official_site_agent_display_tab_id ON official_site_agent_display(tab_id)"))
conn.execute(text("CREATE INDEX IF NOT EXISTS ix_official_site_agent_display_agent_id ON official_site_agent_display(agent_id)"))
conn.execute(text("CREATE INDEX IF NOT EXISTS ix_official_site_agent_display_display_status ON official_site_agent_display(display_status)"))
conn.execute(text("CREATE INDEX IF NOT EXISTS ix_official_site_agent_display_sort_order ON official_site_agent_display(sort_order)"))
conn.execute(text("CREATE INDEX IF NOT EXISTS idx_osad_status ON official_site_agent_display(status)"))
conn.execute(text("PRAGMA foreign_keys=ON"))
def _rebuild_qrcode_channels_without_redundant_position_fields(conn) -> None:
conn.execute(text("PRAGMA foreign_keys=OFF"))
conn.execute(
text(
"""
CREATE TABLE IF NOT EXISTS qrcode_channels_new (
id INTEGER PRIMARY KEY,
qrcode_name VARCHAR(50),
position_id INTEGER,
wecom_bot_id INTEGER,
employee_id VARCHAR(100) NOT NULL,
employee_name VARCHAR(100) NOT NULL DEFAULT '',
qrcode_url VARCHAR(500),
agent_ids JSON,
remark VARCHAR(200),
welcome_message VARCHAR(500),
status VARCHAR(20) NOT NULL DEFAULT 'enable',
disable_reason VARCHAR(200),
created_by VARCHAR(64),
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
)
"""
)
)
conn.execute(
text(
"""
INSERT INTO qrcode_channels_new (
id,
qrcode_name,
position_id,
wecom_bot_id,
employee_id,
employee_name,
qrcode_url,
agent_ids,
remark,
welcome_message,
status,
disable_reason,
created_by,
created_at,
updated_at
)
SELECT
id,
qrcode_name,
position_id,
wecom_bot_id,
employee_id,
COALESCE(employee_name, ''),
qrcode_url,
agent_ids,
remark,
welcome_message,
COALESCE(status, 'enable'),
disable_reason,
created_by,
created_at,
updated_at
FROM qrcode_channels
"""
)
)
conn.execute(text("DROP TABLE qrcode_channels"))
conn.execute(text("ALTER TABLE qrcode_channels_new RENAME TO qrcode_channels"))
conn.execute(text("CREATE UNIQUE INDEX IF NOT EXISTS uk_qrcode_position_id ON qrcode_channels(position_id)"))
conn.execute(text("CREATE UNIQUE INDEX IF NOT EXISTS uk_qrcode_wecom_bot_id ON qrcode_channels(wecom_bot_id)"))
conn.execute(text("CREATE INDEX IF NOT EXISTS ix_qrcode_channels_employee_id ON qrcode_channels(employee_id)"))
conn.execute(text("CREATE INDEX IF NOT EXISTS ix_qrcode_channels_status ON qrcode_channels(status)"))
conn.execute(text("CREATE INDEX IF NOT EXISTS ix_qrcode_channels_position_id ON qrcode_channels(position_id)"))
conn.execute(text("CREATE INDEX IF NOT EXISTS ix_qrcode_channels_wecom_bot_id ON qrcode_channels(wecom_bot_id)"))
conn.execute(text("PRAGMA foreign_keys=ON"))
def _rebuild_official_tabs_without_redundant_position_fields(conn) -> None:
conn.execute(text("PRAGMA foreign_keys=OFF"))
conn.execute(
text(
"""
CREATE TABLE IF NOT EXISTS official_tabs_new (
id INTEGER PRIMARY KEY,
tab_name VARCHAR(100) NOT NULL,
position_id INTEGER,
qrcode_id INTEGER,
display_status VARCHAR(20) NOT NULL DEFAULT 'enable',
sort_order INTEGER NOT NULL DEFAULT 0,
main_title VARCHAR(200),
sub_title VARCHAR(500),
created_by VARCHAR(64),
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
deleted_at DATETIME
)
"""
)
)
conn.execute(
text(
"""
INSERT INTO official_tabs_new (
id,
tab_name,
position_id,
qrcode_id,
display_status,
sort_order,
main_title,
sub_title,
created_by,
created_at,
updated_at,
deleted_at
)
SELECT
id,
COALESCE(tab_name, ''),
position_id,
qrcode_id,
COALESCE(display_status, 'enable'),
COALESCE(sort_order, 0),
main_title,
sub_title,
created_by,
created_at,
updated_at,
deleted_at
FROM official_tabs
"""
)
)
conn.execute(text("DROP TABLE official_tabs"))
conn.execute(text("ALTER TABLE official_tabs_new RENAME TO official_tabs"))
conn.execute(text("CREATE INDEX IF NOT EXISTS ix_official_tabs_position_id ON official_tabs(position_id)"))
conn.execute(text("CREATE INDEX IF NOT EXISTS ix_official_tabs_qrcode_id ON official_tabs(qrcode_id)"))
conn.execute(text("CREATE INDEX IF NOT EXISTS ix_official_tabs_display_status ON official_tabs(display_status)"))
conn.execute(text("CREATE INDEX IF NOT EXISTS ix_official_tabs_sort_order ON official_tabs(sort_order)"))
conn.execute(text("CREATE INDEX IF NOT EXISTS ix_official_tabs_deleted_at ON official_tabs(deleted_at)"))
conn.execute(text("PRAGMA foreign_keys=ON"))
def _column_names(conn, dialect: str, table_name: str) -> set[str]:
if dialect == "sqlite":
result = conn.execute(text(f"PRAGMA table_info({table_name})"))
return {row[1] for row in result}
result = conn.execute(text(f"SHOW COLUMNS FROM {table_name}"))
return {row[0] for row in result}
def _mysql_table_names(conn) -> set[str]:
result = conn.execute(text("SHOW TABLES"))
return {row[0] for row in result}
def _ensure_mysql_runtime_schema(conn) -> None:
conn.execute(
text(
"""
CREATE TABLE IF NOT EXISTS wecom_employee_qrcodes (
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
employee_id VARCHAR(100) NOT NULL UNIQUE,
qrcode_url VARCHAR(500) NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX uk_wecom_employee_qrcodes_employee_id (employee_id)
)
"""
)
)
table_names = _mysql_table_names(conn)
if "customer_group_phones" not in table_names:
conn.execute(
text(
"""
CREATE TABLE customer_group_phones (
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
customer_name VARCHAR(255) NOT NULL,
group_id VARCHAR(255) NOT NULL,
group_status VARCHAR(50) NOT NULL DEFAULT 'active',
phone VARCHAR(20) DEFAULT NULL,
last_pushed_at DATETIME DEFAULT NULL,
create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY uk_group_id (group_id),
KEY idx_phone (phone),
KEY idx_push_candidates (group_status, phone, last_pushed_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
"""
)
)
if "wecom_messages" in table_names:
columns = _column_names(conn, "mysql", "wecom_messages")
if "customer_group_id" not in columns:
conn.execute(text("ALTER TABLE wecom_messages ADD COLUMN customer_group_id INTEGER NULL"))
conn.execute(text("CREATE INDEX ix_wecom_messages_customer_group_id ON wecom_messages(customer_group_id)"))
if "official_tabs" in table_names:
columns = _column_names(conn, "mysql", "official_tabs")
if "main_title" not in columns:
conn.execute(text("ALTER TABLE official_tabs ADD COLUMN main_title VARCHAR(200) NULL"))
if "sub_title" not in columns:
conn.execute(text("ALTER TABLE official_tabs ADD COLUMN sub_title VARCHAR(500) NULL"))
if "ai_agents" in table_names:
columns = _column_names(conn, "mysql", "ai_agents")
if "managed_user_id" not in columns:
conn.execute(text("ALTER TABLE ai_agents ADD COLUMN managed_user_id INTEGER NULL"))
conn.execute(text("CREATE INDEX ix_ai_agents_managed_user_id ON ai_agents(managed_user_id)"))
if "is_steward" not in columns:
conn.execute(text("ALTER TABLE ai_agents ADD COLUMN is_steward BOOLEAN NOT NULL DEFAULT 0"))
if "official_site_agent_display" in table_names:
columns = _column_names(conn, "mysql", "official_site_agent_display")
if "agent_name" not in columns:
conn.execute(text("ALTER TABLE official_site_agent_display ADD COLUMN agent_name VARCHAR(50) NOT NULL DEFAULT ''"))
if "status" not in columns:
conn.execute(text("ALTER TABLE official_site_agent_display ADD COLUMN status VARCHAR(20) NOT NULL DEFAULT 'enable'"))
if "summary" not in columns:
conn.execute(text("ALTER TABLE official_site_agent_display ADD COLUMN summary VARCHAR(500) NULL"))
if "icon_url" not in columns:
conn.execute(text("ALTER TABLE official_site_agent_display ADD COLUMN icon_url VARCHAR(500) NULL"))
if "media_url" not in columns:
conn.execute(text("ALTER TABLE official_site_agent_display ADD COLUMN media_url VARCHAR(500) NULL"))
if "media_type" not in columns:
conn.execute(text("ALTER TABLE official_site_agent_display ADD COLUMN media_type VARCHAR(10) NULL"))
if "speech_title" not in columns:
conn.execute(text("ALTER TABLE official_site_agent_display ADD COLUMN speech_title VARCHAR(100) NULL"))
if "speech_content" not in columns:
conn.execute(text("ALTER TABLE official_site_agent_display ADD COLUMN speech_content VARCHAR(200) NULL"))
def _ensure_sqlite_runtime_schema(conn) -> None:
table_names = {
row[0]
for row in conn.execute(text("SELECT name FROM sqlite_master WHERE type='table'"))
}
if "wecom_customer_position_groups" in table_names:
conn.execute(text("DROP TABLE wecom_customer_position_groups"))
table_names.remove("wecom_customer_position_groups")
if "wecom_groups" in table_names:
conn.execute(text("DROP TABLE wecom_groups"))
table_names.remove("wecom_groups")
if "qrcode_channels" in table_names:
qrcode_columns = _column_names(conn, "sqlite", "qrcode_channels")
if "position_name" in qrcode_columns or "channel_code" in qrcode_columns or "agent_names" in qrcode_columns:
_rebuild_qrcode_channels_without_redundant_position_fields(conn)
if "wecom_employee_qrcodes" not in table_names:
conn.execute(
text(
"""
CREATE TABLE wecom_employee_qrcodes (
id INTEGER PRIMARY KEY,
employee_id VARCHAR(100) NOT NULL UNIQUE,
qrcode_url VARCHAR(500) NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
)
"""
)
)
conn.execute(
text(
"CREATE UNIQUE INDEX IF NOT EXISTS uk_wecom_employee_qrcodes_employee_id "
"ON wecom_employee_qrcodes(employee_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 "wecom_group_create_requests" not in table_names:
conn.execute(
text(
"""
CREATE TABLE wecom_group_create_requests (
id INTEGER PRIMARY KEY,
biz_rela_no VARCHAR(191) NOT NULL UNIQUE,
bot_id VARCHAR(128) NOT NULL,
bot_code VARCHAR(191) NOT NULL,
customer_contact_id VARCHAR(191) NOT NULL,
customer_name VARCHAR(255),
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_name VARCHAR(255) NOT NULL,
member_bot_ids JSON NOT NULL,
member_snapshot JSON,
request_payload JSON,
submit_result JSON,
task_id VARCHAR(191),
status VARCHAR(32) NOT NULL DEFAULT 'pending',
fail_reason TEXT,
resolved_group_contact_id VARCHAR(191),
resolved_group_name VARCHAR(255),
resolved_contact_type VARCHAR(64),
external_result JSON,
resolved_at DATETIME,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
)
"""
)
)
conn.execute(text("CREATE INDEX idx_wgcr_status ON wecom_group_create_requests(status)"))
conn.execute(text("CREATE INDEX idx_wgcr_bot ON wecom_group_create_requests(bot_id)"))
conn.execute(text("CREATE INDEX idx_wgcr_customer ON wecom_group_create_requests(customer_contact_id)"))
conn.execute(text("CREATE INDEX idx_wgcr_task_id ON wecom_group_create_requests(task_id)"))
conn.execute(text("CREATE INDEX idx_wgcr_resolved_group ON wecom_group_create_requests(resolved_group_contact_id)"))
else:
group_create_columns = _column_names(conn, "sqlite", "wecom_group_create_requests")
if "submit_result" not in group_create_columns:
conn.execute(text("ALTER TABLE wecom_group_create_requests ADD COLUMN submit_result JSON"))
if "wecom_customer_groups" not in table_names:
conn.execute(
text(
"""
CREATE TABLE wecom_customer_groups (
id INTEGER PRIMARY KEY,
customer_contact_id VARCHAR(191) NOT NULL UNIQUE,
customer_name VARCHAR(255),
bot_id VARCHAR(128) NOT NULL,
bot_code VARCHAR(191) NOT NULL,
group_id VARCHAR(191),
group_name VARCHAR(255),
group_status VARCHAR(32) NOT NULL DEFAULT 'pending',
group_create_request_id INTEGER,
member_bot_ids JSON NOT NULL,
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 INDEX ix_wecom_customer_groups_customer_contact_id ON wecom_customer_groups(customer_contact_id)"))
conn.execute(text("CREATE INDEX ix_wecom_customer_groups_bot_id ON wecom_customer_groups(bot_id)"))
conn.execute(text("CREATE INDEX ix_wecom_customer_groups_bot_code ON wecom_customer_groups(bot_code)"))
conn.execute(text("CREATE INDEX ix_wecom_customer_groups_group_id ON wecom_customer_groups(group_id)"))
conn.execute(text("CREATE INDEX ix_wecom_customer_groups_group_status ON wecom_customer_groups(group_status)"))
conn.execute(text("CREATE INDEX ix_wecom_customer_groups_group_create_request_id ON wecom_customer_groups(group_create_request_id)"))
conn.execute(text("CREATE INDEX ix_wecom_customer_groups_is_deleted ON wecom_customer_groups(is_deleted)"))
if "wecom_messages" in table_names:
wecom_message_columns = _column_names(conn, "sqlite", "wecom_messages")
if "customer_group_id" not in wecom_message_columns:
conn.execute(text("ALTER TABLE wecom_messages ADD COLUMN customer_group_id INTEGER"))
conn.execute(text("CREATE INDEX IF NOT EXISTS ix_wecom_messages_customer_group_id ON wecom_messages(customer_group_id)"))
if "official_tabs" in table_names:
official_tab_columns = _column_names(conn, "sqlite", "official_tabs")
if "position_code" in official_tab_columns or "position_name" in official_tab_columns:
_rebuild_official_tabs_without_redundant_position_fields(conn)
official_tab_columns = _column_names(conn, "sqlite", "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 = _column_names(conn, "sqlite", "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 "is_steward" not in ai_agent_columns:
conn.execute(text("ALTER TABLE ai_agents ADD COLUMN is_steward BOOLEAN NOT NULL DEFAULT 0"))
if "official_site_agent_display" in table_names:
display_columns = _column_names(conn, "sqlite", "official_site_agent_display")
display_table_sql = conn.execute(
text("SELECT sql FROM sqlite_master WHERE type='table' AND name='official_site_agent_display'")
).scalar() or ""
if "UNIQUE (tab_id, agent_id)" in display_table_sql or "UNIQUE(tab_id, agent_id)" in display_table_sql:
_rebuild_official_site_agent_display_without_unique(conn)
display_columns = _column_names(conn, "sqlite", "official_site_agent_display")
if "agent_name" not in display_columns:
conn.execute(text("ALTER TABLE official_site_agent_display ADD COLUMN agent_name VARCHAR(50) NOT NULL DEFAULT ''"))
if "status" not in display_columns:
conn.execute(text("ALTER TABLE official_site_agent_display ADD COLUMN status VARCHAR(20) NOT NULL DEFAULT 'enable'"))
conn.execute(text("CREATE INDEX IF NOT EXISTS idx_osad_status ON official_site_agent_display(status)"))
if "summary" not in display_columns:
conn.execute(text("ALTER TABLE official_site_agent_display ADD COLUMN summary VARCHAR(500)"))
if "icon_url" not in display_columns:
conn.execute(text("ALTER TABLE official_site_agent_display ADD COLUMN icon_url VARCHAR(500)"))
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 "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)"))
def ensure_runtime_schema(engine) -> None:
dialect = make_url(str(engine.url)).get_backend_name()
if dialect != "mysql":
raise RuntimeError(f"Unsupported database dialect: {dialect}. Only MySQL is allowed.")
with engine.begin() as conn:
_ensure_mysql_runtime_schema(conn)