fix(gate_bot): exclude active trend plans from orphan position warning

Trend pullback plans manage positions before order_monitors handoff; treat them as covered and add a pre-deploy DB backup script.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-06 09:40:14 +08:00
parent 32f4eec1d3
commit 4fad5696df
2 changed files with 114 additions and 4 deletions
@@ -0,0 +1,69 @@
#!/usr/bin/env python3
"""One-shot SQLite backup before code deploy. Reads DB_PATH from .env (default crypto.db)."""
from __future__ import annotations
import os
import shutil
import sqlite3
from datetime import datetime
from pathlib import Path
PROJECT_DIR = Path(__file__).resolve().parent.parent
def _read_env_db_path() -> Path:
env_file = PROJECT_DIR / ".env"
default = PROJECT_DIR / "crypto.db"
if not env_file.is_file():
return default
for line in env_file.read_text(encoding="utf-8", errors="replace").splitlines():
line = line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, val = line.split("=", 1)
if key.strip() != "DB_PATH":
continue
val = val.strip().strip('"').strip("'")
p = Path(val)
return p if p.is_absolute() else PROJECT_DIR / p
return default
def main() -> int:
db_path = _read_env_db_path()
if not db_path.is_file():
print(f"error: database not found: {db_path}")
return 1
stamp = datetime.now().strftime("%Y%m%d_%H%M%S")
dest_dir = PROJECT_DIR / "backups" / stamp
dest_dir.mkdir(parents=True, exist_ok=True)
dest = dest_dir / db_path.name
try:
src = sqlite3.connect(str(db_path))
dst = sqlite3.connect(str(dest))
src.backup(dst)
dst.close()
src.close()
method = "sqlite3 backup"
except Exception:
shutil.copy2(db_path, dest)
method = "file copy"
manifest = dest_dir / "manifest.txt"
manifest.write_text(
"\n".join(
[
f"project_dir={PROJECT_DIR}",
f"source_db={db_path}",
f"backup_file={dest}",
f"method={method}",
f"created_at={stamp}",
]
),
encoding="utf-8",
)
print(f"ok: {dest} ({method})")
return 0
if __name__ == "__main__":
raise SystemExit(main())