Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(tenancy): introduce db_name_template to project table (#352)

spinningbot3c559b9ab619
diff --git a/api/project.go b/api/project.go
index 2e8d85c529..422966afaa 100644
--- a/api/project.go
+++ b/api/project.go
@@ -81,6 +81,9 @@ type Project struct {
WorkflowType ProjectWorkflowType `jsonapi:"attr,workflowType"`
Visibility ProjectVisibility `jsonapi:"attr,visibility"`
TenantMode ProjectTenantMode `jsonapi:"attr,tenantMode"`
+ // DBNameTemplate is only used when a project is in tenant mode.
+ // Empty value means {{DB_NAME}}.
+ DBNameTemplate string `jsonapi:"attr,dbNameTemplate"`
}
// ProjectCreate is the API message for creating a project.
@@ -90,9 +93,10 @@ type ProjectCreate struct {
CreatorID int
// Domain specific fields
- Name string `jsonapi:"attr,name"`
- Key string `jsonapi:"attr,key"`
- TenantMode ProjectTenantMode `jsonapi:"attr,tenantMode"`
+ Name string `jsonapi:"attr,name"`
+ Key string `jsonapi:"attr,key"`
+ TenantMode ProjectTenantMode `jsonapi:"attr,tenantMode"`
+ DBNameTemplate string `jsonapi:"attr,dbNameTemplate"`
}
// ProjectFind is the API message for finding projects.
@@ -125,10 +129,11 @@ type ProjectPatch struct {
UpdaterID int
// Domain specific fields
- Name *string `jsonapi:"attr,name"`
- Key *string `jsonapi:"attr,key"`
- WorkflowType *ProjectWorkflowType `jsonapi:"attr,workflowType"`
- TenantMode *ProjectTenantMode `jsonapi:"attr,tenantMode"`
+ Name *string `jsonapi:"attr,name"`
+ Key *string `jsonapi:"attr,key"`
+ WorkflowType *ProjectWorkflowType `jsonapi:"attr,workflowType"`
+ TenantMode *ProjectTenantMode `jsonapi:"attr,tenantMode"`
+ DBNameTemplate *string `jsonapi:"attr,dbNameTemplate"`
}
// ProjectService is the service for projects.
diff --git a/docs/design/tenancy.md b/docs/design/tenancy.md
index b89faa3647..e0dbfc991d 100644
--- a/docs/design/tenancy.md
+++ b/docs/design/tenancy.md
@@ -313,7 +313,7 @@ This will be a new ENUM field on the existing [project table](https://github.com
There are use cases that customers put different tenant databases onto the same or a few instances. Since database names are unique within an instance, tenants will have different database names. There are two approaches to solve the problem by reusing most of the infrastructure above.
-- Database naming pattern: most users have tenant names as part of database names such as `db1_customer1`, `db1_customer2`, `db2_customer1`, `db2_customer2`. We can allow users to provide a database name pattern e.g. `{{DB_NAME}}_{{TENANT}}` or `{{DB_NAME}}_{{LOCATION}}` to pin a collection of tenant databases with the same `{{DB_NAME}}` prefix. This name pattern is a project level setting which is allowed only for projects in tenant mode.
+- Database naming template: most users have tenant names as part of database names such as `db1_customer1`, `db1_customer2`, `db2_customer1`, `db2_customer2`. We can allow users to provide a database name template e.g. `{{DB_NAME}}_{{TENANT}}` or `{{DB_NAME}}_{{LOCATION}}` to pin a collection of tenant databases with the same `{{DB_NAME}}` prefix. This name template is a project level setting which is allowed only for projects in tenant mode.
- Namespace: this is a database level setting similar to [k8s namespaces](https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/). For example, all tenant databases with the same prefix `db1` are in the same namespace. Deployments and deployment configurations are tighted to namespaces.
We choose the first option because it is easy to understand, solving most use cases with minimal changes. More importantly, it's compatible with the second option if we want to support the rest use cases latter e.g. migrating existing tenant databases to Bytebase platform.
diff --git a/store/migration/10001__init_schema.sql b/store/migration/10001__init_schema.sql
index 7740fee047..1769354af3 100644
--- a/store/migration/10001__init_schema.sql
+++ b/store/migration/10001__init_schema.sql
@@ -213,7 +213,10 @@ CREATE TABLE project (
`key` TEXT NOT NULL UNIQUE,
workflow_type TEXT NOT NULL CHECK (workflow_type IN ('UI', 'VCS')),
visibility TEXT NOT NULL CHECK (visibility IN ('PUBLIC', 'PRIVATE')),
- tenant_mode TEXT NOT NULL DEFAULT 'DISABLED' CHECK (tenant_mode IN ('DISABLED', 'TENANT'))
+ tenant_mode TEXT NOT NULL DEFAULT 'DISABLED' CHECK (tenant_mode IN ('DISABLED', 'TENANT')),
+ -- db_name_template is only used when a project is in tenant mode.
+ -- Empty value means {{DB_NAME}}.
+ db_name_template TEXT NOT NULL
);
INSERT INTO
@@ -224,7 +227,9 @@ INSERT INTO
name,
`key`,
workflow_type,
- visibility
+ visibility,
+ tenant_mode,
+ db_name_template
)
VALUES
(
@@ -234,7 +239,9 @@ VALUES
'Default',
'DEFAULT',
'UI',
- 'PUBLIC'
+ 'PUBLIC',
+ 'DISABLED',
+ ''
);
UPDATE
diff --git a/store/project.go b/store/project.go
index a3e5114f09..4a8b4d011f 100644
--- a/store/project.go
+++ b/store/project.go
@@ -164,16 +164,18 @@ func createProject(ctx context.Context, tx *Tx, create *api.ProjectCreate) (*api
key,
workflow_type,
visibility,
- tenant_mode
+ tenant_mode,
+ db_name_template
)
- VALUES (?, ?, ?, ?, 'UI', 'PUBLIC', ?)
- RETURNING id, row_status, creator_id, created_ts, updater_id, updated_ts, name, `+"`key`, workflow_type, visibility, tenant_mode"+`
+ VALUES (?, ?, ?, ?, 'UI', 'PUBLIC', ?, ?)
+ RETURNING id, row_status, creator_id, created_ts, updater_id, updated_ts, name, `+"`key`, workflow_type, visibility, tenant_mode, db_name_template"+`
`,
create.CreatorID,
create.CreatorID,
create.Name,
strings.ToUpper(create.Key),
create.TenantMode,
+ create.DBNameTemplate,
)
if err != nil {
@@ -195,6 +197,7 @@ func createProject(ctx context.Context, tx *Tx, create *api.ProjectCreate) (*api
&project.WorkflowType,
&project.Visibility,
&project.TenantMode,
+ &project.DBNameTemplate,
); err != nil {
return nil, FormatError(err)
}
@@ -227,7 +230,8 @@ func findProjectList(ctx context.Context, tx *Tx, find *api.ProjectFind) (_ []*a
key,
workflow_type,
visibility,
- tenant_mode
+ tenant_mode,
+ db_name_template
FROM project
WHERE `+strings.Join(where, " AND "),
args...,
@@ -253,6 +257,7 @@ func findProjectList(ctx context.Context, tx *Tx, find *api.ProjectFind) (_ []*a
&project.WorkflowType,
&project.Visibility,
&project.TenantMode,
+ &project.DBNameTemplate,
); err != nil {
return nil, FormatError(err)
}
@@ -285,6 +290,9 @@ func patchProject(ctx context.Context, tx *sql.Tx, patch *api.ProjectPatch) (*ap
if v := patch.TenantMode; v != nil {
set, args = append(set, "`tenant_mode` = ?"), append(args, *v)
}
+ if v := patch.DBNameTemplate; v != nil {
+ set, args = append(set, "`db_name_template` = ?"), append(args, *v)
+ }
args = append(args, patch.ID)
@@ -293,7 +301,7 @@ func patchProject(ctx context.Context, tx *sql.Tx, patch *api.ProjectPatch) (*ap
UPDATE project
SET `+strings.Join(set, ", ")+`
WHERE id = ?
- RETURNING id, row_status, creator_id, created_ts, updater_id, updated_ts, name, `+"`key`, workflow_type, visibility, tenant_mode"+`
+ RETURNING id, row_status, creator_id, created_ts, updater_id, updated_ts, name, `+"`key`, workflow_type, visibility, tenant_mode, db_name_template"+`
`,
args...,
)
@@ -316,6 +324,7 @@ func patchProject(ctx context.Context, tx *sql.Tx, patch *api.ProjectPatch) (*ap
&project.WorkflowType,
&project.Visibility,
&project.TenantMode,
+ &project.DBNameTemplate,
); err != nil {
return nil, FormatError(err)
}
diff --git a/store/seed/test/10030__project.sql b/store/seed/test/10030__project.sql
index f7feeb3da7..a82dd409ef 100644
--- a/store/seed/test/10030__project.sql
+++ b/store/seed/test/10030__project.sql
@@ -7,7 +7,8 @@ INSERT INTO
`key`,
workflow_type,
visibility,
- tenant_mode
+ tenant_mode,
+ db_name_template
)
VALUES
(
@@ -18,7 +19,8 @@ VALUES
'TEST',
'UI',
'PUBLIC',
- 'DISABLED'
+ 'DISABLED',
+ ''
);
INSERT INTO
@@ -30,7 +32,8 @@ INSERT INTO
`key`,
workflow_type,
visibility,
- tenant_mode
+ tenant_mode,
+ db_name_template
)
VALUES
(
@@ -41,7 +44,8 @@ VALUES
'SHP',
'VCS',
'PUBLIC',
- 'DISABLED'
+ 'DISABLED',
+ ''
);
INSERT INTO
@@ -53,7 +57,8 @@ INSERT INTO
`key`,
workflow_type,
visibility,
- tenant_mode
+ tenant_mode,
+ db_name_template
)
VALUES
(
@@ -64,7 +69,8 @@ VALUES
'BLG',
'VCS',
'PUBLIC',
- 'DISABLED'
+ 'DISABLED',
+ ''
);
INSERT INTO
@@ -77,7 +83,8 @@ INSERT INTO
`key`,
workflow_type,
visibility,
- tenant_mode
+ tenant_mode,
+ db_name_template
)
VALUES
(
@@ -89,7 +96,8 @@ VALUES
'RTR',
'UI',
'PUBLIC',
- 'DISABLED'
+ 'DISABLED',
+ ''
);
INSERT INTO
@@ -101,7 +109,8 @@ INSERT INTO
`key`,
workflow_type,
visibility,
- tenant_mode
+ tenant_mode,
+ db_name_template
)
VALUES
(
@@ -112,7 +121,8 @@ VALUES
'TNTG',
'VCS',
'PUBLIC',
- 'TENANT'
+ 'TENANT',
+ ''
);
INSERT INTO
@@ -124,7 +134,8 @@ INSERT INTO
`key`,
workflow_type,
visibility,
- tenant_mode
+ tenant_mode,
+ db_name_template
)
VALUES
(
@@ -135,5 +146,31 @@ VALUES
'TNTU',
'UI',
'PUBLIC',
- 'TENANT'
+ 'TENANT',
+ ''
+ );
+
+INSERT INTO
+ project (
+ id,
+ creator_id,
+ updater_id,
+ name,
+ `key`,
+ workflow_type,
+ visibility,
+ tenant_mode,
+ db_name_template
+ )
+VALUES
+ (
+ 3007,
+ 101,
+ 101,
+ 'Tenant (db pattern)',
+ 'TNMG',
+ 'UI',
+ 'PUBLIC',
+ 'TENANT',
+ '{{DB_NAME}}_{{TENANT}}'
);