Spaces:
Paused
Paused
Delete import_local_to_pg.py with huggingface_hub
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253"""Import local auth files from /data/auths/ to PostgreSQL auth_store table.Run once at container startup before CLIProxyAPI starts (when PGSTORE_DSN is set).This ensures local files are merged into PG when switching from local to PG mode."""
import os, sys, json, glob, psycopg2
PGSTORE_DSN = os.environ.get("PGSTORE_DSN", "")AUTH_DIR = "/data/auths"TABLE = "auth_store"
def main(): if not PGSTORE_DSN: print("PGSTORE_DSN not set, skipping local-to-PG import") return
files = glob.glob(os.path.join(AUTH_DIR, "*.json")) if not files: print(f"No local auth files in {AUTH_DIR}, skipping import") return
print(f"Found {len(files)} local auth files to import to PG...") conn = psycopg2.connect(PGSTORE_DSN) imported = 0 skipped = 0
with conn.cursor() as cur: for fpath in files: fname = os.path.basename(fpath) try: with open(fpath, "r") as f: content = json.load(f) cur.execute( f""" INSERT INTO {TABLE} (id, content, updated_at) VALUES (%s, %s, NOW()) ON CONFLICT (id) DO UPDATE SET content = EXCLUDED.content, updated_at = NOW() """, (fname, json.dumps(content)), ) imported += 1 except Exception as e: print(f" Skip {fname}: {e}") skipped += 1
conn.commit() conn.close() print(f"Import complete: {imported} imported, {skipped} skipped")
if __name__ == "__main__": main()import_local_to_pg.pyDeleted-53