Skip to content

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

chore: add resource-id to environment, instance, project (#4024)

Danny Xu5bcb96bb19ec
diff --git a/api/environment.go b/api/environment.go
index ce991b1faa..27b08854c7 100644
--- a/api/environment.go
+++ b/api/environment.go
@@ -59,12 +59,17 @@ func (find *EnvironmentFind) String() string {
// placeholderRegexp is the regexp for placeholder.
// Refer to https://stackoverflow.com/a/6222235/19075342, but we support '.' for now.
var placeholderRegexp = regexp.MustCompile(`[^\\/?%*:|"<>]+`)
+var alphaNumeric = regexp.MustCompile(`^[a-zA-Z0-9]*$`)
// IsValidEnvironmentName checks if the environment name is valid.
func IsValidEnvironmentName(environmentName string) error {
if !placeholderRegexp.MatchString(environmentName) {
return errors.Errorf("environment name %q cannot contain placeholder characters (\\, /, ?, %%, *, :, |, \", <, >)", environmentName)
}
+ if !alphaNumeric.MatchString(environmentName) {
+ return errors.Errorf("environment name %q can only contain alphabet numeric characters (a-z, A-Z, 0-9)", environmentName)
+ }
+
return nil
}
diff --git a/server/utils/utils.go b/server/utils/utils.go
index 1c46ee4c29..f1a4d9ff72 100644
--- a/server/utils/utils.go
+++ b/server/utils/utils.go
@@ -222,13 +222,7 @@ func SetDatabaseLabels(ctx context.Context, store *store.Store, labelsJSON strin
return echo.NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
}
- rowStatus := api.Normal
- labelKeyList, err := store.FindLabelKey(ctx, &api.LabelKeyFind{RowStatus: &rowStatus})
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find label key list").SetInternal(err)
- }
-
- if err := validateDatabaseLabelList(labels, labelKeyList, database.Instance.Environment.Name); err != nil {
+ if err := validateDatabaseLabelList(labels, database.Instance.Environment.Name); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to validate database labels").SetInternal(err)
}
@@ -240,15 +234,7 @@ func SetDatabaseLabels(ctx context.Context, store *store.Store, labelsJSON strin
return nil
}
-func validateDatabaseLabelList(labelList []*api.DatabaseLabel, labelKeyList []*api.LabelKey, environmentName string) error {
- keyValueList := make(map[string]map[string]bool)
- for _, labelKey := range labelKeyList {
- keyValueList[labelKey.Key] = map[string]bool{}
- for _, value := range labelKey.ValueList {
- keyValueList[labelKey.Key][value] = true
- }
- }
-
+func validateDatabaseLabelList(labelList []*api.DatabaseLabel, environmentName string) error {
var environmentValue *string
// check label key & value availability
@@ -257,9 +243,6 @@ func validateDatabaseLabelList(labelList []*api.DatabaseLabel, labelKeyList []*a
environmentValue = &label.Value
continue
}
- if _, ok := keyValueList[label.Key]; !ok {
- return common.Errorf(common.Invalid, "invalid database label key: %v", label.Key)
- }
}
// Environment label must exist and is immutable.
diff --git a/server/utils/utils_test.go b/server/utils/utils_test.go
index e9af2aafed..09c7df3dd8 100644
--- a/server/utils/utils_test.go
+++ b/server/utils/utils_test.go
@@ -13,7 +13,6 @@ func TestValidateDatabaseLabelList(t *testing.T) {
tests := []struct {
name string
labelList []*api.DatabaseLabel
- labelKeyList []*api.LabelKey
environmentName string
wantErr bool
}{
@@ -29,48 +28,9 @@ func TestValidateDatabaseLabelList(t *testing.T) {
Value: "Dev",
},
},
- labelKeyList: []*api.LabelKey{
- {
- Key: "bb.location",
- ValueList: []string{"earth"},
- },
- {
- Key: api.EnvironmentKeyName,
- ValueList: []string{},
- },
- },
environmentName: "Dev",
wantErr: false,
},
- {
- name: "invalid label key",
- labelList: []*api.DatabaseLabel{
- {
- Key: "bb.location",
- Value: "earth",
- },
- {
- Key: api.EnvironmentKeyName,
- Value: "Dev",
- },
- {
- Key: "bb.tenant",
- Value: "bytebase",
- },
- },
- labelKeyList: []*api.LabelKey{
- {
- Key: "bb.location",
- ValueList: []string{"earth"},
- },
- {
- Key: api.EnvironmentKeyName,
- ValueList: []string{},
- },
- },
- environmentName: "Dev",
- wantErr: true,
- },
{
name: "environment label not present",
labelList: []*api.DatabaseLabel{
@@ -79,16 +39,6 @@ func TestValidateDatabaseLabelList(t *testing.T) {
Value: "earth",
},
},
- labelKeyList: []*api.LabelKey{
- {
- Key: "bb.location",
- ValueList: []string{"earth"},
- },
- {
- Key: api.EnvironmentKeyName,
- ValueList: []string{},
- },
- },
environmentName: "Dev",
wantErr: true,
},
@@ -104,23 +54,13 @@ func TestValidateDatabaseLabelList(t *testing.T) {
Value: "Prod",
},
},
- labelKeyList: []*api.LabelKey{
- {
- Key: "bb.location",
- ValueList: []string{"earth"},
- },
- {
- Key: api.EnvironmentKeyName,
- ValueList: []string{},
- },
- },
environmentName: "Dev",
wantErr: true,
},
}
for _, test := range tests {
- err := validateDatabaseLabelList(test.labelList, test.labelKeyList, test.environmentName)
+ err := validateDatabaseLabelList(test.labelList, test.environmentName)
if test.wantErr {
assert.Error(t, err)
} else {
diff --git a/store/demo/dev/10030##project.sql b/store/demo/dev/10030##project.sql
index 5b3d3d6f53..04e7c2709c 100644
--- a/store/demo/dev/10030##project.sql
+++ b/store/demo/dev/10030##project.sql
@@ -8,7 +8,8 @@ INSERT INTO
workflow_type,
visibility,
tenant_mode,
- db_name_template
+ db_name_template,
+ resource_id
)
VALUES
(
@@ -20,7 +21,8 @@ VALUES
'UI',
'PUBLIC',
'DISABLED',
- ''
+ '',
+ 'test-ui'
);
INSERT INTO
@@ -33,7 +35,8 @@ INSERT INTO
workflow_type,
visibility,
tenant_mode,
- db_name_template
+ db_name_template,
+ resource_id
)
VALUES
(
@@ -45,7 +48,8 @@ VALUES
'VCS',
'PUBLIC',
'DISABLED',
- ''
+ '',
+ 'shop-git'
);
INSERT INTO
@@ -58,7 +62,8 @@ INSERT INTO
workflow_type,
visibility,
tenant_mode,
- db_name_template
+ db_name_template,
+ resource_id
)
VALUES
(
@@ -70,7 +75,8 @@ VALUES
'VCS',
'PUBLIC',
'DISABLED',
- ''
+ '',
+ 'blog-git'
);
INSERT INTO
@@ -84,7 +90,8 @@ INSERT INTO
workflow_type,
visibility,
tenant_mode,
- db_name_template
+ db_name_template,
+ resource_id
)
VALUES
(
@@ -97,7 +104,8 @@ VALUES
'UI',
'PUBLIC',
'DISABLED',
- ''
+ '',
+ 'retired-project'
);
ALTER SEQUENCE principal_id_seq RESTART WITH 3005;
diff --git a/store/demo/dev/10050##environment.sql b/store/demo/dev/10050##environment.sql
index 205d93b330..9456c5d6d7 100644
--- a/store/demo/dev/10050##environment.sql
+++ b/store/demo/dev/10050##environment.sql
@@ -4,7 +4,8 @@ INSERT INTO
creator_id,
updater_id,
name,
- "order"
+ "order",
+ resource_id
)
VALUES
(
@@ -12,7 +13,8 @@ VALUES
101,
101,
'Dev',
- 0
+ 0,
+ 'dev'
);
INSERT INTO
@@ -21,7 +23,8 @@ INSERT INTO
creator_id,
updater_id,
name,
- "order"
+ "order",
+ resource_id
)
VALUES
(
@@ -29,7 +32,8 @@ VALUES
101,
101,
'Integration',
- 1
+ 1,
+ 'integration'
);
INSERT INTO
@@ -38,7 +42,8 @@ INSERT INTO
creator_id,
updater_id,
name,
- "order"
+ "order",
+ resource_id
)
VALUES
(
@@ -46,7 +51,8 @@ VALUES
101,
101,
'Staging',
- 2
+ 2,
+ 'staging'
);
INSERT INTO
@@ -55,7 +61,8 @@ INSERT INTO
creator_id,
updater_id,
name,
- "order"
+ "order",
+ resource_id
)
VALUES
(
@@ -63,7 +70,8 @@ VALUES
101,
101,
'Prod',
- 3
+ 3,
+ 'prod'
);
INSERT INTO
@@ -73,7 +81,8 @@ INSERT INTO
creator_id,
updater_id,
name,
- "order"
+ "order",
+ resource_id
)
VALUES
(
@@ -82,7 +91,8 @@ VALUES
101,
101,
'Archived Env 1',
- 4
+ 4,
+ 'archived'
);
ALTER SEQUENCE environment_id_seq RESTART WITH 5006;
diff --git a/store/demo/dev/10060##instance.sql b/store/demo/dev/10060##instance.sql
index 1026a8d7ca..92e1308087 100644
--- a/store/demo/dev/10060##instance.sql
+++ b/store/demo/dev/10060##instance.sql
@@ -9,7 +9,8 @@ INSERT INTO
engine_version,
host,
port,
- external_link
+ external_link,
+ resource_id
)
VALUES
(
@@ -22,7 +23,8 @@ VALUES
'8.0.19',
'127.0.0.1',
'3306',
- 'bytebase.com/database/mysql'
+ 'bytebase.com/database/mysql',
+ 'localhost-dev-mysql'
);
INSERT INTO
@@ -36,7 +38,8 @@ INSERT INTO
engine_version,
host,
port,
- external_link
+ external_link,
+ resource_id
)
VALUES
(
@@ -49,7 +52,8 @@ VALUES
'8.0.19',
'mysql.integration.example.com',
'3306',
- 'bytebase.com/database/mysql'
+ 'bytebase.com/database/mysql',
+ 'onpremises-integration-mysql'
);
INSERT INTO
@@ -63,7 +67,8 @@ INSERT INTO
engine_version,
host,
port,
- external_link
+ external_link,
+ resource_id
)
VALUES
(
@@ -76,7 +81,8 @@ VALUES
'8.0.19',
'mysql.staging.example.com',
'3306',
- 'bytebase.com/database/mysql'
+ 'bytebase.com/database/mysql',
+ 'onpremises-staging-mysql'
);
INSERT INTO
@@ -90,7 +96,8 @@ INSERT INTO
engine_version,
host,
port,
- external_link
+ external_link,
+ resource_id
)
VALUES
(
@@ -103,7 +110,8 @@ VALUES
'8.0.19',
'mysql.prod.example.com',
'3306',
- 'bytebase.com/database/mysql'
+ 'bytebase.com/database/mysql',
+ 'onpremises-prod-mysql'
);
INSERT INTO
@@ -117,7 +125,8 @@ INSERT INTO
engine_version,
host,
port,
- external_link
+ external_link,
+ resource_id
)
VALUES
(
@@ -130,7 +139,8 @@ VALUES
'13.0',
'127.0.0.1',
'5432',
- 'bytebase.com/database/postgres'
+ 'bytebase.com/database/postgres',
+ 'localhost-dev-postgresql'
);
INSERT INTO
@@ -144,7 +154,8 @@ INSERT INTO
engine_version,
host,
port,
- external_link
+ external_link,
+ resource_id
)
VALUES
(
@@ -157,7 +168,8 @@ VALUES
'13.0',
'dpg-c8a7pcd0mal7gtod05p0',
'5432',
- 'postgres://postgre_demo_user:3QixNmRMGhklX6B1lmCZ3ZsHFPIE5EgG@dpg-c8a7pcd0mal7gtod05p0/postgre_demo'
+ 'postgres://postgre_demo_user:3QixNmRMGhklX6B1lmCZ3ZsHFPIE5EgG@dpg-c8a7pcd0mal7gtod05p0/postgre_demo',
+ 'demo-postgresql-on-render'
);
INSERT INTO
@@ -171,7 +183,8 @@ INSERT INTO
engine_version,
host,
port,
- external_link
+ external_link,
+ resource_id
)
VALUES
(
@@ -184,7 +197,8 @@ VALUES
'5.7.25-TiDB-v5.2.1',
'127.0.0.1',
'4000',
- 'bytebase.com/database/tidb'
+ 'bytebase.com/database/tidb',
+ 'localhost-dev-tidb'
);
INSERT INTO
@@ -198,7 +212,8 @@ INSERT INTO
engine_version,
host,
port,
- external_link
+ external_link,
+ resource_id
)
VALUES
(
@@ -211,7 +226,8 @@ VALUES
'21.10.2.15',
'127.0.0.1',
'9000',
- 'bytebase.com/database/clickhouse'
+ 'bytebase.com/database/clickhouse',
+ 'localhost-dev-clickhouse'
);
INSERT INTO
@@ -225,7 +241,8 @@ INSERT INTO
engine_version,
host,
port,
- external_link
+ external_link,
+ resource_id
)
VALUES
(
@@ -238,7 +255,8 @@ VALUES
'21.10.2.15',
'127.0.0.1',
'',
- 'bytebase.com/database/snowflake'
+ 'bytebase.com/database/snowflake',
+ 'localhost-dev-snowflake'
);
INSERT INTO
@@ -253,7 +271,8 @@ INSERT INTO
engine_version,
host,
port,
- external_link
+ external_link,
+ resource_id
)
VALUES
(
@@ -267,7 +286,8 @@ VALUES
'5.7.25',
'mysql.retired.example.com',
'3306',
- 'bytebase.com/database/mysql'
+ 'bytebase.com/database/mysql',
+ 'retired-prod-mysql'
);
ALTER SEQUENCE instance_id_seq RESTART WITH 6011;
diff --git a/store/demo/prod/10030##project.sql b/store/demo/prod/10030##project.sql
index 5b3d3d6f53..04e7c2709c 100644
--- a/store/demo/prod/10030##project.sql
+++ b/store/demo/prod/10030##project.sql
@@ -8,7 +8,8 @@ INSERT INTO
workflow_type,
visibility,
tenant_mode,
- db_name_template
+ db_name_template,
+ resource_id
)
VALUES
(
@@ -20,7 +21,8 @@ VALUES
'UI',
'PUBLIC',
'DISABLED',
- ''
+ '',
+ 'test-ui'
);
INSERT INTO
@@ -33,7 +35,8 @@ INSERT INTO
workflow_type,
visibility,
tenant_mode,
- db_name_template
+ db_name_template,
+ resource_id
)
VALUES
(
@@ -45,7 +48,8 @@ VALUES
'VCS',
'PUBLIC',
'DISABLED',
- ''
+ '',
+ 'shop-git'
);
INSERT INTO
@@ -58,7 +62,8 @@ INSERT INTO
workflow_type,
visibility,
tenant_mode,
- db_name_template
+ db_name_template,
+ resource_id
)
VALUES
(
@@ -70,7 +75,8 @@ VALUES
'VCS',
'PUBLIC',
'DISABLED',
- ''
+ '',
+ 'blog-git'
);
INSERT INTO
@@ -84,7 +90,8 @@ INSERT INTO
workflow_type,
visibility,
tenant_mode,
- db_name_template
+ db_name_template,
+ resource_id
)
VALUES
(
@@ -97,7 +104,8 @@ VALUES
'UI',
'PUBLIC',
'DISABLED',
- ''
+ '',
+ 'retired-project'
);
ALTER SEQUENCE principal_id_seq RESTART WITH 3005;
diff --git a/store/demo/prod/10050##environment.sql b/store/demo/prod/10050##environment.sql
index 205d93b330..9456c5d6d7 100644
--- a/store/demo/prod/10050##environment.sql
+++ b/store/demo/prod/10050##environment.sql
@@ -4,7 +4,8 @@ INSERT INTO
creator_id,
updater_id,
name,
- "order"
+ "order",
+ resource_id
)
VALUES
(
@@ -12,7 +13,8 @@ VALUES
101,
101,
'Dev',
- 0
+ 0,
+ 'dev'
);
INSERT INTO
@@ -21,7 +23,8 @@ INSERT INTO
creator_id,
updater_id,
name,
- "order"
+ "order",
+ resource_id
)
VALUES
(
@@ -29,7 +32,8 @@ VALUES
101,
101,
'Integration',
- 1
+ 1,
+ 'integration'
);
INSERT INTO
@@ -38,7 +42,8 @@ INSERT INTO
creator_id,
updater_id,
name,
- "order"
+ "order",
+ resource_id
)
VALUES
(
@@ -46,7 +51,8 @@ VALUES
101,
101,
'Staging',
- 2
+ 2,
+ 'staging'
);
INSERT INTO
@@ -55,7 +61,8 @@ INSERT INTO
creator_id,
updater_id,
name,
- "order"
+ "order",
+ resource_id
)
VALUES
(
@@ -63,7 +70,8 @@ VALUES
101,
101,
'Prod',
- 3
+ 3,
+ 'prod'
);
INSERT INTO
@@ -73,7 +81,8 @@ INSERT INTO
creator_id,
updater_id,
name,
- "order"
+ "order",
+ resource_id
)
VALUES
(
@@ -82,7 +91,8 @@ VALUES
101,
101,
'Archived Env 1',
- 4
+ 4,
+ 'archived'
);
ALTER SEQUENCE environment_id_seq RESTART WITH 5006;
diff --git a/store/demo/prod/10060##instance.sql b/store/demo/prod/10060##instance.sql
index 1026a8d7ca..92e1308087 100644
--- a/store/demo/prod/10060##instance.sql
+++ b/store/demo/prod/10060##instance.sql
@@ -9,7 +9,8 @@ INSERT INTO
engine_version,
host,
port,
- external_link
+ external_link,
+ resource_id
)
VALUES
(
@@ -22,7 +23,8 @@ VALUES
'8.0.19',
'127.0.0.1',
'3306',
- 'bytebase.com/database/mysql'
+ 'bytebase.com/database/mysql',
+ 'localhost-dev-mysql'
);
INSERT INTO
@@ -36,7 +38,8 @@ INSERT INTO
engine_version,
host,
port,
- external_link
+ external_link,
+ resource_id
)
VALUES
(
@@ -49,7 +52,8 @@ VALUES
'8.0.19',
'mysql.integration.example.com',
'3306',
- 'bytebase.com/database/mysql'
+ 'bytebase.com/database/mysql',
+ 'onpremises-integration-mysql'
);
INSERT INTO
@@ -63,7 +67,8 @@ INSERT INTO
engine_version,
host,
port,
- external_link
+ external_link,
+ resource_id
)
VALUES
(
@@ -76,7 +81,8 @@ VALUES
'8.0.19',
'mysql.staging.example.com',
'3306',
- 'bytebase.com/database/mysql'
+ 'bytebase.com/database/mysql',
+ 'onpremises-staging-mysql'
);
INSERT INTO
@@ -90,7 +96,8 @@ INSERT INTO
engine_version,
host,
port,
- external_link
+ external_link,
+ resource_id
)
VALUES
(
@@ -103,7 +110,8 @@ VALUES
'8.0.19',
'mysql.prod.example.com',
'3306',
- 'bytebase.com/database/mysql'
+ 'bytebase.com/database/mysql',
+ 'onpremises-prod-mysql'
);
INSERT INTO
@@ -117,7 +125,8 @@ INSERT INTO
engine_version,
host,
port,
- external_link
+ external_link,
+ resource_id
)
VALUES
(
@@ -130,7 +139,8 @@ VALUES
'13.0',
'127.0.0.1',
'5432',
- 'bytebase.com/database/postgres'
+ 'bytebase.com/database/postgres',
+ 'localhost-dev-postgresql'
);
INSERT INTO
@@ -144,7 +154,8 @@ INSERT INTO
engine_version,
host,
port,
- external_link
+ external_link,
+ resource_id
)
VALUES
(
@@ -157,7 +168,8 @@ VALUES
'13.0',
'dpg-c8a7pcd0mal7gtod05p0',
'5432',
- 'postgres://postgre_demo_user:3QixNmRMGhklX6B1lmCZ3ZsHFPIE5EgG@dpg-c8a7pcd0mal7gtod05p0/postgre_demo'
+ 'postgres://postgre_demo_user:3QixNmRMGhklX6B1lmCZ3ZsHFPIE5EgG@dpg-c8a7pcd0mal7gtod05p0/postgre_demo',
+ 'demo-postgresql-on-render'
);
INSERT INTO
@@ -171,7 +183,8 @@ INSERT INTO
engine_version,
host,
port,
- external_link
+ external_link,
+ resource_id
)
VALUES
(
@@ -184,7 +197,8 @@ VALUES
'5.7.25-TiDB-v5.2.1',
'127.0.0.1',
'4000',
- 'bytebase.com/database/tidb'
+ 'bytebase.com/database/tidb',
+ 'localhost-dev-tidb'
);
INSERT INTO
@@ -198,7 +212,8 @@ INSERT INTO
engine_version,
host,
port,
- external_link
+ external_link,
+ resource_id
)
VALUES
(
@@ -211,7 +226,8 @@ VALUES
'21.10.2.15',
'127.0.0.1',
'9000',
- 'bytebase.com/database/clickhouse'
+ 'bytebase.com/database/clickhouse',
+ 'localhost-dev-clickhouse'
);
INSERT INTO
@@ -225,7 +241,8 @@ INSERT INTO
engine_version,
host,
port,
- external_link
+ external_link,
+ resource_id
)
VALUES
(
@@ -238,7 +255,8 @@ VALUES
'21.10.2.15',
'127.0.0.1',
'',
- 'bytebase.com/database/snowflake'
+ 'bytebase.com/database/snowflake',
+ 'localhost-dev-snowflake'
);
INSERT INTO
@@ -253,7 +271,8 @@ INSERT INTO
engine_version,
host,
port,
- external_link
+ external_link,
+ resource_id
)
VALUES
(
@@ -267,7 +286,8 @@ VALUES
'5.7.25',
'mysql.retired.example.com',
'3306',
- 'bytebase.com/database/mysql'
+ 'bytebase.com/database/mysql',
+ 'retired-prod-mysql'
);
ALTER SEQUENCE instance_id_seq RESTART WITH 6011;
diff --git a/store/environment.go b/store/environment.go
index 6d3966883d..4598aee191 100644
--- a/store/environment.go
+++ b/store/environment.go
@@ -383,15 +383,18 @@ func (Store) createEnvironmentImpl(ctx context.Context, tx *Tx, create *Environm
order++
}
+ // TODO(d): allow users to set resource_id.
+ resourceID := strings.ToLower(create.Name)
// Insert row into database.
query := `
INSERT INTO environment (
creator_id,
updater_id,
name,
- "order"
+ "order",
+ resource_id
)
- VALUES ($1, $2, $3, $4)
+ VALUES ($1, $2, $3, $4, $5)
RETURNING id, row_status, creator_id, created_ts, updater_id, updated_ts, name, "order"
`
var envRaw environmentRaw
@@ -400,6 +403,7 @@ func (Store) createEnvironmentImpl(ctx context.Context, tx *Tx, create *Environm
create.CreatorID,
create.Name,
order,
+ resourceID,
).Scan(
&envRaw.ID,
&envRaw.RowStatus,
diff --git a/store/instance.go b/store/instance.go
index 7cb3cfb22e..5ad11a7b58 100644
--- a/store/instance.go
+++ b/store/instance.go
@@ -6,6 +6,7 @@ import (
"fmt"
"strings"
+ "github.com/google/uuid"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/api"
@@ -559,6 +560,8 @@ func (s *Store) patchInstanceRaw(ctx context.Context, patch *InstancePatch) (*in
// createInstanceImpl creates a new instance.
func createInstanceImpl(ctx context.Context, tx *Tx, create *InstanceCreate) (*instanceRaw, error) {
+ // TODO(d): allow users to set resource_id.
+ resourceID := fmt.Sprintf("instance-%s", uuid.New().String()[:8])
// Insert row into database.
query := `
INSERT INTO instance (
@@ -570,9 +573,10 @@ func createInstanceImpl(ctx context.Context, tx *Tx, create *InstanceCreate) (*i
external_link,
host,
port,
- database
+ database,
+ resource_id
)
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
RETURNING id, row_status, creator_id, created_ts, updater_id, updated_ts, environment_id, name, engine, engine_version, external_link, host, port, database
`
var instanceRaw instanceRaw
@@ -586,6 +590,7 @@ func createInstanceImpl(ctx context.Context, tx *Tx, create *InstanceCreate) (*i
create.Host,
create.Port,
create.Database,
+ resourceID,
).Scan(
&instanceRaw.ID,
&instanceRaw.RowStatus,
diff --git a/store/migration/dev/LATEST.sql b/store/migration/dev/LATEST.sql
index c062861e41..90eb532d2e 100644
--- a/store/migration/dev/LATEST.sql
+++ b/store/migration/dev/LATEST.sql
@@ -112,11 +112,14 @@ CREATE TABLE environment (
updater_id INTEGER NOT NULL REFERENCES principal (id),
updated_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
name TEXT NOT NULL,
- "order" INTEGER NOT NULL CHECK ("order" >= 0)
+ "order" INTEGER NOT NULL CHECK ("order" >= 0),
+ resource_id TEXT NOT NULL
);
CREATE UNIQUE INDEX idx_environment_unique_name ON environment(name);
+CREATE UNIQUE INDEX idx_environment_unique_resource_id ON environment(resource_id);
+
ALTER SEQUENCE environment_id_seq RESTART WITH 101;
CREATE TRIGGER update_environment_updated_ts
@@ -173,11 +176,14 @@ CREATE TABLE project (
role_provider TEXT NOT NULL CHECK (role_provider IN ('BYTEBASE', 'GITLAB_SELF_HOST', 'GITHUB_COM')) DEFAULT 'BYTEBASE',
schema_version_type TEXT NOT NULL CHECK (schema_version_type IN ('TIMESTAMP', 'SEMANTIC')) DEFAULT 'TIMESTAMP',
schema_change_type TEXT NOT NULL CHECK (schema_change_type IN ('DDL', 'SDL')) DEFAULT 'DDL',
- lgtm_check JSONB NOT NULL DEFAULT '{}'
+ lgtm_check JSONB NOT NULL DEFAULT '{}',
+ resource_id TEXT NOT NULL
);
CREATE UNIQUE INDEX idx_project_unique_key ON project(key);
+CREATE UNIQUE INDEX idx_project_unique_resource_id ON project(resource_id);
+
INSERT INTO
project (
id,
@@ -188,7 +194,8 @@ INSERT INTO
workflow_type,
visibility,
tenant_mode,
- db_name_template
+ db_name_template,
+ resource_id
)
VALUES
(
@@ -200,7 +207,8 @@ VALUES
'UI',
'PUBLIC',
'DISABLED',
- ''
+ '',
+ 'default'
);
ALTER SEQUENCE project_id_seq RESTART WITH 101;
@@ -279,9 +287,12 @@ CREATE TABLE instance (
host TEXT NOT NULL,
port TEXT NOT NULL,
external_link TEXT NOT NULL DEFAULT '',
- database TEXT NOT NULL DEFAULT ''
+ database TEXT NOT NULL DEFAULT '',
+ resource_id TEXT NOT NULL
);
+CREATE UNIQUE INDEX idx_instance_unique_resource_id ON instance(resource_id);
+
ALTER SEQUENCE instance_id_seq RESTART WITH 101;
CREATE TRIGGER update_instance_updated_ts
diff --git a/store/migration/dev/LATEST_DATA.sql b/store/migration/dev/LATEST_DATA.sql
index 93ec5f965d..1ccb8373c3 100644
--- a/store/migration/dev/LATEST_DATA.sql
+++ b/store/migration/dev/LATEST_DATA.sql
@@ -5,7 +5,8 @@ INSERT INTO
creator_id,
updater_id,
name,
- "order"
+ "order",
+ resource_id
)
VALUES
(
@@ -13,7 +14,8 @@ VALUES
1,
1,
'Test',
- 0
+ 0,
+ 'test'
);
INSERT INTO
@@ -22,7 +24,8 @@ INSERT INTO
creator_id,
updater_id,
name,
- "order"
+ "order",
+ resource_id
)
VALUES
(
@@ -30,7 +33,8 @@ VALUES
1,
1,
'Prod',
- 1
+ 1,
+ 'prod'
);
ALTER SEQUENCE environment_id_seq RESTART WITH 103;
@@ -105,36 +109,3 @@ VALUES
);
ALTER SEQUENCE policy_id_seq RESTART WITH 104;
-
--- Create label keys for `bb.location` and `bb.tenant`.
-INSERT INTO
- label_key (
- id,
- creator_id,
- updater_id,
- key
- )
-VALUES
- (
- 101,
- 1,
- 1,
- 'bb.location'
- );
-
-INSERT INTO
- label_key (
- id,
- creator_id,
- updater_id,
- key
- )
-VALUES
- (
- 102,
- 1,
- 1,
- 'bb.tenant'
- );
-
-ALTER SEQUENCE label_key_id_seq RESTART WITH 103;
diff --git a/store/migration/prod/1.10/0002##resource_id.sql b/store/migration/prod/1.10/0002##resource_id.sql
new file mode 100644
index 0000000000..b9d32578a3
--- /dev/null
+++ b/store/migration/prod/1.10/0002##resource_id.sql
@@ -0,0 +1,16 @@
+ALTER TABLE environment ADD COLUMN resource_id TEXT;
+ALTER TABLE project ADD COLUMN resource_id TEXT;
+ALTER TABLE instance ADD COLUMN resource_id TEXT;
+
+UPDATE environment SET resource_id=LOWER(name);
+UPDATE project SET resource_id='default' WHERE id == 1;
+UPDATE project SET resource_id=CONCAT('project-', LEFT(gen_random_uuid()::TEXT, 8)) WHERE id != 1;
+UPDATE instance SET resource_id=CONCAT('instance-', LEFT(gen_random_uuid()::TEXT, 8));
+
+ALTER TABLE environment ALTER COLUMN resource_id SET NOT NULL;
+ALTER TABLE project ALTER COLUMN resource_id SET NOT NULL;
+ALTER TABLE instance ALTER COLUMN resource_id SET NOT NULL;
+
+CREATE UNIQUE INDEX idx_environment_unique_resource_id ON environment(resource_id);
+CREATE UNIQUE INDEX idx_project_unique_resource_id ON project(resource_id);
+CREATE UNIQUE INDEX idx_instance_unique_resource_id ON instance(resource_id);
diff --git a/store/migration/prod/LATEST.sql b/store/migration/prod/LATEST.sql
index d75b0f7375..c4ae4b8073 100644
--- a/store/migration/prod/LATEST.sql
+++ b/store/migration/prod/LATEST.sql
@@ -112,11 +112,14 @@ CREATE TABLE environment (
updater_id INTEGER NOT NULL REFERENCES principal (id),
updated_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
name TEXT NOT NULL,
- "order" INTEGER NOT NULL CHECK ("order" >= 0)
+ "order" INTEGER NOT NULL CHECK ("order" >= 0),
+ resource_id TEXT NOT NULL
);
CREATE UNIQUE INDEX idx_environment_unique_name ON environment(name);
+CREATE UNIQUE INDEX idx_environment_unique_resource_id ON environment(resource_id);
+
ALTER SEQUENCE environment_id_seq RESTART WITH 101;
CREATE TRIGGER update_environment_updated_ts
@@ -172,11 +175,14 @@ CREATE TABLE project (
db_name_template TEXT NOT NULL,
role_provider TEXT NOT NULL CHECK (role_provider IN ('BYTEBASE', 'GITLAB_SELF_HOST', 'GITHUB_COM')) DEFAULT 'BYTEBASE',
schema_change_type TEXT NOT NULL CHECK (schema_change_type IN ('DDL', 'SDL')) DEFAULT 'DDL',
- lgtm_check JSONB NOT NULL DEFAULT '{}'
+ lgtm_check JSONB NOT NULL DEFAULT '{}',
+ resource_id TEXT NOT NULL
);
CREATE UNIQUE INDEX idx_project_unique_key ON project(key);
+CREATE UNIQUE INDEX idx_project_unique_resource_id ON project(resource_id);
+
INSERT INTO
project (
id,
@@ -187,7 +193,8 @@ INSERT INTO
workflow_type,
visibility,
tenant_mode,
- db_name_template
+ db_name_template,
+ resource_id
)
VALUES
(
@@ -199,7 +206,8 @@ VALUES
'UI',
'PUBLIC',
'DISABLED',
- ''
+ '',
+ 'default'
);
ALTER SEQUENCE project_id_seq RESTART WITH 101;
@@ -278,9 +286,12 @@ CREATE TABLE instance (
host TEXT NOT NULL,
port TEXT NOT NULL,
external_link TEXT NOT NULL DEFAULT '',
- database TEXT NOT NULL DEFAULT ''
+ database TEXT NOT NULL DEFAULT '',
+ resource_id TEXT NOT NULL
);
+CREATE UNIQUE INDEX idx_instance_unique_resource_id ON instance(resource_id);
+
ALTER SEQUENCE instance_id_seq RESTART WITH 101;
CREATE TRIGGER update_instance_updated_ts
diff --git a/store/migration/prod/LATEST_DATA.sql b/store/migration/prod/LATEST_DATA.sql
index 93ec5f965d..1ccb8373c3 100644
--- a/store/migration/prod/LATEST_DATA.sql
+++ b/store/migration/prod/LATEST_DATA.sql
@@ -5,7 +5,8 @@ INSERT INTO
creator_id,
updater_id,
name,
- "order"
+ "order",
+ resource_id
)
VALUES
(
@@ -13,7 +14,8 @@ VALUES
1,
1,
'Test',
- 0
+ 0,
+ 'test'
);
INSERT INTO
@@ -22,7 +24,8 @@ INSERT INTO
creator_id,
updater_id,
name,
- "order"
+ "order",
+ resource_id
)
VALUES
(
@@ -30,7 +33,8 @@ VALUES
1,
1,
'Prod',
- 1
+ 1,
+ 'prod'
);
ALTER SEQUENCE environment_id_seq RESTART WITH 103;
@@ -105,36 +109,3 @@ VALUES
);
ALTER SEQUENCE policy_id_seq RESTART WITH 104;
-
--- Create label keys for `bb.location` and `bb.tenant`.
-INSERT INTO
- label_key (
- id,
- creator_id,
- updater_id,
- key
- )
-VALUES
- (
- 101,
- 1,
- 1,
- 'bb.location'
- );
-
-INSERT INTO
- label_key (
- id,
- creator_id,
- updater_id,
- key
- )
-VALUES
- (
- 102,
- 1,
- 1,
- 'bb.tenant'
- );
-
-ALTER SEQUENCE label_key_id_seq RESTART WITH 103;
diff --git a/store/pg_engine_test.go b/store/pg_engine_test.go
index 031c2a15eb..54933fea01 100644
--- a/store/pg_engine_test.go
+++ b/store/pg_engine_test.go
@@ -220,5 +220,5 @@ func TestMigrationCompatibility(t *testing.T) {
func TestGetCutoffVersion(t *testing.T) {
releaseVersion, err := getProdCutoffVersion()
require.NoError(t, err)
- require.Equal(t, semver.MustParse("1.10.1"), releaseVersion)
+ require.Equal(t, semver.MustParse("1.10.2"), releaseVersion)
}
diff --git a/store/project.go b/store/project.go
index 86473e0fc3..1e64f259d8 100644
--- a/store/project.go
+++ b/store/project.go
@@ -6,6 +6,7 @@ import (
"fmt"
"strings"
+ "github.com/google/uuid"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/api"
@@ -315,6 +316,8 @@ func createProjectImpl(ctx context.Context, tx *Tx, create *api.ProjectCreate) (
if create.SchemaChangeType == "" {
create.SchemaChangeType = api.ProjectSchemaChangeTypeDDL
}
+ // TODO(d): allow users to set resource_id.
+ resourceID := fmt.Sprintf("project-%s", uuid.New().String()[:8])
query := `
INSERT INTO project (
creator_id,
@@ -327,9 +330,10 @@ func createProjectImpl(ctx context.Context, tx *Tx, create *api.ProjectCreate) (
db_name_template,
role_provider,
schema_change_type,
- lgtm_check
+ lgtm_check,
+ resource_id
)
- VALUES ($1, $2, $3, $4, 'UI', 'PUBLIC', $5, $6, $7, $8, $9)
+ VALUES ($1, $2, $3, $4, 'UI', 'PUBLIC', $5, $6, $7, $8, $9, $10)
RETURNING id, row_status, creator_id, created_ts, updater_id, updated_ts, name, key, workflow_type, visibility, tenant_mode, db_name_template, role_provider, schema_change_type, lgtm_check
`
var project projectRaw
@@ -343,6 +347,7 @@ func createProjectImpl(ctx context.Context, tx *Tx, create *api.ProjectCreate) (
create.RoleProvider,
create.SchemaChangeType,
api.GetDefaultLGTMCheckSetting(),
+ resourceID,
).Scan(
&project.ID,
&project.RowStatus,
diff --git a/tests/database.go b/tests/database.go
index 331b60eb81..abcce28f60 100644
--- a/tests/database.go
+++ b/tests/database.go
@@ -222,76 +222,6 @@ func marshalLabels(labelMap map[string]string, environmentName string) (string,
return string(labels), nil
}
-// getLabels gets all the labels.
-func (ctl *controller) getLabels() ([]*api.LabelKey, error) {
- body, err := ctl.get("/label", nil)
- if err != nil {
- return nil, err
- }
-
- var labelKeys []*api.LabelKey
- lks, err := jsonapi.UnmarshalManyPayload(body, reflect.TypeOf(new(api.LabelKey)))
- if err != nil {
- return nil, errors.Wrap(err, "fail to unmarshal get label response")
- }
- for _, lk := range lks {
- labelKey, ok := lk.(*api.LabelKey)
- if !ok {
- return nil, errors.Errorf("fail to convert label key")
- }
- labelKeys = append(labelKeys, labelKey)
- }
- return labelKeys, nil
-}
-
-// patchLabelKey patches the label key with given ID.
-func (ctl *controller) patchLabelKey(labelKeyPatch api.LabelKeyPatch) (*api.LabelKey, error) {
- buf := new(bytes.Buffer)
- if err := jsonapi.MarshalPayload(buf, &labelKeyPatch); err != nil {
- return nil, errors.Wrap(err, "failed to marshal label key patch")
- }
-
- body, err := ctl.patch(fmt.Sprintf("/label/%d", labelKeyPatch.ID), buf)
- if err != nil {
- return nil, err
- }
-
- labelKey := new(api.LabelKey)
- if err = jsonapi.UnmarshalPayload(body, labelKey); err != nil {
- return nil, errors.Wrap(err, "fail to unmarshal patch label key response")
- }
- return labelKey, nil
-}
-
-// addLabelValues adds values to an existing label key.
-func (ctl *controller) addLabelValues(key string, values []string) error {
- labelKeys, err := ctl.getLabels()
- if err != nil {
- return errors.Wrap(err, "failed to get labels")
- }
- var labelKey *api.LabelKey
- for _, lk := range labelKeys {
- if lk.Key == key {
- labelKey = lk
- break
- }
- }
- if labelKey == nil {
- return errors.Errorf("failed to find label with key %q", key)
- }
- var valueList []string
- valueList = append(valueList, labelKey.ValueList...)
- valueList = append(valueList, values...)
- _, err = ctl.patchLabelKey(api.LabelKeyPatch{
- ID: labelKey.ID,
- ValueList: valueList,
- })
- if err != nil {
- return errors.Wrapf(err, "failed to patch label key for key %q ID %d values %+v", key, labelKey.ID, valueList)
- }
- return nil
-}
-
func (ctl *controller) createDataSource(dataSourceCreate api.DataSourceCreate) error {
buf := new(bytes.Buffer)
if err := jsonapi.MarshalPayload(buf, &dataSourceCreate); err != nil {
diff --git a/tests/ghost_test.go b/tests/ghost_test.go
index b21857970f..0423fa571e 100644
--- a/tests/ghost_test.go
+++ b/tests/ghost_test.go
@@ -285,15 +285,6 @@ func TestGhostTenant(t *testing.T) {
prodInstances = append(prodInstances, instance)
}
- // Set up label values for tenants.
- // Prod and test are using the same tenant values. Use prodInstancesNumber because it's larger than testInstancesNumber.
- var tenants []string
- for i := 0; i < prodTenantNumber; i++ {
- tenants = append(tenants, fmt.Sprintf("tenant%d", i))
- }
- err = ctl.addLabelValues(api.TenantLabelKey, tenants)
- a.NoError(err)
-
// Create deployment configuration.
_, err = ctl.upsertDeploymentConfig(
api.DeploymentConfigUpsert{
diff --git a/tests/schema_update_test.go b/tests/schema_update_test.go
index 50ff44d1a3..30bdba6446 100644
--- a/tests/schema_update_test.go
+++ b/tests/schema_update_test.go
@@ -1173,17 +1173,17 @@ func TestWildcardInVCSFilePathTemplate(t *testing.T) {
{
name: "mixAsterisks",
vcsProviderCreator: fake.NewGitLab,
- envName: "生产",
+ envName: "prod1",
baseDirectory: "bbtest",
vcsType: vcs.GitLabSelfHost,
filePathTemplate: "{{ENV_NAME}}/**/foo/*/{{DB_NAME}}##{{VERSION}}##{{TYPE}}##{{DESCRIPTION}}.sql",
commitNewFileNames: []string{
// ** matches foo, foo matches foo, * matches bar
- fmt.Sprintf("%s/%s/foo/foo/bar/%s##ver1##migrate##create_table_t1.sql", baseDirectory, "生产", dbName),
+ fmt.Sprintf("%s/%s/foo/foo/bar/%s##ver1##migrate##create_table_t1.sql", baseDirectory, "prod1", dbName),
// ** matches foo/bar/foo, foo matches foo, * matches bar
- fmt.Sprintf("%s/%s/foo/bar/foo/foo/bar/%s##ver2##migrate##create_table_t2.sql", baseDirectory, "生产", dbName),
+ fmt.Sprintf("%s/%s/foo/bar/foo/foo/bar/%s##ver2##migrate##create_table_t2.sql", baseDirectory, "prod1", dbName),
// cannot match
- fmt.Sprintf("%s/%s/%s##ver3##migrate##create_table_t3.sql", baseDirectory, "生产", dbName),
+ fmt.Sprintf("%s/%s/%s##ver3##migrate##create_table_t3.sql", baseDirectory, "prod1", dbName),
},
commitNewFileContents: []string{
"CREATE TABLE t1 (id INT);",
diff --git a/tests/tenant_test.go b/tests/tenant_test.go
index 6afc6dc1bd..9d08d34e61 100644
--- a/tests/tenant_test.go
+++ b/tests/tenant_test.go
@@ -95,15 +95,6 @@ func TestTenant(t *testing.T) {
prodInstances = append(prodInstances, instance)
}
- // Set up label values for tenants.
- // Prod and test are using the same tenant values. Use prodInstancesNumber because it's larger than testInstancesNumber.
- var tenants []string
- for i := 0; i < prodTenantNumber; i++ {
- tenants = append(tenants, fmt.Sprintf("tenant%d", i))
- }
- err = ctl.addLabelValues(api.TenantLabelKey, tenants)
- a.NoError(err)
-
// Create deployment configuration.
_, err = ctl.upsertDeploymentConfig(
api.DeploymentConfigUpsert{
@@ -396,15 +387,6 @@ func TestTenantVCS(t *testing.T) {
prodInstances = append(prodInstances, instance)
}
- // Set up label values for tenants.
- // Prod and test are using the same tenant values. Use prodInstancesNumber because it's larger than testInstancesNumber.
- var tenants []string
- for i := 0; i < prodTenantNumber; i++ {
- tenants = append(tenants, fmt.Sprintf("tenant%d", i))
- }
- err = ctl.addLabelValues(api.TenantLabelKey, tenants)
- a.NoError(err)
-
// Create deployment configuration.
_, err = ctl.upsertDeploymentConfig(
api.DeploymentConfigUpsert{
@@ -573,15 +555,6 @@ func TestTenantDatabaseNameTemplate(t *testing.T) {
})
a.NoError(err)
- // Set up label values for tenants.
- // Prod and test are using the same tenant values. Use prodTenantNumber because it's larger than testInstancesNumber.
- var tenants []string
- for i := 0; i < prodTenantNumber; i++ {
- tenants = append(tenants, fmt.Sprintf("tenant%d", i))
- }
- err = ctl.addLabelValues(api.TenantLabelKey, tenants)
- a.NoError(err)
-
// Create deployment configuration.
_, err = ctl.upsertDeploymentConfig(
api.DeploymentConfigUpsert{
@@ -862,15 +835,6 @@ func TestTenantVCSDatabaseNameTemplate(t *testing.T) {
prodInstances = append(prodInstances, instance)
}
- // Set up label values for tenants.
- // Prod and test are using the same tenant values. Use prodInstancesNumber because it's larger than testInstancesNumber.
- var tenants []string
- for i := 0; i < prodTenantNumber; i++ {
- tenants = append(tenants, fmt.Sprintf("tenant%d", i))
- }
- err = ctl.addLabelValues(api.TenantLabelKey, tenants)
- a.NoError(err)
-
// Create deployment configuration.
_, err = ctl.upsertDeploymentConfig(
api.DeploymentConfigUpsert{