0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

refactor(schemas): skip adding membership when no user found

This commit is contained in:
Gao Sun 2023-12-16 14:48:01 +08:00
parent 74e5975be5
commit 36139d7002
No known key found for this signature in database
GPG key ID: 13EBE123E4773688
2 changed files with 42 additions and 35 deletions

View file

@ -5,7 +5,6 @@ on:
push:
branches:
- master
- "push-action/**"
pull_request:
concurrency:
@ -19,8 +18,7 @@ jobs:
has-alteration-changes: ${{ steps.changes-detection.outputs.has-alteration-changes }}
steps:
- name: checkout head
uses: actions/checkout@v4
- uses: actions/checkout@v4
with:
fetch-depth: 0

View file

@ -82,7 +82,6 @@ const alteration: AlterationScript = {
)};
`);
consoleLog.info('Add membership records and assign organization roles to existing users');
const usersRoles = await transaction.any<{ userId: string; roleName: string }>(sql`
select
public.users.id as "userId",
@ -94,37 +93,47 @@ const alteration: AlterationScript = {
and public.roles.name like '%:admin';
`);
// Add membership records
await transaction.query(sql`
insert into public.organization_user_relations (tenant_id, organization_id, user_id)
values
${sql.join(
usersRoles.map(
(userRole) =>
sql`(${adminTenantId}, ${`t-${userRole.roleName.slice(0, -6)}`}, ${userRole.userId})`
),
sql`, `
)};
`);
// We treat all existing users as the owner of the tenant
await transaction.query(sql`
insert into public.organization_role_user_relations (tenant_id, organization_id, user_id, organization_role_id)
values
${sql.join(
usersRoles.map(
(userRole) =>
sql`
(
${adminTenantId},
${`t-${userRole.roleName.slice(0, -6)}`},
${userRole.userId},
'owner'
)
`
),
sql`, `
)};
`);
if (usersRoles.length === 0) {
consoleLog.warn(
'No existing admin users found, skip adding membership records for tenant organizations.'
);
} else {
consoleLog.info('Add membership records and assign organization roles to existing users');
// Add membership records
await transaction.query(sql`
insert into public.organization_user_relations (tenant_id, organization_id, user_id)
values
${sql.join(
usersRoles.map(
(userRole) =>
sql`(${adminTenantId}, ${`t-${userRole.roleName.slice(0, -6)}`}, ${
userRole.userId
})`
),
sql`, `
)};
`);
// We treat all existing users as the owner of the tenant
await transaction.query(sql`
insert into public.organization_role_user_relations (tenant_id, organization_id, user_id, organization_role_id)
values
${sql.join(
usersRoles.map(
(userRole) =>
sql`
(
${adminTenantId},
${`t-${userRole.roleName.slice(0, -6)}`},
${userRole.userId},
'owner'
)
`
),
sql`, `
)};
`);
}
consoleLog.info('Create machine-to-machine Management API role for each tenant');
await transaction.query(sql`