Files
sorsfonalak/crates/db/migrations/0047_weekly_horoscope.sql
T

38 lines
2.0 KiB
SQL
Raw Normal View History

-- The free weekly horoscope: the week's sky on a member's own chart (from
-- the horoscope service), its text (an analysis of kind 'weekly', written by
-- the summary's model), and the Monday e-mail that says it is there (on for
-- everyone, the members of today too; it can be turned off).
CREATE TABLE weekly_skies (
chart_id uuid NOT NULL REFERENCES charts (id) ON DELETE CASCADE,
-- The chart's data when it was calculated: new birth data, new week sky.
chart_hash text NOT NULL,
-- The Monday the week starts on (Hungarian time).
week_start date NOT NULL,
result jsonb NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (chart_id, chart_hash, week_start)
);
ALTER TABLE analyses ADD COLUMN week_start date;
ALTER TABLE analyses DROP CONSTRAINT analyses_kind_check;
ALTER TABLE analyses ADD CONSTRAINT analyses_kind_check
CHECK (kind IN ('natal', 'synastry', 'summary', 'forecast', 'tarot', 'celebrity', 'weekly'));
ALTER TABLE analyses DROP CONSTRAINT analyses_target;
ALTER TABLE analyses ADD CONSTRAINT analyses_target CHECK (
(kind IN ('natal', 'summary', 'celebrity') AND chart_id IS NOT NULL AND synastry_id IS NULL AND forecast_id IS NULL)
OR (kind = 'synastry' AND synastry_id IS NOT NULL AND chart_id IS NULL AND forecast_id IS NULL)
OR (kind = 'forecast' AND forecast_id IS NOT NULL AND chart_id IS NULL AND synastry_id IS NULL)
OR (kind = 'tarot' AND tarot IS NOT NULL AND chart_id IS NULL AND synastry_id IS NULL AND forecast_id IS NULL)
OR (kind = 'weekly' AND chart_id IS NOT NULL AND week_start IS NOT NULL AND synastry_id IS NULL AND forecast_id IS NULL)
);
-- The Monday e-mail: on unless the member turns it off. The token opens the
-- page of every e-mail setting from an e-mail's footer (no login needed).
ALTER TABLE users ADD COLUMN weekly_email boolean NOT NULL DEFAULT true;
ALTER TABLE users ADD COLUMN weekly_email_sent_on date;
ALTER TABLE users ADD COLUMN email_token uuid NOT NULL DEFAULT gen_random_uuid();
CREATE UNIQUE INDEX users_email_token ON users (email_token);