From 025ab3072619414c4d835c0c607184f84133e092 Mon Sep 17 00:00:00 2001 From: Eric Gullickson <16152721+ericgullickson@users.noreply.github.com> Date: Tue, 13 Jan 2026 21:51:44 -0600 Subject: [PATCH] fix: add schema migration for ownership_costs table (refs #29) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The ownership_costs table was created with an outdated schema that had different column names (start_date/end_date vs period_start/period_end) and was missing the notes column. This migration aligns the database schema with the current code expectations. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- .../003_alter_ownership_costs_schema.sql | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 backend/src/features/ownership-costs/migrations/003_alter_ownership_costs_schema.sql diff --git a/backend/src/features/ownership-costs/migrations/003_alter_ownership_costs_schema.sql b/backend/src/features/ownership-costs/migrations/003_alter_ownership_costs_schema.sql new file mode 100644 index 0000000..9ce49c5 --- /dev/null +++ b/backend/src/features/ownership-costs/migrations/003_alter_ownership_costs_schema.sql @@ -0,0 +1,33 @@ +-- Migration: Alter ownership_costs table to match updated schema +-- Issue: #29 +-- Description: Fix schema mismatch - rename columns, add notes, update constraints + +-- Rename columns to match code expectations +ALTER TABLE ownership_costs RENAME COLUMN start_date TO period_start; +ALTER TABLE ownership_costs RENAME COLUMN end_date TO period_end; + +-- Make period_start nullable (was NOT NULL as start_date) +ALTER TABLE ownership_costs ALTER COLUMN period_start DROP NOT NULL; + +-- Drop interval column (no longer used in new schema) +ALTER TABLE ownership_costs DROP COLUMN IF EXISTS interval; + +-- Add notes column +ALTER TABLE ownership_costs ADD COLUMN IF NOT EXISTS notes TEXT; + +-- Update description column type to match new schema +ALTER TABLE ownership_costs ALTER COLUMN description TYPE VARCHAR(200); + +-- Drop old check constraints +ALTER TABLE ownership_costs DROP CONSTRAINT IF EXISTS chk_ownership_costs_type; +ALTER TABLE ownership_costs DROP CONSTRAINT IF EXISTS chk_ownership_costs_amount_non_negative; +ALTER TABLE ownership_costs DROP CONSTRAINT IF EXISTS chk_ownership_costs_date_range; +ALTER TABLE ownership_costs DROP CONSTRAINT IF EXISTS chk_ownership_costs_interval; + +-- Update cost_type column to VARCHAR(32) and add new constraint with additional types +ALTER TABLE ownership_costs ALTER COLUMN cost_type TYPE VARCHAR(32); +ALTER TABLE ownership_costs ADD CONSTRAINT ownership_costs_cost_type_check + CHECK (cost_type IN ('insurance', 'registration', 'tax', 'inspection', 'parking', 'other')); + +-- Add amount constraint (amount > 0) +ALTER TABLE ownership_costs ADD CONSTRAINT ownership_costs_amount_check CHECK (amount > 0);