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:
jtricerolph 2026-07-05 14:07:01 +00:00
parent 4977768645
commit 33d466ed73
3 changed files with 16 additions and 3 deletions

View file

@ -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);