-- Charts of other people ("persons": name and birth data, with their own -- chart and analyses) next to the user's own chart. -- -- kind 'own': the user's own chart, the current one (is_primary) and the -- earlier ones replaced by a correction (kept for their analyses, not listed). -- kind 'person': someone else. Each new person uses one 'person' unit of the -- user's package (deleting does not give it back); unlimited packages and -- admins have no limit. ALTER TABLE charts ADD COLUMN kind text NOT NULL DEFAULT 'person' CHECK (kind IN ('own', 'person')); UPDATE charts SET kind = 'own' WHERE is_primary OR id IN (SELECT chart_id FROM birth_data_changes WHERE chart_id IS NOT NULL); ALTER TABLE charts ADD CONSTRAINT charts_primary_is_own CHECK (NOT is_primary OR kind = 'own'); 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', 'person')); ALTER TABLE credit_ledger DROP CONSTRAINT credit_ledger_reason_check; ALTER TABLE credit_ledger ADD CONSTRAINT credit_ledger_reason_check CHECK (reason IN ('monthly_grant', 'analysis', 'chat', 'refund', 'admin_adjust', 'purchase', 'person')); ALTER TABLE credit_ledger ADD COLUMN chart_id uuid REFERENCES charts (id) ON DELETE SET NULL; -- Lasting rights from a package, e.g. any number of persons. CREATE TABLE entitlements ( user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, entitlement text NOT NULL CHECK (entitlement IN ('unlimited_persons')), note text, created_by uuid REFERENCES users (id) ON DELETE SET NULL, created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (user_id, entitlement) );