Skip to content

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

chore: data source from instance (#7426)

Danny Xu222616d7e8e9
diff --git a/backend/legacyapi/database.go b/backend/legacyapi/database.go
index c0e4850c7a..598a1e4ab1 100644
--- a/backend/legacyapi/database.go
+++ b/backend/legacyapi/database.go
@@ -4,11 +4,6 @@ import (
"encoding/json"
)
-const (
- // AllDatabaseName is the wild expression for all databases.
- AllDatabaseName = "*"
-)
-
// SyncStatus is the database sync status.
type SyncStatus string
@@ -49,9 +44,8 @@ type DatabaseFind struct {
InstanceID *int
// Domain specific fields
- Name *string
- IncludeAllDatabase bool
- SyncStatus *SyncStatus
+ Name *string
+ SyncStatus *SyncStatus
}
func (find *DatabaseFind) String() string {
diff --git a/backend/migrator/demo/default/10070##db.sql b/backend/migrator/demo/default/10070##db.sql
index a45ff7e333..df338240ca 100644
--- a/backend/migrator/demo/default/10070##db.sql
+++ b/backend/migrator/demo/default/10070##db.sql
@@ -1,29 +1,4 @@
-- Database for sandbox environment instance 6001
-INSERT INTO
- db (
- id,
- creator_id,
- updater_id,
- instance_id,
- project_id,
- name,
- sync_status,
- last_successful_sync_ts,
- schema_version
- )
-VALUES
- (
- 7001,
- 101,
- 101,
- 6001,
- 1,
- '*',
- 'OK',
- 0,
- ''
- );
-
INSERT INTO
db (
id,
@@ -100,31 +75,6 @@ VALUES
);
-- Database for integration environment instance 6002
-INSERT INTO
- db (
- id,
- creator_id,
- updater_id,
- instance_id,
- project_id,
- name,
- sync_status,
- last_successful_sync_ts,
- schema_version
- )
-VALUES
- (
- 7005,
- 101,
- 101,
- 6002,
- 1,
- '*',
- 'OK',
- 0,
- ''
- );
-
INSERT INTO
db (
id,
@@ -201,31 +151,6 @@ VALUES
);
-- Database for staging environment instance 6003
-INSERT INTO
- db (
- id,
- creator_id,
- updater_id,
- instance_id,
- project_id,
- name,
- sync_status,
- last_successful_sync_ts,
- schema_version
- )
-VALUES
- (
- 7009,
- 101,
- 101,
- 6003,
- 1,
- '*',
- 'OK',
- 0,
- ''
- );
-
INSERT INTO
db (
id,
@@ -302,31 +227,6 @@ VALUES
);
-- Database for prod environment instance 6004
-INSERT INTO
- db (
- id,
- creator_id,
- updater_id,
- instance_id,
- project_id,
- name,
- sync_status,
- last_successful_sync_ts,
- schema_version
- )
-VALUES
- (
- 7013,
- 101,
- 101,
- 6004,
- 1,
- '*',
- 'OK',
- 0,
- ''
- );
-
INSERT INTO
db (
id,
@@ -402,134 +302,5 @@ VALUES
''
);
--- Database for dev environment PostgreSQL instance 6005
-INSERT INTO
- db (
- id,
- creator_id,
- updater_id,
- instance_id,
- project_id,
- name,
- sync_status,
- last_successful_sync_ts,
- schema_version
- )
-VALUES
- (
- 7017,
- 101,
- 101,
- 6005,
- 1,
- '*',
- 'OK',
- 0,
- ''
- );
-
--- Wildcard database for dev environment PostgreSQL instance 6006
-INSERT INTO
- db (
- id,
- creator_id,
- updater_id,
- instance_id,
- project_id,
- name,
- sync_status,
- last_successful_sync_ts,
- schema_version
- )
-VALUES
- (
- 7018,
- 101,
- 101,
- 6006,
- 1,
- '*',
- 'OK',
- 0,
- ''
- );
-
--- Database for dev environment TiDB instance 6007
-INSERT INTO
- db (
- id,
- creator_id,
- updater_id,
- instance_id,
- project_id,
- name,
- sync_status,
- last_successful_sync_ts,
- schema_version
- )
-VALUES
- (
- 7020,
- 101,
- 101,
- 6007,
- 1,
- '*',
- 'OK',
- 0,
- ''
- );
-
--- Database for dev environment ClickHouse instance 6008
-INSERT INTO
- db (
- id,
- creator_id,
- updater_id,
- instance_id,
- project_id,
- name,
- sync_status,
- last_successful_sync_ts,
- schema_version
- )
-VALUES
- (
- 7021,
- 101,
- 101,
- 6008,
- 1,
- '*',
- 'OK',
- 0,
- ''
- );
-
--- Database for dev environment Snowflake instance 6009
-INSERT INTO
- db (
- id,
- creator_id,
- updater_id,
- instance_id,
- project_id,
- name,
- sync_status,
- last_successful_sync_ts,
- schema_version
- )
-VALUES
- (
- 7022,
- 101,
- 101,
- 6009,
- 1,
- '*',
- 'OK',
- 0,
- ''
- );
ALTER SEQUENCE db_id_seq RESTART WITH 7023;
diff --git a/backend/migrator/demo/default/10080##data_source.sql b/backend/migrator/demo/default/10080##data_source.sql
index c94e6379e8..440a28a3d6 100644
--- a/backend/migrator/demo/default/10080##data_source.sql
+++ b/backend/migrator/demo/default/10080##data_source.sql
@@ -4,7 +4,6 @@ INSERT INTO
creator_id,
updater_id,
instance_id,
- database_id,
name,
TYPE,
host,
@@ -18,7 +17,6 @@ VALUES
1,
1,
6001,
- 7001,
'Admin data source',
'ADMIN',
'127.0.0.1',
@@ -33,7 +31,6 @@ INSERT INTO
creator_id,
updater_id,
instance_id,
- database_id,
name,
TYPE,
host,
@@ -47,7 +44,6 @@ VALUES
1,
1,
6002,
- 7005,
'Admin data source',
'ADMIN',
'mysql.integration.example.com',
@@ -62,7 +58,6 @@ INSERT INTO
creator_id,
updater_id,
instance_id,
- database_id,
name,
TYPE,
host,
@@ -76,7 +71,6 @@ VALUES
1,
1,
6003,
- 7009,
'Admin data source',
'ADMIN',
'mysql.staging.example.com',
@@ -91,7 +85,6 @@ INSERT INTO
creator_id,
updater_id,
instance_id,
- database_id,
name,
TYPE,
host,
@@ -105,7 +98,6 @@ VALUES
1,
1,
6004,
- 7013,
'Admin data source',
'ADMIN',
'mysql.prod.example.com',
@@ -120,7 +112,6 @@ INSERT INTO
creator_id,
updater_id,
instance_id,
- database_id,
name,
TYPE,
host,
@@ -134,7 +125,6 @@ VALUES
1,
1,
6005,
- 7017,
'Admin data source',
'ADMIN',
'127.0.0.1',
@@ -149,7 +139,6 @@ INSERT INTO
creator_id,
updater_id,
instance_id,
- database_id,
name,
TYPE,
host,
@@ -163,7 +152,6 @@ VALUES
1,
1,
6006,
- 7018,
'Admin data source',
'ADMIN',
'dpg-c8a7pcd0mal7gtod05p0',
@@ -178,7 +166,6 @@ INSERT INTO
creator_id,
updater_id,
instance_id,
- database_id,
name,
TYPE,
host,
@@ -192,7 +179,6 @@ VALUES
1,
1,
6007,
- 7020,
'Admin data source',
'ADMIN',
'127.0.0.1',
@@ -207,7 +193,6 @@ INSERT INTO
creator_id,
updater_id,
instance_id,
- database_id,
name,
TYPE,
host,
@@ -221,7 +206,6 @@ VALUES
1,
1,
6008,
- 7021,
'Admin data source',
'ADMIN',
'127.0.0.1',
@@ -236,7 +220,6 @@ INSERT INTO
creator_id,
updater_id,
instance_id,
- database_id,
name,
TYPE,
host,
@@ -250,7 +233,6 @@ VALUES
1,
1,
6009,
- 7022,
'Admin data source',
'ADMIN',
'127.0.0.1',
diff --git a/backend/migrator/migration/dev/LATEST.sql b/backend/migrator/migration/dev/LATEST.sql
index 8d53d823d3..a1499768cb 100644
--- a/backend/migrator/migration/dev/LATEST.sql
+++ b/backend/migrator/migration/dev/LATEST.sql
@@ -441,7 +441,6 @@ CREATE TABLE data_source (
updater_id INTEGER NOT NULL REFERENCES principal (id),
updated_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
instance_id INTEGER NOT NULL REFERENCES instance (id),
- database_id INTEGER NOT NULL REFERENCES db (id),
name TEXT NOT NULL,
type TEXT NOT NULL CHECK (type IN ('ADMIN', 'RW', 'RO')),
username TEXT NOT NULL,
@@ -455,9 +454,7 @@ CREATE TABLE data_source (
database TEXT NOT NULL DEFAULT ''
);
-CREATE INDEX idx_data_source_instance_id ON data_source(instance_id);
-
-CREATE UNIQUE INDEX idx_data_source_unique_database_id_name ON data_source(database_id, name);
+CREATE UNIQUE INDEX idx_data_source_unique_instance_id_name ON data_source(instance_id, name);
ALTER SEQUENCE data_source_id_seq RESTART WITH 101;
diff --git a/backend/migrator/migration/prod/2.6/0005##ds_db.sql b/backend/migrator/migration/prod/2.6/0005##ds_db.sql
new file mode 100644
index 0000000000..f53a8e42e8
--- /dev/null
+++ b/backend/migrator/migration/prod/2.6/0005##ds_db.sql
@@ -0,0 +1,5 @@
+DROP INDEX IF EXISTS idx_data_source_instance_id;
+DROP INDEX IF EXISTS idx_data_source_unique_database_id_name;
+ALTER TABLE data_source DROP COLUMN database_id;
+CREATE UNIQUE INDEX idx_data_source_unique_instance_id_name ON data_source(instance_id, name);
+DELETE FROM db WHERE name = '*';
\ No newline at end of file
diff --git a/backend/migrator/migration/prod/LATEST.sql b/backend/migrator/migration/prod/LATEST.sql
index 0fa6dd62e3..712c8111ca 100644
--- a/backend/migrator/migration/prod/LATEST.sql
+++ b/backend/migrator/migration/prod/LATEST.sql
@@ -440,7 +440,6 @@ CREATE TABLE data_source (
updater_id INTEGER NOT NULL REFERENCES principal (id),
updated_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
instance_id INTEGER NOT NULL REFERENCES instance (id),
- database_id INTEGER NOT NULL REFERENCES db (id),
name TEXT NOT NULL,
type TEXT NOT NULL CHECK (type IN ('ADMIN', 'RW', 'RO')),
username TEXT NOT NULL,
@@ -454,9 +453,7 @@ CREATE TABLE data_source (
database TEXT NOT NULL DEFAULT ''
);
-CREATE INDEX idx_data_source_instance_id ON data_source(instance_id);
-
-CREATE UNIQUE INDEX idx_data_source_unique_database_id_name ON data_source(database_id, name);
+CREATE UNIQUE INDEX idx_data_source_unique_instance_id_name ON data_source(instance_id, name);
ALTER SEQUENCE data_source_id_seq RESTART WITH 101;
diff --git a/backend/migrator/migrator_test.go b/backend/migrator/migrator_test.go
index 0890c52836..fd130d4234 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.6.4"), releaseVersion)
+ require.Equal(t, semver.MustParse("2.6.5"), releaseVersion)
}
diff --git a/backend/runner/backuprun/runner.go b/backend/runner/backuprun/runner.go
index 5c6841b9c7..65cd146043 100644
--- a/backend/runner/backuprun/runner.go
+++ b/backend/runner/backuprun/runner.go
@@ -328,10 +328,6 @@ func (r *Runner) startAutoBackups(ctx context.Context) {
log.Error("Failed to get database", zap.Error(err))
return
}
- if database.DatabaseName == api.AllDatabaseName {
- // Skip backup job for wildcard database `*`.
- continue
- }
project, err := r.store.GetProjectV2(ctx, &store.FindProjectMessage{ResourceID: &database.ProjectID})
if err != nil {
log.Error("Failed to get project", zap.Error(err))
diff --git a/backend/store/data_source.go b/backend/store/data_source.go
index 3e7bbff30c..05e11277f9 100644
--- a/backend/store/data_source.go
+++ b/backend/store/data_source.go
@@ -36,8 +36,7 @@ type DataSourceMessage struct {
SSHObfuscatedPassword string
SSHObfuscatedPrivateKey string
// (deprecated) Output only.
- UID int
- DatabaseID int
+ UID int
}
// Copy returns a copy of the data source message.
@@ -63,7 +62,6 @@ func (m *DataSourceMessage) Copy() *DataSourceMessage {
SSHObfuscatedPassword: m.SSHObfuscatedPassword,
SSHObfuscatedPrivateKey: m.SSHObfuscatedPrivateKey,
UID: m.UID,
- DatabaseID: m.DatabaseID,
}
}
@@ -100,7 +98,6 @@ func (*Store) listDataSourceV2(ctx context.Context, tx *Tx, instanceID string) (
rows, err := tx.QueryContext(ctx, `
SELECT
data_source.id,
- data_source.database_id,
data_source.name,
data_source.type,
data_source.username,
@@ -126,7 +123,6 @@ func (*Store) listDataSourceV2(ctx context.Context, tx *Tx, instanceID string) (
var dataSourceMessage DataSourceMessage
if err := rows.Scan(
&dataSourceMessage.UID,
- &dataSourceMessage.DatabaseID,
&dataSourceMessage.ID,
&dataSourceMessage.Type,
&dataSourceMessage.Username,
@@ -173,17 +169,7 @@ func (s *Store) AddDataSourceToInstanceV2(ctx context.Context, instanceUID, crea
}
defer tx.Rollback()
- allDatabaseName := api.AllDatabaseName
- allDatabase, err := s.getDatabaseImplV2(ctx, tx, &FindDatabaseMessage{
- InstanceID: &instanceID,
- DatabaseName: &allDatabaseName,
- IncludeAllDatabase: true,
- })
- if err != nil {
- return err
- }
-
- if err := s.addDataSourceToInstanceImplV2(ctx, tx, instanceUID, allDatabase.UID, creatorID, dataSource); err != nil {
+ if err := s.addDataSourceToInstanceImplV2(ctx, tx, instanceUID, creatorID, dataSource); err != nil {
return err
}
@@ -323,7 +309,7 @@ func (s *Store) UpdateDataSourceV2(ctx context.Context, patch *UpdateDataSourceM
return nil
}
-func (*Store) addDataSourceToInstanceImplV2(ctx context.Context, tx *Tx, instanceUID, databaseUID, creatorID int, dataSource *DataSourceMessage) error {
+func (*Store) addDataSourceToInstanceImplV2(ctx context.Context, tx *Tx, instanceUID, creatorID int, dataSource *DataSourceMessage) error {
// We flatten the data source fields in DataSourceMessage, so we need to compose them in store layer before INSERT.
dataSourceOptions := storepb.DataSourceOptions{
Srv: dataSource.SRV,
@@ -346,7 +332,6 @@ func (*Store) addDataSourceToInstanceImplV2(ctx context.Context, tx *Tx, instanc
creator_id,
updater_id,
instance_id,
- database_id,
name,
type,
username,
@@ -359,8 +344,8 @@ func (*Store) addDataSourceToInstanceImplV2(ctx context.Context, tx *Tx, instanc
options,
database
)
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)
- `, creatorID, creatorID, instanceUID, databaseUID, dataSource.ID,
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
+ `, creatorID, creatorID, instanceUID, dataSource.ID,
dataSource.Type, dataSource.Username, dataSource.ObfuscatedPassword, dataSource.ObfuscatedSslKey,
dataSource.ObfuscatedSslCert, dataSource.ObfuscatedSslCa, dataSource.Host, dataSource.Port,
protoBytes, dataSource.Database,
@@ -372,8 +357,8 @@ func (*Store) addDataSourceToInstanceImplV2(ctx context.Context, tx *Tx, instanc
}
// clearDataSourceImpl deletes dataSources by instance id and database id.
-func (*Store) clearDataSourceImpl(ctx context.Context, tx *Tx, instanceID, databaseID int) error {
- if _, err := tx.ExecContext(ctx, `DELETE FROM data_source WHERE instance_id = $1 AND database_id = $2`, instanceID, databaseID); err != nil {
+func (*Store) clearDataSourceImpl(ctx context.Context, tx *Tx, instanceID int) error {
+ if _, err := tx.ExecContext(ctx, `DELETE FROM data_source WHERE instance_id = $1`, instanceID); err != nil {
return err
}
return nil
diff --git a/backend/store/database.go b/backend/store/database.go
index 72849f03ac..7f59c39100 100644
--- a/backend/store/database.go
+++ b/backend/store/database.go
@@ -40,7 +40,6 @@ func (s *Store) GetDatabase(ctx context.Context, find *api.DatabaseFind) (*api.D
if find.ID != nil {
v2Find.UID = find.ID
}
- v2Find.IncludeAllDatabase = find.IncludeAllDatabase
database, err := s.GetDatabaseV2(ctx, v2Find)
if err != nil {
@@ -166,9 +165,6 @@ type FindDatabaseMessage struct {
// This is used for existing tasks with archived databases.
ShowDeleted bool
- // TODO(d): deprecate this field when we migrate all datasource to v1 store.
- IncludeAllDatabase bool
-
// IgnoreCaseSensitive is used to ignore case sensitive when finding database.
IgnoreCaseSensitive bool
}
@@ -578,26 +574,8 @@ func (s *Store) BatchUpdateDatabaseProject(ctx context.Context, databases []*Dat
return updatedDatabases, nil
}
-func (s *Store) getDatabaseImplV2(ctx context.Context, tx *Tx, find *FindDatabaseMessage) (*DatabaseMessage, error) {
- databaseList, err := s.listDatabaseImplV2(ctx, tx, find)
- if err != nil {
- return nil, err
- }
- if len(databaseList) == 0 {
- return nil, &common.Error{Code: common.NotFound, Err: errors.Errorf("database not found with %v", find)}
- }
- if len(databaseList) > 1 {
- return nil, &common.Error{Code: common.NotFound, Err: errors.Errorf("found %d data source databases with %v, but expect 1", len(databaseList), find)}
- }
-
- return databaseList[0], nil
-}
-
func (*Store) listDatabaseImplV2(ctx context.Context, tx *Tx, find *FindDatabaseMessage) ([]*DatabaseMessage, error) {
where, args := []string{"TRUE"}, []any{}
- if !find.IncludeAllDatabase {
- where, args = append(where, fmt.Sprintf("db.name != $%d", len(args)+1)), append(args, api.AllDatabaseName)
- }
if v := find.ProjectID; v != nil {
where, args = append(where, fmt.Sprintf("project.resource_id = $%d", len(args)+1)), append(args, *v)
}
diff --git a/backend/store/instance.go b/backend/store/instance.go
index ac8a912ccc..33e9dd6755 100644
--- a/backend/store/instance.go
+++ b/backend/store/instance.go
@@ -64,7 +64,6 @@ func (s *Store) composeInstance(ctx context.Context, instance *InstanceMessage)
composedInstance.DataSourceList = append(composedInstance.DataSourceList, &api.DataSource{
ID: ds.UID,
InstanceID: instance.UID,
- DatabaseID: ds.DatabaseID,
Name: ds.ID,
Type: ds.Type,
Username: ds.Username,
@@ -267,13 +266,8 @@ func (s *Store) CreateInstanceV2(ctx context.Context, instanceCreate *InstanceMe
return nil, err
}
- allDatabaseUID, err := s.createDatabaseDefaultImpl(ctx, tx, instanceID, &DatabaseMessage{DatabaseName: api.AllDatabaseName})
- if err != nil {
- return nil, err
- }
-
for _, ds := range instanceCreate.DataSources {
- if err := s.addDataSourceToInstanceImplV2(ctx, tx, instanceID, allDatabaseUID, creatorID, ds); err != nil {
+ if err := s.addDataSourceToInstanceImplV2(ctx, tx, instanceID, creatorID, ds); err != nil {
return nil, err
}
}
@@ -406,22 +400,12 @@ func (s *Store) UpdateInstanceV2(ctx context.Context, patch *UpdateInstanceMessa
}
if patch.DataSources != nil {
- allDatabaseName := api.AllDatabaseName
- allDatabase, err := s.getDatabaseImplV2(ctx, tx, &FindDatabaseMessage{
- InstanceID: &instance.ResourceID,
- DatabaseName: &allDatabaseName,
- IncludeAllDatabase: true,
- })
- if err != nil {
- return nil, err
- }
-
- if err := s.clearDataSourceImpl(ctx, tx, instance.UID, allDatabase.UID); err != nil {
+ if err := s.clearDataSourceImpl(ctx, tx, instance.UID); err != nil {
return nil, err
}
for _, ds := range *patch.DataSources {
- if err := s.addDataSourceToInstanceImplV2(ctx, tx, instance.UID, allDatabase.UID, patch.UpdaterID, ds); err != nil {
+ if err := s.addDataSourceToInstanceImplV2(ctx, tx, instance.UID, patch.UpdaterID, ds); err != nil {
return nil, err
}
}