exports.up = (pgm) => { pgm.sql(` CREATE TABLE IF NOT EXISTS favorites ( customer_id INTEGER NOT NULL REFERENCES customers(id) ON DELETE CASCADE, item_id INTEGER NOT NULL REFERENCES items(id) ON DELETE CASCADE, created_at TIMESTAMPTZ NOT NULL DEFAULT now(), PRIMARY KEY (customer_id, item_id) ); -- The primary key covers lookups by customer. Notifying everyone who -- favorited an item that just sold goes the other way. CREATE INDEX IF NOT EXISTS favorites_item_id_idx ON favorites (item_id); -- A consent of its own rather than reusing marketing_consent. Being told -- that a specific item you asked about has gone is a narrower thing than -- agreeing to marketing, and folding one into the other would make the -- existing consent record no longer describe what was agreed to. -- -- Mirrors the marketing consent columns: the flag, when it was given, and -- the exact wording shown at the time. ALTER TABLE customers ADD COLUMN IF NOT EXISTS favorite_alerts BOOLEAN NOT NULL DEFAULT false; ALTER TABLE customers ADD COLUMN IF NOT EXISTS favorite_alerts_at TIMESTAMPTZ; ALTER TABLE customers ADD COLUMN IF NOT EXISTS favorite_alerts_text TEXT; `); }; exports.down = (pgm) => { pgm.sql(` ALTER TABLE customers DROP COLUMN IF EXISTS favorite_alerts_text; ALTER TABLE customers DROP COLUMN IF EXISTS favorite_alerts_at; ALTER TABLE customers DROP COLUMN IF EXISTS favorite_alerts; DROP TABLE IF EXISTS favorites; `); };