Skip to content

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

feat(schema): custom role (#5590)

p0nycf316847df30
diff --git a/backend/migrator/migration/dev/LATEST.sql b/backend/migrator/migration/dev/LATEST.sql
index 01e629f4a8..7cb6ab422f 100644
--- a/backend/migrator/migration/dev/LATEST.sql
+++ b/backend/migrator/migration/dev/LATEST.sql
@@ -102,6 +102,30 @@ UPDATE
ON setting FOR EACH ROW
EXECUTE FUNCTION trigger_update_updated_ts();
+-- Role
+CREATE TABLE role (
+ id BIGSERIAL PRIMARY KEY,
+ row_status row_status NOT NULL DEFAULT 'NORMAL',
+ creator_id INTEGER NOT NULL REFERENCES principal (id),
+ created_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
+ updater_id INTEGER NOT NULL REFERENCES principal (id),
+ updated_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
+ resource_id TEXT NOT NULL, -- user-defined id, such as projectDBA
+ description TEXT NOT NULL,
+ permissions JSONB NOT NULL DEFAULT '{}', -- saved for future use
+ payload JSONB NOT NULL DEFAULT '{}' -- saved for future use
+);
+
+CREATE UNIQUE INDEX idx_role_unique_resource_id on role (resource_id);
+
+ALTER SEQUENCE role_id_seq RESTART WITH 101;
+
+CREATE TRIGGER update_role_updated_ts
+BEFORE
+UPDATE
+ ON role FOR EACH ROW
+EXECUTE FUNCTION trigger_update_updated_ts();
+
-- Member
-- We separate the concept from Principal because if we support multiple workspace in the future, each workspace can have different member for the same principal
CREATE TABLE member (
@@ -249,7 +273,7 @@ CREATE TABLE project_member (
updater_id INTEGER NOT NULL REFERENCES principal (id),
updated_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
project_id INTEGER NOT NULL REFERENCES project (id),
- role TEXT NOT NULL CHECK (role IN ('OWNER', 'DEVELOPER')),
+ role TEXT NOT NULL,
principal_id INTEGER NOT NULL REFERENCES principal (id),
payload JSONB NOT NULL DEFAULT '{}'
);
@@ -983,7 +1007,7 @@ UPDATE
ON label_value FOR EACH ROW
EXECUTE FUNCTION trigger_update_updated_ts();
--- db_label stores labels asscociated with databases.
+-- db_label stores labels associated with databases.
CREATE TABLE db_label (
id SERIAL PRIMARY KEY,
row_status row_status NOT NULL DEFAULT 'NORMAL',
diff --git a/backend/migrator/migration/prod/1.17/0000##custom_role.sql b/backend/migrator/migration/prod/1.17/0000##custom_role.sql
new file mode 100644
index 0000000000..88d6e5536a
--- /dev/null
+++ b/backend/migrator/migration/prod/1.17/0000##custom_role.sql
@@ -0,0 +1,24 @@
+CREATE TABLE role (
+ id BIGSERIAL PRIMARY KEY,
+ row_status row_status NOT NULL DEFAULT 'NORMAL',
+ creator_id INTEGER NOT NULL REFERENCES principal (id),
+ created_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
+ updater_id INTEGER NOT NULL REFERENCES principal (id),
+ updated_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
+ resource_id TEXT NOT NULL, -- user-defined id, such as projectDBA
+ description TEXT NOT NULL,
+ permissions JSONB NOT NULL DEFAULT '{}', -- saved for future use
+ payload JSONB NOT NULL DEFAULT '{}' -- saved for future use
+);
+
+CREATE UNIQUE INDEX idx_role_unique_resource_id on role (resource_id);
+
+ALTER SEQUENCE role_id_seq RESTART WITH 101;
+
+CREATE TRIGGER update_role_updated_ts
+BEFORE
+UPDATE
+ ON role FOR EACH ROW
+EXECUTE FUNCTION trigger_update_updated_ts();
+
+ALTER TABLE project_member DROP CONSTRAINT project_member_role_check;
diff --git a/backend/migrator/migration/prod/LATEST.sql b/backend/migrator/migration/prod/LATEST.sql
index 94b102ac74..897fae1117 100644
--- a/backend/migrator/migration/prod/LATEST.sql
+++ b/backend/migrator/migration/prod/LATEST.sql
@@ -102,6 +102,30 @@ UPDATE
ON setting FOR EACH ROW
EXECUTE FUNCTION trigger_update_updated_ts();
+-- Role
+CREATE TABLE role (
+ id BIGSERIAL PRIMARY KEY,
+ row_status row_status NOT NULL DEFAULT 'NORMAL',
+ creator_id INTEGER NOT NULL REFERENCES principal (id),
+ created_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
+ updater_id INTEGER NOT NULL REFERENCES principal (id),
+ updated_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
+ resource_id TEXT NOT NULL, -- user-defined id, such as projectDBA
+ description TEXT NOT NULL,
+ permissions JSONB NOT NULL DEFAULT '{}', -- saved for future use
+ payload JSONB NOT NULL DEFAULT '{}' -- saved for future use
+);
+
+CREATE UNIQUE INDEX idx_role_unique_resource_id on role (resource_id);
+
+ALTER SEQUENCE role_id_seq RESTART WITH 101;
+
+CREATE TRIGGER update_role_updated_ts
+BEFORE
+UPDATE
+ ON role FOR EACH ROW
+EXECUTE FUNCTION trigger_update_updated_ts();
+
-- Member
-- We separate the concept from Principal because if we support multiple workspace in the future, each workspace can have different member for the same principal
CREATE TABLE member (
@@ -248,7 +272,7 @@ CREATE TABLE project_member (
updater_id INTEGER NOT NULL REFERENCES principal (id),
updated_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
project_id INTEGER NOT NULL REFERENCES project (id),
- role TEXT NOT NULL CHECK (role IN ('OWNER', 'DEVELOPER')),
+ role TEXT NOT NULL,
principal_id INTEGER NOT NULL REFERENCES principal (id),
payload JSONB NOT NULL DEFAULT '{}'
);
@@ -982,7 +1006,7 @@ UPDATE
ON label_value FOR EACH ROW
EXECUTE FUNCTION trigger_update_updated_ts();
--- db_label stores labels asscociated with databases.
+-- db_label stores labels associated with databases.
CREATE TABLE db_label (
id SERIAL PRIMARY KEY,
row_status row_status NOT NULL DEFAULT 'NORMAL',
diff --git a/backend/migrator/migrator_test.go b/backend/migrator/migrator_test.go
index 2dc3cbd24f..1c9fb804e4 100644
--- a/backend/migrator/migrator_test.go
+++ b/backend/migrator/migrator_test.go
@@ -241,5 +241,5 @@ func TestMigrationCompatibility(t *testing.T) {
func TestGetCutoffVersion(t *testing.T) {
releaseVersion, err := getProdCutoffVersion()
require.NoError(t, err)
- require.Equal(t, semver.MustParse("1.16.2"), releaseVersion)
+ require.Equal(t, semver.MustParse("1.17.0"), releaseVersion)
}