120 lines
6.0 KiB
Python
120 lines
6.0 KiB
Python
"""Back up, merge and import both Google history tabs into corm_db once."""
|
|
|
|
import argparse
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
from history_store import PostgresHistoryStore, merge_sheet_and_local, validate_bundle
|
|
from settings_migrate import BASE_DIR, fingerprint, save_snapshot
|
|
from settings_store import SettingsAlreadyInitialized, SettingsNotInitialized, settings_db_config
|
|
|
|
|
|
def read_local_records(path):
|
|
target = Path(path)
|
|
if not target.exists():
|
|
return []
|
|
return [json.loads(line) for line in target.read_text(encoding="utf-8").splitlines() if line.strip()]
|
|
|
|
|
|
def read_source(sms_local_file=None, manual_local_file=None):
|
|
import main
|
|
sms_path = Path(sms_local_file or main.SMS_HISTORY_FILE)
|
|
manual_path = Path(manual_local_file or main.MANUAL_HISTORY_FILE)
|
|
# Explicit source paths must exist; a missing mounted file must not lose detail silently.
|
|
for explicit_path in (sms_local_file, manual_local_file):
|
|
if explicit_path and not Path(explicit_path).is_file():
|
|
raise FileNotFoundError("지정한 로컬 내역 파일이 없습니다.")
|
|
local_sms = read_local_records(sms_path)
|
|
local_manual = read_local_records(manual_path)
|
|
spreadsheet = main.get_gspread_client().open_by_key(main.SETTINGS_SPREADSHEET_ID)
|
|
sms_rows = spreadsheet.worksheet("문자내역").get_all_values()
|
|
manual_rows = spreadsheet.worksheet("수동발주내역").get_all_values()
|
|
bundle = {
|
|
"kind": "corm-history",
|
|
"sms": merge_sheet_and_local("sms", sms_rows, local_sms),
|
|
"manual": merge_sheet_and_local("manual", manual_rows, local_manual),
|
|
# Keep complete source material, including unused columns and local-only records.
|
|
"source_rows": {"문자내역": sms_rows, "수동발주내역": manual_rows},
|
|
"local_records": {"sms": local_sms, "manual": local_manual},
|
|
"local_files_found": {"sms": sms_path.is_file(), "manual": manual_path.is_file()},
|
|
}
|
|
return bundle, "google:" + main.SETTINGS_SPREADSHEET_ID + ":histories+local"
|
|
|
|
|
|
def load_backup(path):
|
|
payload = json.loads(Path(path).read_text(encoding="utf-8"))
|
|
bundle = payload["rows"]
|
|
if payload.get("format") != 1 or payload.get("sha256") != fingerprint(bundle):
|
|
raise ValueError("내역 백업 체크섬이 올바르지 않습니다.")
|
|
validate_bundle(bundle)
|
|
return bundle, "history-backup-file"
|
|
|
|
|
|
def build_store():
|
|
return PostgresHistoryStore(settings_db_config())
|
|
|
|
|
|
def main(argv=None):
|
|
load_dotenv(BASE_DIR / ".env")
|
|
parser = argparse.ArgumentParser(description="문자내역·수동발주내역 → corm_db 이전 / 검증 / 백업")
|
|
commands = parser.add_subparsers(dest="command", required=True)
|
|
importer = commands.add_parser("import", help="기본은 점검만, --apply일 때만 DB 저장")
|
|
importer.add_argument("--apply", action="store_true")
|
|
importer.add_argument("--backup")
|
|
importer.add_argument("--input-file")
|
|
verifier = commands.add_parser("verify", help="전체 내역과 순서를 검증")
|
|
verifier.add_argument("--input-file")
|
|
snapshot = commands.add_parser("snapshot", help="DB에 접속하지 않고 시트·로컬 원본 백업")
|
|
snapshot.add_argument("--output", required=True)
|
|
for command in (importer, verifier, snapshot):
|
|
command.add_argument("--sms-local-file", help="기존 sms_history.dat 파일 경로")
|
|
command.add_argument("--manual-local-file", help="기존 manual_history.dat 파일 경로")
|
|
exporter = commands.add_parser("export", help="DB 내역 전체 백업")
|
|
exporter.add_argument("--output", required=True)
|
|
args = parser.parse_args(argv)
|
|
if args.command == "import" and args.apply and not args.backup:
|
|
parser.error("--apply에는 --backup 경로가 필요합니다.")
|
|
try:
|
|
if args.command == "export":
|
|
bundle = build_store().export_all()
|
|
save_snapshot(args.output, bundle, "postgres:histories")
|
|
print(f"내역 백업 완료: 문자 {len(bundle['sms'])}건 / 수동발주 {len(bundle['manual'])}건.")
|
|
return 0
|
|
bundle, source = (load_backup(args.input_file) if getattr(args, "input_file", None) else
|
|
read_source(args.sms_local_file, args.manual_local_file))
|
|
expected = validate_bundle(bundle)
|
|
print(f"원본 확인: 문자 {len(expected['sms'])}건 / 수동발주 {len(expected['manual'])}건.")
|
|
if "local_files_found" in bundle:
|
|
for kind, label in (("sms", "문자"), ("manual", "수동발주")):
|
|
state = "있음" if bundle["local_files_found"][kind] else "없음"
|
|
print(f"로컬 {label} 파일: {state} ({len(bundle['local_records'][kind])}건).")
|
|
if args.command == "snapshot":
|
|
save_snapshot(args.output, bundle, source)
|
|
print("시트·로컬 원본 백업 완료. DB·구글 시트에 쓰지 않았습니다.")
|
|
return 0
|
|
if args.command == "verify":
|
|
if validate_bundle(build_store().export_all()) != expected:
|
|
print("검증 실패: 원본과 DB 내역이 다릅니다. 전환 전에 차이를 확인해주세요.", file=sys.stderr)
|
|
return 1
|
|
print("원본과 DB 전체 내역·순서 일치.")
|
|
elif args.apply:
|
|
save_snapshot(args.backup, bundle, source)
|
|
build_store().import_all(bundle, source)
|
|
print("두 내역의 DB 이전 및 트랜잭션 내 전체 검증 완료.")
|
|
else:
|
|
print("점검만 완료했습니다. DB·구글 시트에 쓰지 않았습니다.")
|
|
return 0
|
|
except (SettingsAlreadyInitialized, SettingsNotInitialized) as exc:
|
|
print(str(exc), file=sys.stderr)
|
|
except Exception as exc:
|
|
# Never print customer information, source rows or connection credentials.
|
|
print(f"작업 실패 ({type(exc).__name__}). 접속 환경·원본·로컬 내역·백업 경로를 확인해주세요.", file=sys.stderr)
|
|
return 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|