Some checks failed
Run Tests / test (push) Failing after 5m2s
- Introduced a new template for currency overview and management (`currencies.html`). - Updated footer to include attribution to AllYouCanGET. - Added "Currencies" link to the main navigation header. - Implemented end-to-end tests for currency creation, update, and activation toggling. - Created unit tests for currency API endpoints, including creation, updating, and activation toggling. - Added a fixture to seed default currencies for testing. - Enhanced database setup tests to ensure proper seeding and migration handling.
3.6 KiB
3.6 KiB
Consolidated Migration Baseline Plan
This note outlines the content and structure of the planned baseline migration (scripts/migrations/000_base.sql). The objective is to capture the currently required schema changes in a single idempotent script so that fresh environments only need to apply one SQL file before proceeding with incremental migrations.
Guiding Principles
- Idempotent DDL: Every
CREATEorALTERstatement must tolerate repeated execution. UseIF NOT EXISTSguards or existence checks (information_schema) where necessary. - Order of Operations: Create reference tables first, then update dependent tables, finally enforce foreign keys and constraints.
- Data Safety: Default data seeded by migrations should be minimal and in ASCII-only form to avoid encoding issues in various shells and CI logs.
- Compatibility: The baseline must reflect the schema shape expected by the current SQLAlchemy models, API routes, and seeding scripts.
Schema Elements to Include
1. currency Table
- Columns:
id SERIAL PRIMARY KEY,code VARCHAR(3) UNIQUE NOT NULL,name VARCHAR(128) NOT NULL,symbol VARCHAR(8),is_active BOOLEAN NOT NULL DEFAULT TRUE. - Index: implicit via unique constraint on
code. - Seed rows matching
scripts.seed_data.CURRENCY_SEEDS(ASCII-only symbols such asUSD$,CAD$). - Upsert logic using
ON CONFLICT (code) DO UPDATEto keep names/symbols in sync when rerun.
2. Currency Integration for CAPEX/OPEX
- Add
currency_id INTEGERcolumns withIF NOT EXISTSguards. - Populate
currency_idfrom legacycurrency_codeif the column exists. - Default null
currency_idvalues to the USD row, thenALTERtoSET NOT NULL. - Create
fk_capex_currencyandfk_opex_currencyconstraints withON DELETE RESTRICTsemantics. - Drop legacy
currency_codecolumn if it exists (safe because new column holds data).
3. Measurement Metadata on Consumption/Production
- Ensure
consumptionandproduction_outputtables haveunit_name VARCHAR(64)andunit_symbol VARCHAR(16)columns withIF NOT EXISTSguards.
4. measurement_unit Reference Table
- Columns:
id SERIAL PRIMARY KEY,code VARCHAR(64) UNIQUE NOT NULL,name VARCHAR(128) NOT NULL,symbol VARCHAR(16),unit_type VARCHAR(32) NOT NULL,is_active BOOLEAN NOT NULL DEFAULT TRUE,created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(). - Assume a simple trigger to maintain
updated_atis deferred: automate via application layer later; for now, omit trigger. - Seed rows matching
MEASUREMENT_UNIT_SEEDS(ASCII names/symbols). UseON CONFLICT (code) DO UPDATEto keep descriptive fields aligned.
5. Transaction Handling
- Wrap the main operations in a single
BEGIN; ... COMMIT;block. - Use subtransactions (
DO $$ ... $$;) only where conditional logic is required (e.g., checking column existence before backfill).
Migration Tracking Alignment
- Baseline file will be named
000_base.sql. After execution, insert a row intoschema_migrationswith filename000_base.sqlto keep the tracking table aligned. - Existing migrations (
20251021_add_currency_and_unit_fields.sql,20251022_create_currency_table_and_fks.sql) remain for historical reference but will no longer be applied to new environments once the baseline is present.
Next Steps
- Draft
000_base.sqlreflecting the steps above. - Update
run_migrationsto recognise the baseline file and mark older migrations as applied when the baseline exists. - Provide documentation in
docs/quickstart.mdexplaining how to reset an environment using the baseline plus seeds.