"""0.11 — wipe the board before the site is announced, keeping the setup. Deletes orders, quotes, cards, uploads and their history; keeps operators, machines, integrations, prices, categories and the Tiny ids. Numbering is not reset: orders.number is an identity column and DELETE keeps its sequence, as the PDF requires (DTF-1…DTF-15 already exist in Tiny). The Kanban folders are written by the browser, so the server cannot discover them: pass the same root the Kanban's folder setting points at. Only the flow folders and Recibos/ are emptied; Estampas/ (the art library) stays. Dry run by default. To execute:: python -m ops.reset_board --root /srv/Kanban --yes Run inside the API container or with DATABASE_URL set. """ import argparse import shutil import sys from pathlib import Path from app.core.db import connect # Children before parents; uploads after every row that references them. TABLES = ( 'hot_parts', 'print_parts', 'print_files', 'order_files', 'movements', 'reworks', 'contact_waits', 'outbox', 'payment_intents', 'orders', 'quotes', 'upload_thumbnails', 'uploads', ) # payment_events has no foreign key to orders/quotes, but it is payment history. HISTORY = ('payment_events',) FLOW_FOLDERS = ('1. Arte recebida', '2. Arte conferida', '3. Imprimindo', '4. Revisão', '5. Finalizado', 'Recibos', 'Revisões') def counts(c): return {table: c.execute(f'SELECT count(*) AS n FROM dtf_local.{table}').fetchone()['n'] for table in TABLES + HISTORY} def clean_folders(root, dry_run=True): """Empty the flow folders under `root`, recreating them; keep Estampas/.""" root = Path(root) if not any((root / name).exists() for name in FLOW_FOLDERS): raise SystemExit(f'{root} does not look like the Kanban root: no flow folders found.') if dry_run: return [str(root / name) for name in FLOW_FOLDERS if (root / name).exists()] cleaned = [] for name in FLOW_FOLDERS: path = root / name if path.is_dir(): shutil.rmtree(path) path.mkdir(parents=True, exist_ok=True) cleaned.append(str(path)) return cleaned def main(argv=None): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--root', type=Path, help='Kanban root folder (the one with 1. Arte recebida)') parser.add_argument('--yes', action='store_true', help='execute; without it, only report') parser.add_argument('--keep-files', action='store_true', help='leave the Kanban folders untouched') args = parser.parse_args(argv) dry = not args.yes with connect() as c: before = counts(c) if dry: print('DRY RUN — nothing was deleted.') for table, number in before.items(): print(f' {table}: {number}') if args.root and not args.keep_files: print('Folders that would be emptied:') for folder in clean_folders(args.root, dry_run=True): print(f' {folder}') print('Run again with --yes to execute.') return for table in TABLES + HISTORY: c.execute(f'DELETE FROM dtf_local.{table}') c.execute('ANALYZE') after = counts(c) for table in TABLES + HISTORY: print(f'{table}: {before[table]} -> {after[table]}') if args.root and not args.keep_files: for folder in clean_folders(args.root, dry_run=False): print(f'emptied {folder}') print('Board wiped. Orders.number was not reset; the next card continues the sequence.') return 0 if __name__ == '__main__': sys.exit(main())