From f1d8466919e1ed6b8b538b64523615babf755c84 Mon Sep 17 00:00:00 2001 From: Darcy Ye Date: Fri, 26 May 2023 17:38:52 +0800 Subject: [PATCH] chore(cli,cloud,schemas): change default tenant name (#3890) --- .../cli/src/commands/database/seed/tenant.ts | 6 ++--- packages/cloud/src/libraries/tenants.ts | 8 +++--- packages/cloud/src/queries/tenants.ts | 4 +-- ...d-name-tag-created-at-for-tenants-table.ts | 25 +------------------ packages/schemas/src/models/tenants.ts | 2 +- packages/schemas/src/seeds/tenant.ts | 4 ++- packages/schemas/tables/_after_all.sql | 6 ----- packages/schemas/tables/_functions.sql | 6 ----- 8 files changed, 14 insertions(+), 47 deletions(-) diff --git a/packages/cli/src/commands/database/seed/tenant.ts b/packages/cli/src/commands/database/seed/tenant.ts index b1a3e14a6..0f7830ec7 100644 --- a/packages/cli/src/commands/database/seed/tenant.ts +++ b/packages/cli/src/commands/database/seed/tenant.ts @@ -1,6 +1,6 @@ import { createTenantMetadata } from '@logto/core-kit'; import type { - TenantModel, + CreateTenant, AdminData, UpdateAdminData, CreateScope, @@ -18,9 +18,9 @@ import { getDatabaseName } from '../../../queries/database.js'; export const createTenant = async (pool: CommonQueryMethods, tenantId: string) => { const database = await getDatabaseName(pool, true); const { parentRole, role, password } = createTenantMetadata(database, tenantId); - const tenantModel: TenantModel = { id: tenantId, dbUser: role, dbUserPassword: password }; + const createTenant: CreateTenant = { id: tenantId, dbUser: role, dbUserPassword: password }; - await pool.query(insertInto(tenantModel, 'tenants')); + await pool.query(insertInto(createTenant, 'tenants')); await pool.query(sql` create role ${sql.identifier([role])} with inherit login password '${raw(password)}' diff --git a/packages/cloud/src/libraries/tenants.ts b/packages/cloud/src/libraries/tenants.ts index f04be5fda..779c86760 100644 --- a/packages/cloud/src/libraries/tenants.ts +++ b/packages/cloud/src/libraries/tenants.ts @@ -4,7 +4,7 @@ import { } from '@logto/cli/lib/commands/database/utils.js'; import { DemoConnector } from '@logto/connector-kit'; import { createTenantMetadata } from '@logto/core-kit'; -import type { LogtoOidcConfigType, TenantInfo, TenantModel } from '@logto/schemas'; +import type { LogtoOidcConfigType, TenantInfo, CreateTenant } from '@logto/schemas'; import { createAdminTenantApplicationRole, AdminTenantRole, @@ -70,7 +70,7 @@ export class TenantsLibrary { const { id: tenantId, parentRole, role, password } = createTenantMetadata(databaseName); // Init tenant - const tenantModel: TenantModel = { id: tenantId, dbUser: role, dbUserPassword: password }; + const createTenant: CreateTenant = { id: tenantId, dbUser: role, dbUserPassword: password }; const transaction = await this.queries.client.transaction(); const tenants = createTenantsQueries(transaction); const users = createUsersQueries(transaction); @@ -83,7 +83,7 @@ export class TenantsLibrary { await transaction.start(); // Init tenant - await tenants.insertTenant(tenantModel); + await tenants.insertTenant(createTenant); await tenants.createTenantRole(parentRole, role, password); // Create admin data set (resource, roles, etc.) @@ -162,7 +162,7 @@ export class TenantsLibrary { // Update Redirect URI for Admin Console await tenants.appendAdminConsoleRedirectUris( - ...cloudUrlSet.deduplicated().map((url) => appendPath(url, tenantModel.id, 'callback')) + ...cloudUrlSet.deduplicated().map((url) => appendPath(url, createTenant.id, 'callback')) ); await transaction.end(); diff --git a/packages/cloud/src/queries/tenants.ts b/packages/cloud/src/queries/tenants.ts index 4cca6eb1d..d7ba05519 100644 --- a/packages/cloud/src/queries/tenants.ts +++ b/packages/cloud/src/queries/tenants.ts @@ -6,7 +6,7 @@ import { getManagementApiResourceIndicator, PredefinedScope, } from '@logto/schemas'; -import type { AdminData, TenantModel, CreateRolesScope } from '@logto/schemas'; +import type { AdminData, CreateTenant, CreateRolesScope } from '@logto/schemas'; import { generateStandardId } from '@logto/shared'; import type { PostgreSql } from '@withtyped/postgres'; import { jsonb, dangerousRaw, id, sql } from '@withtyped/postgres'; @@ -34,7 +34,7 @@ export const createTenantsQueries = (client: Queryable) => { where roles.tenant_id = ${adminTenantId}; `); - const insertTenant = async (tenant: TenantModel) => client.query(insertInto(tenant, 'tenants')); + const insertTenant = async (tenant: CreateTenant) => client.query(insertInto(tenant, 'tenants')); const createTenantRole = async (parentRole: string, role: string, password: string) => client.query(sql` diff --git a/packages/schemas/alterations/next-1684382842-add-name-tag-created-at-for-tenants-table.ts b/packages/schemas/alterations/next-1684382842-add-name-tag-created-at-for-tenants-table.ts index 1b8284ccf..96e406125 100644 --- a/packages/schemas/alterations/next-1684382842-add-name-tag-created-at-for-tenants-table.ts +++ b/packages/schemas/alterations/next-1684382842-add-name-tag-created-at-for-tenants-table.ts @@ -6,36 +6,13 @@ const alteration: AlterationScript = { up: async (pool) => { // Add new tenant columns for name, tag, and created_at. await pool.query(sql` - alter table tenants add column name varchar(128); + alter table tenants add column name varchar(128) not null default 'My Project'; alter table tenants add column tag varchar(64) not null default 'development'; alter table tenants add column created_at timestamptz not null default(now()); `); - // Manually set the name for existing tenants since the trigger is for new tenants only. - await pool.query(sql` - update tenants set name = concat('tenant_', id); - `); - await pool.query(sql` - alter table tenants alter column name set not null; - `); - // Create a trigger to set the tenant name since column reference is not available as default value. - await pool.query(sql` - create function set_tenant_name() returns trigger as - $$ begin - new.name := concat('tenant_', new.id); - return new; - end; $$ language plpgsql; - `); - await pool.query(sql` - create trigger set_tenant_name_trigger - before insert on tenants - for each row when (new.name is null) - execute procedure set_tenant_name(); - `); }, down: async (pool) => { await pool.query(sql` - drop trigger set_tenant_name_trigger on tenants; - drop function set_tenant_name; alter table tenants drop column name; alter table tenants drop column tag; alter table tenants drop column created_at; diff --git a/packages/schemas/src/models/tenants.ts b/packages/schemas/src/models/tenants.ts index 8d8a61bf9..2c5fb771f 100644 --- a/packages/schemas/src/models/tenants.ts +++ b/packages/schemas/src/models/tenants.ts @@ -8,7 +8,7 @@ export const Tenants = createModel(/* sql */ ` id varchar(21) not null, db_user varchar(128), db_user_password varchar(128), - name varchar(128) not null, + name varchar(128) not null default 'My Project', tag varchar(64) not null default '${TenantTag.Development}', created_at timestamptz not null default(now()), primary key (id), diff --git a/packages/schemas/src/seeds/tenant.ts b/packages/schemas/src/seeds/tenant.ts index 85847fee5..8eb4389b1 100644 --- a/packages/schemas/src/seeds/tenant.ts +++ b/packages/schemas/src/seeds/tenant.ts @@ -9,4 +9,6 @@ export const adminTenantId = 'admin'; * `createModel` from @withtyped/server can not properly infer the model * type, manually define it here for now. */ -export type TenantModel = Pick, 'id' | 'dbUser' | 'dbUserPassword'>; +export type TenantModel = InferModelType; +export type CreateTenant = Pick & + Partial>; diff --git a/packages/schemas/tables/_after_all.sql b/packages/schemas/tables/_after_all.sql index 60a06ad28..8a3d9b0e4 100644 --- a/packages/schemas/tables/_after_all.sql +++ b/packages/schemas/tables/_after_all.sql @@ -1,11 +1,5 @@ /* This SQL will run after all other queries. */ ----- Create trigger to set tenant name ---- -create trigger set_tenant_name_trigger -before insert on tenants -for each row when (new.name is null) -execute procedure set_tenant_name(); - ---- Grant CRUD access to the group ---- grant select, insert, update, delete on all tables diff --git a/packages/schemas/tables/_functions.sql b/packages/schemas/tables/_functions.sql index 9bb851e4e..ccb7a9ef9 100644 --- a/packages/schemas/tables/_functions.sql +++ b/packages/schemas/tables/_functions.sql @@ -13,10 +13,4 @@ $$ begin return new; end; $$ language plpgsql; -create function set_tenant_name() returns trigger as -$$ begin - new.name := concat('tenant_', new.id); - return new; -end; $$ language plpgsql; - /* no_after_each */