--up
CREATE TABLE plan_overrides (
    id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    "plan_id" integer REFERENCES plans (id) ON UPDATE RESTRICT ON DELETE CASCADE NOT NULL,
    "email" varchar(255) NOT NULL,
    "expires_at" timestamp NOT NULL,
    "enabled" boolean NOT NULL DEFAULT TRUE,
    "note" text,
    "created_at" timestamp NOT NULL DEFAULT NOW(),
    "updated_at" timestamp NOT NULL DEFAULT NOW()
);

CREATE INDEX plan_override_email_index ON plan_overrides (email);

CREATE TRIGGER set_timestamp
BEFORE UPDATE ON plan_overrides FOR EACH ROW
EXECUTE PROCEDURE update_updated_at_column();

--down

DROP TABLE plan_overrides;
