From 33d466ed734b021d8a8edb7f25b1a4345a16bd19 Mon Sep 17 00:00:00 2001 From: jtricerolph Date: Sun, 5 Jul 2026 14:07:01 +0000 Subject: [PATCH] Fix newbook_current_rates column names + rollback on failed date MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - schema defined gross_rate/net_rate but every query (fetch job, bookability, competitors) uses rate_gross/rate_net — rename the columns, with an idempotent DO-block migration for existing tables - roll back the session when a date fails so one bad statement no longer poisons the whole sync run (every subsequent write was dying with 'current transaction is aborted') Co-Authored-By: Claude Fable 5 --- backend/api/analysis.py | 2 +- backend/jobs/fetch_current_rates.py | 3 +++ backend/schema.sql | 14 ++++++++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/backend/api/analysis.py b/backend/api/analysis.py index 3a675ab..496108d 100644 --- a/backend/api/analysis.py +++ b/backend/api/analysis.py @@ -219,7 +219,7 @@ async def rate_comparison( # Own hotel avg rate per date (across included categories) own_result = await db.execute( text(""" - SELECT rate_date, AVG(gross_rate) AS own_rate + SELECT rate_date, AVG(rate_gross) AS own_rate FROM newbook_current_rates WHERE rate_date BETWEEN :from_date AND :to_date GROUP BY rate_date diff --git a/backend/jobs/fetch_current_rates.py b/backend/jobs/fetch_current_rates.py index 8c7d5ff..e43d135 100644 --- a/backend/jobs/fetch_current_rates.py +++ b/backend/jobs/fetch_current_rates.py @@ -342,6 +342,9 @@ async def run_fetch_current_rates(horizon_days: int = 720, start_date: date = No except Exception as e: logger.warning(f"Failed to fetch rates for {current_date}: {e}") + # A failed statement aborts the transaction — roll back so + # subsequent dates in this run can still be written + db.rollback() # Step 5: Commit periodically if day_count % COMMIT_BATCH_SIZE == 0: diff --git a/backend/schema.sql b/backend/schema.sql index 71cec0e..724bde6 100644 --- a/backend/schema.sql +++ b/backend/schema.sql @@ -158,13 +158,23 @@ CREATE TABLE IF NOT EXISTS newbook_current_rates ( id SERIAL PRIMARY KEY, category_id VARCHAR(50) NOT NULL, rate_date DATE NOT NULL, - gross_rate DECIMAL(12,2), - net_rate DECIMAL(12,2), + rate_gross DECIMAL(12,2), + rate_net DECIMAL(12,2), tariffs_data JSONB DEFAULT '{}', valid_from TIMESTAMP DEFAULT NOW(), last_verified_at TIMESTAMP DEFAULT NOW() ); +-- Migrate tables created before the gross_rate/net_rate → rate_gross/rate_net rename +DO $$ +BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns + WHERE table_name = 'newbook_current_rates' AND column_name = 'gross_rate') THEN + ALTER TABLE newbook_current_rates RENAME COLUMN gross_rate TO rate_gross; + ALTER TABLE newbook_current_rates RENAME COLUMN net_rate TO rate_net; + END IF; +END $$; + CREATE INDEX IF NOT EXISTS idx_current_rates_date ON newbook_current_rates(rate_date); CREATE INDEX IF NOT EXISTS idx_current_rates_category ON newbook_current_rates(category_id); CREATE INDEX IF NOT EXISTS idx_current_rates_tariffs ON newbook_current_rates USING gin(tariffs_data);