mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
feat(schemas): update tenants table schema (#3860)
This commit is contained in:
parent
af42e87bc0
commit
0ebaec520e
6 changed files with 74 additions and 1 deletions
|
@ -0,0 +1,45 @@
|
|||
import { sql } from 'slonik';
|
||||
|
||||
import type { AlterationScript } from '../lib/types/alteration.js';
|
||||
|
||||
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 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;
|
||||
`);
|
||||
},
|
||||
};
|
||||
export default alteration;
|
|
@ -1,11 +1,16 @@
|
|||
import { createModel } from '@withtyped/server';
|
||||
|
||||
import { TenantTag } from '../index.js';
|
||||
|
||||
export const Tenants = createModel(/* sql */ `
|
||||
/* init_order = 0 */
|
||||
create table tenants (
|
||||
id varchar(21) not null,
|
||||
db_user varchar(128),
|
||||
db_user_password varchar(128),
|
||||
name varchar(128) not null,
|
||||
tag varchar(64) not null default '${TenantTag.Development}',
|
||||
created_at timestamptz not null default(now()),
|
||||
primary key (id),
|
||||
constraint tenants__db_user
|
||||
unique (db_user)
|
||||
|
|
|
@ -4,4 +4,9 @@ import type { Tenants } from '../models/tenants.js';
|
|||
|
||||
export const defaultTenantId = 'default';
|
||||
export const adminTenantId = 'admin';
|
||||
export type TenantModel = InferModelType<typeof Tenants>;
|
||||
|
||||
/**
|
||||
* `createModel` from @withtyped/server can not properly infer the model
|
||||
* type, manually define it here for now.
|
||||
*/
|
||||
export type TenantModel = Pick<InferModelType<typeof Tenants>, 'id' | 'dbUser' | 'dbUserPassword'>;
|
||||
|
|
|
@ -1,3 +1,9 @@
|
|||
export enum TenantTag {
|
||||
Development = 'development',
|
||||
Staging = 'staging',
|
||||
Production = 'production',
|
||||
}
|
||||
|
||||
export type TenantInfo = {
|
||||
id: string;
|
||||
indicator: string;
|
||||
|
|
|
@ -1,5 +1,11 @@
|
|||
/* 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
|
||||
|
|
|
@ -13,4 +13,10 @@ $$ 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 */
|
||||
|
|
Loading…
Reference in a new issue