# Setup Script Idempotency Audit (2025-10-25) This note captures the current evaluation of idempotent behaviour for `scripts/setup_database.py` and outlines follow-up actions. ## Admin Tasks - **ensure_database**: guarded by `SELECT 1 FROM pg_database`; re-runs safely. Failure mode: network issues or lack of privileges surface as psycopg2 errors without additional context. - **ensure_role**: checks `pg_roles`, creates role if missing, reapplies grants each time. Subsequent runs execute grants again but PostgreSQL tolerates repeated grants. - **ensure_schema**: uses `information_schema` guard and respects `--dry-run`; idempotent when schema is `public` or already present. ## Application Tasks - **initialize_schema**: relies on SQLAlchemy `create_all(checkfirst=True)`; repeatable. Dry-run output remains descriptive. - **run_migrations**: new baseline workflow applies `000_base.sql` once and records legacy scripts as applied. Subsequent runs detect the baseline in `schema_migrations` and skip reapplication. ## Seeding - `seed_baseline_data` seeds currencies and measurement units with upsert logic. Verification now raises on missing data, preventing silent failures. - Running `--seed-data` repeatedly performs `ON CONFLICT` updates, making the operation safe. ## Outstanding Risks 1. Baseline migration relies on legacy files being present when first executed; if removed beforehand, old entries are never marked. (Low risk given repository state.) 2. `ensure_database` and `ensure_role` do not wrap SQL execution errors with additional context beyond psycopg2 messages. 3. Baseline verification assumes migrations and seeding run in the same process; manual runs of `scripts/seed_data.py` without the baseline could still fail. ## Recommended Actions - Add regression tests ensuring repeated executions of key CLI paths (`--run-migrations`, `--seed-data`) result in no-op behaviour after the first run. - Extend logging/error handling for admin operations to provide clearer messages on repeated failures. - Consider a preflight check when migrations directory lacks legacy files but baseline is pending, warning about potential drift.