40 lines
2.0 KiB
SQL
40 lines
2.0 KiB
SQL
-- Yearly forecasts: one year of a chart from a solar return (birthday) to the
|
|||
|
|
-- next, calculated by the horoscope service for where the person is on the
|
||
|
|
-- birthday. The calculation is free; its detailed text analysis (kind
|
||
|
|
-- 'forecast') uses a 'forecast' unit.
|
||
|
|
CREATE TABLE forecasts (
|
||
|
|
id uuid PRIMARY KEY,
|
||
|
|
chart_id uuid NOT NULL REFERENCES charts ON DELETE CASCADE,
|
||
|
|
-- The chart it was calculated from; a corrected birth data gives a new forecast.
|
||
|
|
chart_hash text NOT NULL,
|
||
|
|
-- Calendar year of the solar return that starts it.
|
||
|
|
year integer NOT NULL,
|
||
|
|
-- {country, place, geonames_id, ...}; NULL = the birth place.
|
||
|
|
location jsonb,
|
||
|
|
location_key text NOT NULL,
|
||
|
|
result jsonb NOT NULL,
|
||
|
|
forecast_hash text NOT NULL,
|
||
|
|
created_by uuid REFERENCES users ON DELETE SET NULL,
|
||
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
||
|
|
UNIQUE (chart_id, chart_hash, year, location_key)
|
||
|
|
);
|
||
|
|
|
||
|
|
CREATE INDEX forecasts_chart_idx ON forecasts (chart_id, year DESC);
|
||
|
|
|
||
|
|
ALTER TABLE analyses ADD COLUMN forecast_id uuid REFERENCES forecasts ON DELETE CASCADE;
|
||
|
|
CREATE INDEX analyses_forecast_idx ON analyses (forecast_id, created_at DESC) WHERE forecast_id IS NOT NULL;
|
||
|
|
|
||
|
|
ALTER TABLE analyses DROP CONSTRAINT analyses_kind_check;
|
||
|
|
ALTER TABLE analyses ADD CONSTRAINT analyses_kind_check CHECK (kind IN ('natal', 'synastry', 'summary', 'forecast'));
|
||
|
|
|
||
|
|
ALTER TABLE analyses DROP CONSTRAINT analyses_target;
|
||
|
|
ALTER TABLE analyses ADD CONSTRAINT analyses_target CHECK (
|
||
|
|
(kind IN ('natal', 'summary') 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)
|
||
|
|
);
|
||
|
|
|
||
|
|
ALTER TABLE credit_ledger DROP CONSTRAINT credit_ledger_unit_check;
|
||
|
|
ALTER TABLE credit_ledger ADD CONSTRAINT credit_ledger_unit_check
|
||
|
|
CHECK (unit IN ('natal', 'synastry', 'forecast', 'person'));
|