Fix newbook_current_rates column names + rollback on failed date
- 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 <noreply@anthropic.com>
This commit is contained in:
parent
4977768645
commit
33d466ed73
3 changed files with 16 additions and 3 deletions
|
|
@ -219,7 +219,7 @@ async def rate_comparison(
|
||||||
# Own hotel avg rate per date (across included categories)
|
# Own hotel avg rate per date (across included categories)
|
||||||
own_result = await db.execute(
|
own_result = await db.execute(
|
||||||
text("""
|
text("""
|
||||||
SELECT rate_date, AVG(gross_rate) AS own_rate
|
SELECT rate_date, AVG(rate_gross) AS own_rate
|
||||||
FROM newbook_current_rates
|
FROM newbook_current_rates
|
||||||
WHERE rate_date BETWEEN :from_date AND :to_date
|
WHERE rate_date BETWEEN :from_date AND :to_date
|
||||||
GROUP BY rate_date
|
GROUP BY rate_date
|
||||||
|
|
|
||||||
|
|
@ -342,6 +342,9 @@ async def run_fetch_current_rates(horizon_days: int = 720, start_date: date = No
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"Failed to fetch rates for {current_date}: {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
|
# Step 5: Commit periodically
|
||||||
if day_count % COMMIT_BATCH_SIZE == 0:
|
if day_count % COMMIT_BATCH_SIZE == 0:
|
||||||
|
|
|
||||||
|
|
@ -158,13 +158,23 @@ CREATE TABLE IF NOT EXISTS newbook_current_rates (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
category_id VARCHAR(50) NOT NULL,
|
category_id VARCHAR(50) NOT NULL,
|
||||||
rate_date DATE NOT NULL,
|
rate_date DATE NOT NULL,
|
||||||
gross_rate DECIMAL(12,2),
|
rate_gross DECIMAL(12,2),
|
||||||
net_rate DECIMAL(12,2),
|
rate_net DECIMAL(12,2),
|
||||||
tariffs_data JSONB DEFAULT '{}',
|
tariffs_data JSONB DEFAULT '{}',
|
||||||
valid_from TIMESTAMP DEFAULT NOW(),
|
valid_from TIMESTAMP DEFAULT NOW(),
|
||||||
last_verified_at 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_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_category ON newbook_current_rates(category_id);
|
||||||
CREATE INDEX IF NOT EXISTS idx_current_rates_tariffs ON newbook_current_rates USING gin(tariffs_data);
|
CREATE INDEX IF NOT EXISTS idx_current_rates_tariffs ON newbook_current_rates USING gin(tariffs_data);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue