Skip to content

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

feat: alter instance table for BYT-3419 (#6714)

ecmadaoa59e0726aaef
diff --git a/backend/api/v1/instance_service.go b/backend/api/v1/instance_service.go
index fdf7187af2..cf98b6f31d 100644
--- a/backend/api/v1/instance_service.go
+++ b/backend/api/v1/instance_service.go
@@ -118,10 +118,21 @@ func (s *InstanceService) CreateInstance(ctx context.Context, request *v1pb.Crea
return convertToInstance(instanceMessage), nil
}
+ instanceCountLimit := s.licenseService.LoadSubscription(ctx).InstanceCount
+ if instanceMessage.Activation {
+ if err := s.store.CheckActivationLimit(ctx, instanceCountLimit); err != nil {
+ if common.ErrorCode(err) == common.Invalid {
+ return nil, status.Errorf(codes.ResourceExhausted, err.Error())
+ }
+ return nil, status.Errorf(codes.Internal, err.Error())
+ }
+ }
+
principalID := ctx.Value(common.PrincipalIDContextKey).(int)
instance, err := s.store.CreateInstanceV2(ctx,
instanceMessage,
principalID,
+ instanceCountLimit,
)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
@@ -184,10 +195,24 @@ func (s *InstanceService) UpdateInstance(ctx context.Context, request *v1pb.Upda
return nil, status.Errorf(codes.InvalidArgument, err.Error())
}
patch.DataSources = &datasourceList
+ case "activation":
+ patch.Activation = &request.Instance.Activation
+ default:
+ return nil, status.Errorf(codes.InvalidArgument, `unsupport update_mask "%s"`, path)
}
}
- ins, err := s.store.UpdateInstanceV2(ctx, patch)
+ instanceCountLimit := s.licenseService.LoadSubscription(ctx).InstanceCount
+ if v := patch.Activation; v != nil && *v {
+ if err := s.store.CheckActivationLimit(ctx, instanceCountLimit); err != nil {
+ if common.ErrorCode(err) == common.Invalid {
+ return nil, status.Errorf(codes.ResourceExhausted, err.Error())
+ }
+ return nil, status.Errorf(codes.Internal, err.Error())
+ }
+ }
+
+ ins, err := s.store.UpdateInstanceV2(ctx, patch, instanceCountLimit)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
@@ -313,7 +338,7 @@ func (s *InstanceService) DeleteInstance(ctx context.Context, request *v1pb.Dele
EnvironmentID: instance.EnvironmentID,
ResourceID: instance.ResourceID,
Delete: &deletePatch,
- }); err != nil {
+ }, -1 /* don't need to pass the instance limition */); err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
@@ -335,7 +360,7 @@ func (s *InstanceService) UndeleteInstance(ctx context.Context, request *v1pb.Un
EnvironmentID: instance.EnvironmentID,
ResourceID: instance.ResourceID,
Delete: &undeletePatch,
- })
+ }, -1 /* don't need to pass the instance limition */)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
@@ -522,6 +547,8 @@ func (s *InstanceService) UpdateDataSource(ctx context.Context, request *v1pb.Up
obfuscated := common.Obfuscate(request.DataSource.SshPrivateKey, s.secret)
patch.SSHObfuscatedPrivateKey = &obfuscated
dataSource.SSHObfuscatedPrivateKey = obfuscated
+ default:
+ return nil, status.Errorf(codes.InvalidArgument, `unsupport update_mask "%s"`, path)
}
}
@@ -681,6 +708,7 @@ func convertToInstance(instance *store.InstanceMessage) *v1pb.Instance {
DataSources: dataSourceList,
State: convertDeletedToState(instance.Deleted),
Environment: fmt.Sprintf("environments/%s", instance.EnvironmentID),
+ Activation: instance.Activation,
}
}
@@ -701,6 +729,7 @@ func (s *InstanceService) convertToInstanceMessage(instanceID string, instance *
ExternalLink: instance.ExternalLink,
DataSources: datasources,
EnvironmentID: environmentID,
+ Activation: instance.Activation,
}, nil
}
diff --git a/backend/migrator/migration/dev/LATEST.sql b/backend/migrator/migration/dev/LATEST.sql
index 1093f62746..6fabbe9542 100644
--- a/backend/migrator/migration/dev/LATEST.sql
+++ b/backend/migrator/migration/dev/LATEST.sql
@@ -330,7 +330,9 @@ CREATE TABLE instance (
engine TEXT NOT NULL,
engine_version TEXT NOT NULL DEFAULT '',
external_link TEXT NOT NULL DEFAULT '',
- resource_id TEXT NOT NULL
+ resource_id TEXT NOT NULL,
+ -- activation should set to be TRUE if users assign license to this instance.
+ activation BOOLEAN NOT NULL DEFAULT false
);
CREATE UNIQUE INDEX idx_instance_unique_resource_id ON instance(resource_id);
diff --git a/backend/migrator/migration/prod/2.4/0000##add_activation_to_instance.sql b/backend/migrator/migration/prod/2.4/0000##add_activation_to_instance.sql
new file mode 100644
index 0000000000..db40bb363b
--- /dev/null
+++ b/backend/migrator/migration/prod/2.4/0000##add_activation_to_instance.sql
@@ -0,0 +1 @@
+ALTER TABLE instance ADD COLUMN activation BOOLEAN NOT NULL DEFAULT false;
\ No newline at end of file
diff --git a/backend/migrator/migration/prod/LATEST.sql b/backend/migrator/migration/prod/LATEST.sql
index 5a4d75c49c..e3386ce9d7 100644
--- a/backend/migrator/migration/prod/LATEST.sql
+++ b/backend/migrator/migration/prod/LATEST.sql
@@ -329,7 +329,9 @@ CREATE TABLE instance (
engine TEXT NOT NULL,
engine_version TEXT NOT NULL DEFAULT '',
external_link TEXT NOT NULL DEFAULT '',
- resource_id TEXT NOT NULL
+ resource_id TEXT NOT NULL,
+ -- activation should set to be TRUE if users assign license to this instance.
+ activation BOOLEAN NOT NULL DEFAULT false
);
CREATE UNIQUE INDEX idx_instance_unique_resource_id ON instance(resource_id);
diff --git a/backend/migrator/migrator_test.go b/backend/migrator/migrator_test.go
index f6281f4ccf..18e83e3e21 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("2.3.2"), releaseVersion)
+ require.Equal(t, semver.MustParse("2.4.0"), releaseVersion)
}
diff --git a/backend/runner/schemasync/syncer.go b/backend/runner/schemasync/syncer.go
index d445c6207d..da593ed98c 100644
--- a/backend/runner/schemasync/syncer.go
+++ b/backend/runner/schemasync/syncer.go
@@ -181,7 +181,7 @@ func (s *Syncer) SyncInstance(ctx context.Context, instance *store.InstanceMessa
EnvironmentID: instance.EnvironmentID,
ResourceID: instance.ResourceID,
EngineVersion: &instanceMeta.Version,
- }); err != nil {
+ }, -1); err != nil {
return nil, err
}
}
diff --git a/backend/server/server.go b/backend/server/server.go
index 6c78b5991a..623c625ee4 100644
--- a/backend/server/server.go
+++ b/backend/server/server.go
@@ -1072,7 +1072,7 @@ func (s *Server) generateOnboardingData(ctx context.Context, userID int) error {
},
},
EnvironmentID: api.DefaultProdEnvironmentID,
- }, userID)
+ }, userID, -1)
if err != nil {
return errors.Wrapf(err, "failed to create onboarding instance")
}
diff --git a/backend/store/instance.go b/backend/store/instance.go
index ead3ebaa72..888009f3d4 100644
--- a/backend/store/instance.go
+++ b/backend/store/instance.go
@@ -94,6 +94,7 @@ type InstanceMessage struct {
Engine db.Type
ExternalLink string
DataSources []*DataSourceMessage
+ Activation bool
// Output only.
EnvironmentID string
UID int
@@ -108,6 +109,7 @@ type UpdateInstanceMessage struct {
Delete *bool
DataSources *[]*DataSourceMessage
EngineVersion *string
+ Activation *bool
// Output only.
UpdaterID int
@@ -189,7 +191,7 @@ func (s *Store) ListInstancesV2(ctx context.Context, find *FindInstanceMessage)
}
// CreateInstanceV2 creates the instance.
-func (s *Store) CreateInstanceV2(ctx context.Context, instanceCreate *InstanceMessage, creatorID int) (*InstanceMessage, error) {
+func (s *Store) CreateInstanceV2(ctx context.Context, instanceCreate *InstanceMessage, creatorID, maximumActivation int) (*InstanceMessage, error) {
if err := validateDataSourceList(instanceCreate.DataSources); err != nil {
return nil, err
}
@@ -211,8 +213,13 @@ func (s *Store) CreateInstanceV2(ctx context.Context, instanceCreate *InstanceMe
return nil, common.Errorf(common.NotFound, "environment %s not found", instanceCreate.EnvironmentID)
}
+ where := ""
+ if instanceCreate.Activation {
+ where = fmt.Sprintf("(%s) < %d", countActivateInstanceQuery, maximumActivation)
+ }
+
var instanceID int
- if err := tx.QueryRowContext(ctx, `
+ if err := tx.QueryRowContext(ctx, fmt.Sprintf(`
INSERT INTO instance (
resource_id,
creator_id,
@@ -220,11 +227,13 @@ func (s *Store) CreateInstanceV2(ctx context.Context, instanceCreate *InstanceMe
environment_id,
name,
engine,
- external_link
+ external_link,
+ activation
)
- VALUES ($1, $2, $3, $4, $5, $6, $7)
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
+ %s
RETURNING id
- `,
+ `, where),
instanceCreate.ResourceID,
creatorID,
creatorID,
@@ -232,6 +241,7 @@ func (s *Store) CreateInstanceV2(ctx context.Context, instanceCreate *InstanceMe
instanceCreate.Title,
instanceCreate.Engine,
instanceCreate.ExternalLink,
+ instanceCreate.Activation,
).Scan(&instanceID); err != nil {
return nil, err
}
@@ -264,6 +274,7 @@ func (s *Store) CreateInstanceV2(ctx context.Context, instanceCreate *InstanceMe
Engine: instanceCreate.Engine,
ExternalLink: instanceCreate.ExternalLink,
DataSources: dataSources,
+ Activation: instanceCreate.Activation,
}
s.instanceCache.Store(getInstanceCacheKey(instance.ResourceID), instance)
s.instanceIDCache.Store(instance.UID, instance)
@@ -271,14 +282,14 @@ func (s *Store) CreateInstanceV2(ctx context.Context, instanceCreate *InstanceMe
}
// UpdateInstanceV2 updates an instance.
-func (s *Store) UpdateInstanceV2(ctx context.Context, patch *UpdateInstanceMessage) (*InstanceMessage, error) {
+func (s *Store) UpdateInstanceV2(ctx context.Context, patch *UpdateInstanceMessage, maximumActivation int) (*InstanceMessage, error) {
if patch.DataSources != nil {
if err := validateDataSourceList(*patch.DataSources); err != nil {
return nil, err
}
}
- set, args := []string{"updater_id = $1"}, []any{fmt.Sprintf("%d", patch.UpdaterID)}
+ set, args, where := []string{"updater_id = $1"}, []any{fmt.Sprintf("%d", patch.UpdaterID)}, []string{}
if v := patch.Title; v != nil {
set, args = append(set, fmt.Sprintf("name = $%d", len(args)+1)), append(args, *v)
}
@@ -288,10 +299,20 @@ func (s *Store) UpdateInstanceV2(ctx context.Context, patch *UpdateInstanceMessa
if v := patch.EngineVersion; v != nil {
set, args = append(set, fmt.Sprintf("engine_version = $%d", len(args)+1)), append(args, *v)
}
+ if v := patch.Activation; v != nil {
+ set, args = append(set, fmt.Sprintf("activation = $%d", len(args)+1)), append(args, *v)
+ if *v {
+ where = append(where, fmt.Sprintf("(%s) < %d", countActivateInstanceQuery, maximumActivation))
+ }
+ }
if v := patch.Delete; v != nil {
+ if patch.Activation != nil {
+ return nil, errors.Errorf(`cannot set "activation" and "row_status" at the same time`)
+ }
rowStatus := api.Normal
if *patch.Delete {
rowStatus = api.Archived
+ set, args = append(set, fmt.Sprintf("activation = $%d", len(args)+1)), append(args, false)
}
set, args = append(set, fmt.Sprintf(`"row_status" = $%d`, len(args)+1)), append(args, rowStatus)
}
@@ -314,34 +335,36 @@ func (s *Store) UpdateInstanceV2(ctx context.Context, patch *UpdateInstanceMessa
}
args = append(args, patch.ResourceID, environment.UID)
+ where = append(where, fmt.Sprintf("resource_id = $%d", len(args)-1), fmt.Sprintf("environment_id = $%d", len(args)))
instance := &InstanceMessage{
EnvironmentID: patch.EnvironmentID,
}
- var rowStatus string
- if err := tx.QueryRowContext(ctx, fmt.Sprintf(`
+ query := fmt.Sprintf(`
UPDATE instance
SET `+strings.Join(set, ", ")+`
- WHERE resource_id = $%d AND environment_id = $%d
+ WHERE %s
RETURNING
id,
resource_id,
name,
engine,
external_link,
+ activation,
row_status
- `, len(args)-1, len(args)),
- args...,
- ).Scan(
+ `, strings.Join(where, " AND "))
+ var rowStatus string
+ if err := tx.QueryRowContext(ctx, query, args...).Scan(
&instance.UID,
&instance.ResourceID,
&instance.Title,
&instance.Engine,
&instance.ExternalLink,
+ &instance.Activation,
&rowStatus,
); err != nil {
if err == sql.ErrNoRows {
- return nil, nil
+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)
}
return nil, err
}
@@ -408,6 +431,7 @@ func (s *Store) listInstanceImplV2(ctx context.Context, tx *Tx, find *FindInstan
engine,
engine_version,
external_link,
+ activation,
instance.row_status AS row_status
FROM instance
LEFT JOIN environment ON environment.id = instance.environment_id
@@ -429,6 +453,7 @@ func (s *Store) listInstanceImplV2(ctx context.Context, tx *Tx, find *FindInstan
&instanceMessage.Engine,
&instanceMessage.EngineVersion,
&instanceMessage.ExternalLink,
+ &instanceMessage.Activation,
&rowStatus,
); err != nil {
return nil, err
@@ -493,6 +518,21 @@ func (s *Store) FindInstanceWithDatabaseBackupEnabled(ctx context.Context) ([]*I
return instances, nil
}
+var countActivateInstanceQuery = "SELECT COUNT(*) FROM instance WHERE activation = TRUE"
+
+// CheckActivationLimit checks if activation instance count reaches the limit.
+func (s *Store) CheckActivationLimit(ctx context.Context, maximumActivation int) error {
+ count := 0
+ if err := s.db.db.QueryRowContext(ctx, countActivateInstanceQuery).Scan(&count); err != nil {
+ return err
+ }
+
+ if count >= maximumActivation {
+ return common.Errorf(common.Invalid, "activation instance count reaches the limitation")
+ }
+ return nil
+}
+
func validateDataSourceList(dataSources []*DataSourceMessage) error {
dataSourceMap := map[api.DataSourceType]bool{}
for _, dataSource := range dataSources {
diff --git a/frontend/src/types/proto/v1/instance_service.ts b/frontend/src/types/proto/v1/instance_service.ts
index a6b0afcff7..36595d90dd 100644
--- a/frontend/src/types/proto/v1/instance_service.ts
+++ b/frontend/src/types/proto/v1/instance_service.ts
@@ -209,6 +209,7 @@ export interface Instance {
* Format: environments/prod where prod is the environment resource ID.
*/
environment: string;
+ activation: boolean;
}
export interface DataSource {
@@ -1179,6 +1180,7 @@ function createBaseInstance(): Instance {
externalLink: "",
dataSources: [],
environment: "",
+ activation: false,
};
}
@@ -1211,6 +1213,9 @@ export const Instance = {
if (message.environment !== "") {
writer.uint32(74).string(message.environment);
}
+ if (message.activation === true) {
+ writer.uint32(80).bool(message.activation);
+ }
return writer;
},
@@ -1284,6 +1289,13 @@ export const Instance = {
message.environment = reader.string();
continue;
+ case 10:
+ if (tag !== 80) {
+ break;
+ }
+
+ message.activation = reader.bool();
+ continue;
}
if ((tag & 7) === 4 || tag === 0) {
break;
@@ -1304,6 +1316,7 @@ export const Instance = {
externalLink: isSet(object.externalLink) ? String(object.externalLink) : "",
dataSources: Array.isArray(object?.dataSources) ? object.dataSources.map((e: any) => DataSource.fromJSON(e)) : [],
environment: isSet(object.environment) ? String(object.environment) : "",
+ activation: isSet(object.activation) ? Boolean(object.activation) : false,
};
},
@@ -1322,6 +1335,7 @@ export const Instance = {
obj.dataSources = [];
}
message.environment !== undefined && (obj.environment = message.environment);
+ message.activation !== undefined && (obj.activation = message.activation);
return obj;
},
@@ -1340,6 +1354,7 @@ export const Instance = {
message.externalLink = object.externalLink ?? "";
message.dataSources = object.dataSources?.map((e) => DataSource.fromPartial(e)) || [];
message.environment = object.environment ?? "";
+ message.activation = object.activation ?? false;
return message;
},
};
diff --git a/proto/gen/grpc-doc/v1/README.md b/proto/gen/grpc-doc/v1/README.md
index 2278697104..a94a4a72b7 100644
--- a/proto/gen/grpc-doc/v1/README.md
+++ b/proto/gen/grpc-doc/v1/README.md
@@ -4231,6 +4231,7 @@ This value should be 4-63 characters, and valid characters are /[a-z][0-9]-/. |
| external_link | [string](#string) | | |
| data_sources | [DataSource](#bytebase-v1-DataSource) | repeated | |
| environment | [string](#string) | | The environment resource. Format: environments/prod where prod is the environment resource ID. |
+| activation | [bool](#bool) | | |
diff --git a/proto/generated-go/v1/instance_service.pb.go b/proto/generated-go/v1/instance_service.pb.go
index 66ebcf8db9..78108c13e9 100644
--- a/proto/generated-go/v1/instance_service.pb.go
+++ b/proto/generated-go/v1/instance_service.pb.go
@@ -847,6 +847,7 @@ type Instance struct {
// The environment resource.
// Format: environments/prod where prod is the environment resource ID.
Environment string `protobuf:"bytes,9,opt,name=environment,proto3" json:"environment,omitempty"`
+ Activation bool `protobuf:"varint,10,opt,name=activation,proto3" json:"activation,omitempty"`
}
func (x *Instance) Reset() {
@@ -944,6 +945,13 @@ func (x *Instance) GetEnvironment() string {
return ""
}
+func (x *Instance) GetActivation() bool {
+ if x != nil {
+ return x.Activation
+ }
+ return false
+}
+
type DataSource struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
@@ -1245,7 +1253,7 @@ var file_v1_instance_service_proto_rawDesc = []byte{
0x0a, 0x16, 0x53, 0x79, 0x6e, 0x63, 0x53, 0x6c, 0x6f, 0x77, 0x51, 0x75, 0x65, 0x72, 0x69, 0x65,
0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1f, 0x0a, 0x08, 0x69, 0x6e, 0x73, 0x74,
0x61, 0x6e, 0x63, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x42, 0x03, 0xe0, 0x41, 0x02, 0x52,
- 0x08, 0x69, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x22, 0xd1, 0x02, 0x0a, 0x08, 0x49, 0x6e,
+ 0x08, 0x69, 0x6e, 0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x22, 0xf1, 0x02, 0x0a, 0x08, 0x49, 0x6e,
0x73, 0x74, 0x61, 0x6e, 0x63, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01,
0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x15, 0x0a, 0x03, 0x75, 0x69,
0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x42, 0x03, 0xe0, 0x41, 0x03, 0x52, 0x03, 0x75, 0x69,
@@ -1266,7 +1274,9 @@ var file_v1_instance_service_proto_rawDesc = []byte{
0x2e, 0x76, 0x31, 0x2e, 0x44, 0x61, 0x74, 0x61, 0x53, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x52, 0x0b,
0x64, 0x61, 0x74, 0x61, 0x53, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x73, 0x12, 0x20, 0x0a, 0x0b, 0x65,
0x6e, 0x76, 0x69, 0x72, 0x6f, 0x6e, 0x6d, 0x65, 0x6e, 0x74, 0x18, 0x09, 0x20, 0x01, 0x28, 0x09,
- 0x52, 0x0b, 0x65, 0x6e, 0x76, 0x69, 0x72, 0x6f, 0x6e, 0x6d, 0x65, 0x6e, 0x74, 0x22, 0xd4, 0x04,
+ 0x52, 0x0b, 0x65, 0x6e, 0x76, 0x69, 0x72, 0x6f, 0x6e, 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x1e, 0x0a,
+ 0x0a, 0x61, 0x63, 0x74, 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x0a, 0x20, 0x01, 0x28,
+ 0x08, 0x52, 0x0a, 0x61, 0x63, 0x74, 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0xd4, 0x04,
0x0a, 0x0a, 0x44, 0x61, 0x74, 0x61, 0x53, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x12, 0x14, 0x0a, 0x05,
0x74, 0x69, 0x74, 0x6c, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x69, 0x74,
0x6c, 0x65, 0x12, 0x2f, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0e,
diff --git a/proto/v1/instance_service.proto b/proto/v1/instance_service.proto
index 4f5c3ca423..0df21e4d9f 100644
--- a/proto/v1/instance_service.proto
+++ b/proto/v1/instance_service.proto
@@ -235,6 +235,8 @@ message Instance {
// The environment resource.
// Format: environments/prod where prod is the environment resource ID.
string environment = 9;
+
+ bool activation = 10;
}
message DataSource {