Files
lincodex/backend/db/migrations/00041_create_holdings.sql

135 lines
5.1 KiB
SQL

-- +goose Up
-- Holdings group the organizations that one account founded, the way the
-- divisions of one company belong to one parent. A holding has no profile and
-- grants no right. It only names the scope of the E0 quota pool: the E0
-- organizations of one holding share one pool, so creating more organizations
-- does not multiply the unverified allowance. An organization at E1 or above
-- keeps its own pool and does not use its holding for quotas.
--
-- A holding belongs to its founder account, the account that created its first
-- organization. Every organization belongs to exactly one holding. Holdings of
-- different founders are never merged.
CREATE TABLE holdings (
-- Internal identifier, never shown by the API. The application creates it
-- as UUIDv7; the backfill below uses a random UUID.
id uuid NOT NULL,
founder_account_id uuid NOT NULL,
created_at timestamptz NOT NULL,
CONSTRAINT holdings_pkey PRIMARY KEY (id),
-- One holding per founder. A second creation of the same account that
-- races the first waits on this key and then reads the stored holding.
CONSTRAINT holdings_founder_account_id_key UNIQUE (founder_account_id),
CONSTRAINT holdings_founder_account_id_fkey FOREIGN KEY (
founder_account_id
) REFERENCES users (id) ON DELETE RESTRICT
);
-- The backfill and the NOT NULL constraint must see every organization and
-- membership. Writers of both tables wait until this migration commits.
LOCK TABLE organizations, memberships IN SHARE ROW EXCLUSIVE MODE;
-- Refuses an organization without any membership, because its founder cannot
-- be found. Creation stores the owner membership in the same transaction as
-- the organization, and no statement deletes memberships, so an existing
-- database should never reach this check.
-- +goose StatementBegin
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM organizations AS organization
WHERE NOT EXISTS (
SELECT 1
FROM memberships AS membership
WHERE membership.organization_id = organization.id
)
) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'organizations_founder_required',
TABLE = 'organizations',
MESSAGE = 'an organization without a membership has no founder for its holding';
END IF;
END;
$$;
-- +goose StatementEnd
ALTER TABLE organizations ADD COLUMN holding_id uuid;
-- The founder of an organization is the account of its earliest membership.
-- Creation inserts the organization and its owner membership in one
-- transaction, and a membership cannot exist before its organization, so the
-- earliest membership is the creator's. Ties are broken by identifier. A holding
-- is dated by the earliest creation of its founder.
WITH founders AS (
SELECT DISTINCT ON (membership.organization_id)
membership.organization_id,
membership.user_id
FROM memberships AS membership
ORDER BY membership.organization_id, membership.created_at, membership.id
)
INSERT INTO holdings (id, founder_account_id, created_at)
SELECT
gen_random_uuid(),
founders.user_id,
min(organization.created_at)
FROM founders
INNER JOIN organizations AS organization ON organization.id = founders.organization_id
GROUP BY founders.user_id;
WITH founders AS (
SELECT DISTINCT ON (membership.organization_id)
membership.organization_id,
membership.user_id
FROM memberships AS membership
ORDER BY membership.organization_id, membership.created_at, membership.id
)
UPDATE organizations AS organization
SET holding_id = holding.id
FROM founders
INNER JOIN holdings AS holding ON holding.founder_account_id = founders.user_id
WHERE founders.organization_id = organization.id;
ALTER TABLE organizations ALTER COLUMN holding_id SET NOT NULL;
ALTER TABLE organizations
ADD CONSTRAINT organizations_holding_id_fkey FOREIGN KEY (holding_id)
REFERENCES holdings (id) ON DELETE RESTRICT;
-- Serves the pool of an E0 organization, which reads the organizations of its
-- holding, and the foreign key.
CREATE INDEX organizations_holding_id_idx ON organizations (holding_id);
-- +goose Down
-- A rollback removes the grouping of organizations into holdings. It refuses
-- while a holding groups several organizations, because the schema without
-- holdings would count their E0 quotas separately and could not keep the pool
-- they share. The lock stops a concurrent creation from joining a holding
-- between the check and the removal.
LOCK TABLE organizations IN SHARE ROW EXCLUSIVE MODE;
-- +goose StatementBegin
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM organizations
GROUP BY holding_id
HAVING count(*) > 1
) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'organizations_holding_grouped',
TABLE = 'organizations',
MESSAGE = 'holdings with several organizations must be resolved before rollback';
END IF;
END;
$$;
-- +goose StatementEnd
ALTER TABLE organizations DROP COLUMN holding_id;
DROP TABLE holdings;