/** @param {import('node-pg-migrate').MigrationBuilder} pgm */ exports.shorthands = undefined; exports.up = (pgm) => { pgm.sql(` ALTER TABLE orders_orders ADD COLUMN IF NOT EXISTS state_changed_at timestamptz; UPDATE orders_orders SET state_changed_at = updated_at WHERE state_changed_at IS NULL; ALTER TABLE orders_orders ALTER COLUMN state_changed_at SET DEFAULT now(), ALTER COLUMN state_changed_at SET NOT NULL; CREATE INDEX IF NOT EXISTS orders_orders_stale_state_idx ON orders_orders (state, state_changed_at) WHERE state IN ('PENDING', 'SHIPPED'); `); }; exports.down = (pgm) => { pgm.sql(` DROP INDEX IF EXISTS orders_orders_stale_state_idx; ALTER TABLE orders_orders DROP COLUMN IF EXISTS state_changed_at; `); };