dy-dashboard/scripts/migrate_planning_new_old.py
2026-10-28 11:14:17 +08:00

38 lines
1.5 KiB
Python

"""Add the manual flag and pending snapshot; use --local on the test server."""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
if '--local' in sys.argv:
import test_wsgi
from services.db import query
def migrate():
for table, column, definition in (
('products', 'planning_new_old', "VARCHAR(8) NOT NULL DEFAULT '旧品'"),
('pending_orders', 'new_old_product', 'VARCHAR(8) NULL DEFAULT NULL'),
):
exists = query(
'SELECT COLUMN_NAME FROM information_schema.COLUMNS '
'WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=%s AND COLUMN_NAME=%s',
(table, column),
)
if not exists:
query(f'ALTER TABLE {table} ADD COLUMN {column} {definition}')
elif table == 'products' and column == 'planning_new_old':
query("ALTER TABLE products MODIFY COLUMN planning_new_old VARCHAR(8) NOT NULL DEFAULT '旧品'")
print(f'{table}.{column}: ready')
# 统一旧版本的“是/否”存量值,脚本可重复执行。
for table, column in (('products', 'planning_new_old'),
('pending_orders', 'new_old_product'),
('overseas_orders', '新旧品')):
query(f"UPDATE `{table}` SET `{column}`='新品' WHERE `{column}`='是'")
query(f"UPDATE `{table}` SET `{column}`='旧品' WHERE `{column}`='否'")
print(f'{table}.{column}: legacy values normalized')
if __name__ == '__main__':
migrate()