0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-13 21:30:30 -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: push:
branches: branches:
- master - master
- "push-action/**"
pull_request: pull_request:
concurrency: concurrency:
@ -19,8 +18,7 @@ jobs:
has-alteration-changes: ${{ steps.changes-detection.outputs.has-alteration-changes }} has-alteration-changes: ${{ steps.changes-detection.outputs.has-alteration-changes }}
steps: steps:
- name: checkout head - uses: actions/checkout@v4
uses: actions/checkout@v4
with: with:
fetch-depth: 0 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` const usersRoles = await transaction.any<{ userId: string; roleName: string }>(sql`
select select
public.users.id as "userId", public.users.id as "userId",
@ -94,37 +93,47 @@ const alteration: AlterationScript = {
and public.roles.name like '%:admin'; and public.roles.name like '%:admin';
`); `);
// Add membership records if (usersRoles.length === 0) {
await transaction.query(sql` consoleLog.warn(
insert into public.organization_user_relations (tenant_id, organization_id, user_id) 'No existing admin users found, skip adding membership records for tenant organizations.'
values );
${sql.join( } else {
usersRoles.map( consoleLog.info('Add membership records and assign organization roles to existing users');
(userRole) =>
sql`(${adminTenantId}, ${`t-${userRole.roleName.slice(0, -6)}`}, ${userRole.userId})` // Add membership records
), await transaction.query(sql`
sql`, ` insert into public.organization_user_relations (tenant_id, organization_id, user_id)
)}; values
`); ${sql.join(
// We treat all existing users as the owner of the tenant usersRoles.map(
await transaction.query(sql` (userRole) =>
insert into public.organization_role_user_relations (tenant_id, organization_id, user_id, organization_role_id) sql`(${adminTenantId}, ${`t-${userRole.roleName.slice(0, -6)}`}, ${
values userRole.userId
${sql.join( })`
usersRoles.map( ),
(userRole) => sql`, `
sql` )};
( `);
${adminTenantId}, // We treat all existing users as the owner of the tenant
${`t-${userRole.roleName.slice(0, -6)}`}, await transaction.query(sql`
${userRole.userId}, insert into public.organization_role_user_relations (tenant_id, organization_id, user_id, organization_role_id)
'owner' values
) ${sql.join(
` usersRoles.map(
), (userRole) =>
sql`, ` sql`
)}; (
`); ${adminTenantId},
${`t-${userRole.roleName.slice(0, -6)}`},
${userRole.userId},
'owner'
)
`
),
sql`, `
)};
`);
}
consoleLog.info('Create machine-to-machine Management API role for each tenant'); consoleLog.info('Create machine-to-machine Management API role for each tenant');
await transaction.query(sql` await transaction.query(sql`