fix(scripts): legacy 归档脚本适配死列下线——migrate_custom 改裸 SQL 读 ledger_items(模型已删、表仍在),fix_period_split/merge_project 不再写/排 Task.sort_order;全部 scripts py_compile 通过
parent
7a7b83a271
commit
bb651a6d4d
|
|
@ -255,7 +255,7 @@ def main() -> None:
|
|||
t = db.query(Task).filter(
|
||||
Task.project_id == p.id, Task.name == task_name[:255]).one_or_none()
|
||||
if not t:
|
||||
t = Task(project_id=p.id, name=task_name[:255], sort_order=999)
|
||||
t = Task(project_id=p.id, name=task_name[:255])
|
||||
db.add(t)
|
||||
db.flush()
|
||||
db.add(Update(
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ def main():
|
|||
}
|
||||
merged, moved = [], []
|
||||
|
||||
for t in db.query(Task).filter(Task.project_id == src_id).order_by(Task.sort_order, Task.id).all():
|
||||
for t in db.query(Task).filter(Task.project_id == src_id).order_by(Task.id).all():
|
||||
n = db.query(Update).filter(Update.task_id == t.id).count()
|
||||
target = dst_map.get(t.name)
|
||||
if target:
|
||||
|
|
|
|||
|
|
@ -25,16 +25,32 @@ from app.constants import ( # noqa: E402
|
|||
infer_region,
|
||||
normalize_custom_name,
|
||||
)
|
||||
from sqlalchemy import inspect, text # noqa: E402
|
||||
|
||||
from app.db import SessionLocal, engine # noqa: E402
|
||||
from app.models import ( # noqa: E402
|
||||
Base,
|
||||
CustomProject,
|
||||
CustomUpdate,
|
||||
LedgerItem,
|
||||
Period,
|
||||
Project,
|
||||
)
|
||||
|
||||
|
||||
class _LedgerRow:
|
||||
"""ledger_items 一行(LedgerItem 模型 2026-11-04 已删,表仍留在老库里,用裸 SQL 读)。"""
|
||||
def __init__(self, d):
|
||||
self.__dict__.update(d)
|
||||
|
||||
|
||||
def _ledger_rows(db):
|
||||
if not inspect(engine).has_table("ledger_items"):
|
||||
return []
|
||||
rows = db.execute(text(
|
||||
"SELECT id, name, region, amount, category, status_note, period_label, period_id "
|
||||
"FROM ledger_items ORDER BY id")).mappings().all()
|
||||
return [_LedgerRow(dict(x)) for x in rows]
|
||||
|
||||
CONTAINER = "定制开发"
|
||||
|
||||
|
||||
|
|
@ -49,7 +65,7 @@ def main():
|
|||
# ---------------------------------------------------------------- 台账
|
||||
by_name: dict[str, CustomProject] = {}
|
||||
created_from_ledger = 0
|
||||
for it in db.query(LedgerItem).order_by(LedgerItem.id).all():
|
||||
for it in _ledger_rows(db):
|
||||
clean, amt = normalize_custom_name(it.name)
|
||||
if not clean:
|
||||
continue
|
||||
|
|
@ -83,7 +99,7 @@ def main():
|
|||
moved_updates = 0
|
||||
|
||||
if container:
|
||||
for t in sorted(container.tasks, key=lambda x: (x.sort_order, x.id)):
|
||||
for t in sorted(container.tasks, key=lambda x: x.id):
|
||||
clean, amt = normalize_custom_name(t.name)
|
||||
if not clean:
|
||||
continue
|
||||
|
|
@ -131,7 +147,8 @@ def main():
|
|||
# ------------------------------------------------------------ 清理常规表
|
||||
if not args.dry_run:
|
||||
db.delete(container)
|
||||
db.query(LedgerItem).delete()
|
||||
if inspect(engine).has_table("ledger_items"):
|
||||
db.execute(text("DELETE FROM ledger_items"))
|
||||
|
||||
# ---------------------------------------------------------------- 收尾
|
||||
for cp in by_name.values():
|
||||
|
|
|
|||
Loading…
Reference in New Issue