Reimplement invitations

Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-02-17 00:26:22 +04:00
parent a42fc4fa61
commit 47aba96b69
43 changed files with 1651 additions and 4550 deletions

View File

@@ -49,3 +49,152 @@ ALTER TABLE
iam_scim_events
ALTER COLUMN
user_name DROP DEFAULT;
-- Convert invitations to identities / profiles / memberships
-- Create missing identities (one row per email to avoid "cannot affect row a second time")
INSERT INTO
identities (
id,
created_at,
updated_at,
email_address,
email_address_verified,
full_name
)
SELECT
generate_gid('\x0000000000000000' :: bytea, 11),
NOW(),
NOW(),
i.email,
FALSE,
i.full_name
FROM
(
SELECT
DISTINCT ON (email) email,
full_name
FROM
iam_invitations
WHERE
accepted_at IS NULL
ORDER BY
email
) i ON CONFLICT (email_address) DO
UPDATE
SET
full_name = EXCLUDED.full_name;
-- Create missing profiles
WITH invitation_identities AS (
SELECT
i.id AS identity_id,
inv.tenant_id AS tenant_id,
inv.organization_id AS organization_id,
inv.full_name AS full_name
FROM
iam_invitations inv
INNER JOIN identities i ON i.email_address = inv.email
WHERE
inv.accepted_at IS NULL
)
INSERT INTO
iam_membership_profiles (
id,
tenant_id,
identity_id,
organization_id,
full_name,
kind,
additional_email_addresses,
source,
state,
created_at,
updated_at
)
SELECT
generate_gid(decode_base64_unpadded(ii.tenant_id), 51),
ii.tenant_id,
ii.identity_id,
ii.organization_id,
ii.full_name,
'EMPLOYEE',
'{}' :: CITEXT [],
'MANUAL',
'INACTIVE',
NOW(),
NOW()
FROM
invitation_identities ii ON CONFLICT DO NOTHING;
-- Create missing memberships
WITH invitation_identities AS (
SELECT
i.id AS identity_id,
inv.tenant_id AS tenant_id,
inv.organization_id AS organization_id,
inv.role AS role
FROM
iam_invitations inv
INNER JOIN identities i ON i.email_address = inv.email
WHERE
inv.accepted_at IS NULL
)
INSERT INTO
iam_memberships (
id,
tenant_id,
identity_id,
organization_id,
role,
created_at,
updated_at
)
SELECT
generate_gid(decode_base64_unpadded(ii.tenant_id), 39),
ii.tenant_id,
ii.identity_id,
ii.organization_id,
ii.role,
NOW(),
NOW()
FROM
invitation_identities ii ON CONFLICT DO NOTHING;
ALTER TABLE
iam_invitations
ADD
COLUMN user_id TEXT REFERENCES iam_membership_profiles(id);
WITH profile_identities AS (
SELECT
p.id AS profile_id,
i.email_address,
p.organization_id
FROM
iam_membership_profiles p
INNER JOIN identities i ON i.id = p.identity_id
)
UPDATE
iam_invitations i
SET
user_id = pi.profile_id
FROM
profile_identities pi
WHERE
pi.organization_id = i.organization_id
AND pi.email_address = i.email;
ALTER TABLE
iam_invitations
ALTER COLUMN
user_id
SET
NOT NULL,
ALTER COLUMN
email DROP NOT NULL,
ALTER COLUMN
role TYPE TEXT USING role :: text,
ALTER COLUMN
role DROP NOT NULL,
ALTER COLUMN
full_name DROP NOT NULL;