Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
package store
import (
"context"
"database/sql"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/store/qb"
)
// idMinValue is the minimum ID value for per-project auto-increment.
// Matches the old ALTER SEQUENCE ... RESTART WITH 101 convention,
// reserving IDs below 101 for seed/test data.
const idMinValue int64 = 101
// requireActiveProject requires the project to exist and be active.
func requireActiveProject(ctx context.Context, tx *sql.Tx, projectID string) error {
var deleted bool
if err := tx.QueryRowContext(ctx,
"SELECT deleted FROM project WHERE resource_id = $1", projectID).Scan(&deleted); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return common.Errorf(common.NotFound, "project %s not found", projectID)
}
return errors.Wrapf(err, "failed to find project %s", projectID)
}
if deleted {
return common.Errorf(common.NotFound, "project %s is deleted", projectID)
}
return nil
}
// lockActiveProject locks the project row and requires it to be active. This
// serializes MAX(id) + 1 allocation for project-scoped tables.
func lockActiveProject(ctx context.Context, tx *sql.Tx, projectID string) error {
var deleted bool
if err := tx.QueryRowContext(ctx,
"SELECT deleted FROM project WHERE resource_id = $1 FOR UPDATE", projectID).Scan(&deleted); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return common.Errorf(common.NotFound, "project %s not found", projectID)
}
return errors.Wrapf(err, "failed to lock project %s", projectID)
}
if deleted {
return common.Errorf(common.NotFound, "project %s is deleted", projectID)
}
return nil
}
// nextProjectID returns the next per-project auto-increment ID for the given table.
// Must be called within a transaction. Locks and validates the active project row to
// serialize concurrent allocations.
// Returns at least idMinValue (101) for new projects.
func nextProjectID(ctx context.Context, tx *sql.Tx, table, projectID string) (int64, error) {
if err := lockActiveProject(ctx, tx, projectID); err != nil {
return 0, err
}
var maxID int64
q := qb.Q()
q.Space(`
SELECT COALESCE(MAX(id), 0) FROM ? WHERE project = ?
`, qb.Q().Space(table), projectID)
query, args, err := q.ToSQL()
if err != nil {
return 0, errors.Wrapf(err, "failed to build sql")
}
if err := tx.QueryRowContext(ctx, query, args...).Scan(&maxID); err != nil {
return 0, errors.Wrapf(err, "failed to get max id for %s", table)
}
nextID := maxID + 1
if nextID < idMinValue {
nextID = idMinValue
}
return nextID, nil
}