Skip to content

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

chore: add release guard (#3857)

Danny Xu4b485fe77712
diff --git a/store/data_source.go b/store/data_source.go
index 2a345cc703..d718046b38 100644
--- a/store/data_source.go
+++ b/store/data_source.go
@@ -284,7 +284,68 @@ func (s *Store) patchDataSourceRaw(ctx context.Context, patch *api.DataSourcePat
}
// createDataSourceImpl creates a new dataSource.
-func (*Store) createDataSourceImpl(ctx context.Context, tx *Tx, create *api.DataSourceCreate) (*dataSourceRaw, error) {
+func (s *Store) createDataSourceImpl(ctx context.Context, tx *Tx, create *api.DataSourceCreate) (*dataSourceRaw, error) {
+ if s.db.mode == common.ReleaseModeProd {
+ // Insert row into dataSource.
+ query := `
+ INSERT INTO data_source (
+ creator_id,
+ updater_id,
+ instance_id,
+ database_id,
+ name,
+ type,
+ username,
+ password,
+ ssl_key,
+ ssl_cert,
+ ssl_ca,
+ host_override,
+ port_override
+ )
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, instance_id, database_id, name, type, username, password, ssl_key, ssl_cert, ssl_ca, host_override, port_override
+`
+ var dataSourceRaw dataSourceRaw
+ if err := tx.QueryRowContext(ctx, query,
+ create.CreatorID,
+ create.CreatorID,
+ create.InstanceID,
+ create.DatabaseID,
+ create.Name,
+ create.Type,
+ create.Username,
+ create.Password,
+ create.SslKey,
+ create.SslCert,
+ create.SslCa,
+ create.HostOverride,
+ create.PortOverride,
+ ).Scan(
+ &dataSourceRaw.ID,
+ &dataSourceRaw.CreatorID,
+ &dataSourceRaw.CreatedTs,
+ &dataSourceRaw.UpdaterID,
+ &dataSourceRaw.UpdatedTs,
+ &dataSourceRaw.InstanceID,
+ &dataSourceRaw.DatabaseID,
+ &dataSourceRaw.Name,
+ &dataSourceRaw.Type,
+ &dataSourceRaw.Username,
+ &dataSourceRaw.Password,
+ &dataSourceRaw.SslKey,
+ &dataSourceRaw.SslCert,
+ &dataSourceRaw.SslCa,
+ &dataSourceRaw.HostOverride,
+ &dataSourceRaw.PortOverride,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, common.FormatDBErrorEmptyRowWithQuery(query)
+ }
+ return nil, FormatError(err)
+ }
+ return &dataSourceRaw, nil
+ }
// Insert row into dataSource.
query := `
INSERT INTO data_source (
@@ -349,7 +410,7 @@ func (*Store) createDataSourceImpl(ctx context.Context, tx *Tx, create *api.Data
return &dataSourceRaw, nil
}
-func (*Store) findDataSourceImpl(ctx context.Context, tx *Tx, find *api.DataSourceFind) ([]*dataSourceRaw, error) {
+func (s *Store) findDataSourceImpl(ctx context.Context, tx *Tx, find *api.DataSourceFind) ([]*dataSourceRaw, error) {
// Build WHERE clause.
where, args := []string{"1 = 1"}, []interface{}{}
if v := find.ID; v != nil {
@@ -365,6 +426,68 @@ func (*Store) findDataSourceImpl(ctx context.Context, tx *Tx, find *api.DataSour
where, args = append(where, fmt.Sprintf("type = $%d", len(args)+1)), append(args, api.DataSourceType(*v))
}
+ if s.db.mode == common.ReleaseModeProd {
+ rows, err := tx.QueryContext(ctx, `
+ SELECT
+ id,
+ creator_id,
+ created_ts,
+ updater_id,
+ updated_ts,
+ instance_id,
+ database_id,
+ name,
+ type,
+ username,
+ password,
+ ssl_key,
+ ssl_cert,
+ ssl_ca,
+ host_override,
+ port_override
+ FROM data_source
+ WHERE `+strings.Join(where, " AND "),
+ args...,
+ )
+ if err != nil {
+ return nil, FormatError(err)
+ }
+ defer rows.Close()
+
+ // Iterate over result set and deserialize rows into dataSourceRawList.
+ var dataSourceRawList []*dataSourceRaw
+ for rows.Next() {
+ var dataSourceRaw dataSourceRaw
+ if err := rows.Scan(
+ &dataSourceRaw.ID,
+ &dataSourceRaw.CreatorID,
+ &dataSourceRaw.CreatedTs,
+ &dataSourceRaw.UpdaterID,
+ &dataSourceRaw.UpdatedTs,
+ &dataSourceRaw.InstanceID,
+ &dataSourceRaw.DatabaseID,
+ &dataSourceRaw.Name,
+ &dataSourceRaw.Type,
+ &dataSourceRaw.Username,
+ &dataSourceRaw.Password,
+ &dataSourceRaw.SslKey,
+ &dataSourceRaw.SslCert,
+ &dataSourceRaw.SslCa,
+ &dataSourceRaw.HostOverride,
+ &dataSourceRaw.PortOverride,
+ ); err != nil {
+ return nil, FormatError(err)
+ }
+
+ dataSourceRawList = append(dataSourceRawList, &dataSourceRaw)
+ }
+ if err := rows.Err(); err != nil {
+ return nil, FormatError(err)
+ }
+
+ return dataSourceRawList, nil
+ }
+
rows, err := tx.QueryContext(ctx, `
SELECT
id,
@@ -429,7 +552,7 @@ func (*Store) findDataSourceImpl(ctx context.Context, tx *Tx, find *api.DataSour
}
// patchDataSourceImpl updates a dataSource by ID. Returns the new state of the dataSource after update.
-func (*Store) patchDataSourceImpl(ctx context.Context, tx *Tx, patch *api.DataSourcePatch) (*dataSourceRaw, error) {
+func (s *Store) patchDataSourceImpl(ctx context.Context, tx *Tx, patch *api.DataSourcePatch) (*dataSourceRaw, error) {
// Build UPDATE clause.
set, args := []string{"updater_id = $1"}, []interface{}{patch.UpdaterID}
if v := patch.Username; v != nil {
@@ -459,6 +582,40 @@ func (*Store) patchDataSourceImpl(ctx context.Context, tx *Tx, patch *api.DataSo
args = append(args, patch.ID)
var dataSourceRaw dataSourceRaw
+ if s.db.mode == common.ReleaseModeProd {
+ // Execute update query with RETURNING.
+ if err := tx.QueryRowContext(ctx, fmt.Sprintf(`
+ UPDATE data_source
+ SET `+strings.Join(set, ", ")+`
+ WHERE id = $%d
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, instance_id, database_id, name, type, username, password, ssl_key, ssl_cert, ssl_ca, host_override, port_override
+ `, len(args)),
+ args...,
+ ).Scan(
+ &dataSourceRaw.ID,
+ &dataSourceRaw.CreatorID,
+ &dataSourceRaw.CreatedTs,
+ &dataSourceRaw.UpdaterID,
+ &dataSourceRaw.UpdatedTs,
+ &dataSourceRaw.InstanceID,
+ &dataSourceRaw.DatabaseID,
+ &dataSourceRaw.Name,
+ &dataSourceRaw.Type,
+ &dataSourceRaw.Username,
+ &dataSourceRaw.Password,
+ &dataSourceRaw.SslKey,
+ &dataSourceRaw.SslCert,
+ &dataSourceRaw.SslCa,
+ &dataSourceRaw.HostOverride,
+ &dataSourceRaw.PortOverride,
+ ); err != nil {
+ if err == sql.ErrNoRows {
+ return nil, &common.Error{Code: common.NotFound, Err: errors.Errorf("DataSource not found with ID %d", patch.ID)}
+ }
+ return nil, FormatError(err)
+ }
+ return &dataSourceRaw, nil
+ }
// Execute update query with RETURNING.
if err := tx.QueryRowContext(ctx, fmt.Sprintf(`
UPDATE data_source