59 lines
2.1 KiB
Python
59 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Импорт справочников RACION (JSON seed) в БД.
|
|
|
|
Требует миграцию lab_racion_normy (0023). Скрипт применяет alembic upgrade head синхронно.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description="Import RACION NORMY reference into DB")
|
|
parser.add_argument("--no-replace", action="store_true", help="Do not clear tables before import")
|
|
parser.add_argument("--sync-profile", metavar="KEY", help="Sync racion norms to profile by profile_key")
|
|
args = parser.parse_args()
|
|
|
|
# Без фонового старта — иначе create_app() откладывает миграции и таблиц ещё нет.
|
|
os.environ.setdefault("WESP_BACKGROUND_STARTUP", "0")
|
|
|
|
from app import create_app
|
|
|
|
app = create_app(run_migrations=True)
|
|
with app.app_context():
|
|
from app.lab.services.racion_reference import import_racion_reference
|
|
|
|
stats = import_racion_reference(replace=not args.no_replace)
|
|
if stats.errors:
|
|
for err in stats.errors:
|
|
print(f"ERROR: {err}", file=sys.stderr)
|
|
return 1
|
|
print(
|
|
f"Imported: moskwa={stats.moskwa_rows} piter={stats.piter_rows} info={stats.info_rows}"
|
|
)
|
|
|
|
if args.sync_profile:
|
|
from app.lab.models import LabAnimalProfile
|
|
from app.lab.services.profile_norms import sync_racion_norms_to_profile
|
|
|
|
profile = LabAnimalProfile.query.filter_by(
|
|
profile_key=args.sync_profile, is_deleted=False
|
|
).first()
|
|
if profile is None:
|
|
print(f"Profile not found: {args.sync_profile}", file=sys.stderr)
|
|
return 1
|
|
n = sync_racion_norms_to_profile(profile)
|
|
print(f"Synced {n} norm rows for profile {args.sync_profile}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|