Skip to content

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

feat: specify override metadata in sql check (#10649)

h3n4l6ca86d873366
diff --git a/.github/workflows/backend-tests.yml b/.github/workflows/backend-tests.yml
index 97ba703137..c5221cd1d7 100644
--- a/.github/workflows/backend-tests.yml
+++ b/.github/workflows/backend-tests.yml
@@ -21,7 +21,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
- go-version: 1.21.5
+ go-version: 1.21.6
cache: false
- name: Verify go.mod is tidy
run: |
diff --git a/.github/workflows/build-artifacts-and-draft-release.yml b/.github/workflows/build-artifacts-and-draft-release.yml
index cf0a6b4c06..ead2598100 100644
--- a/.github/workflows/build-artifacts-and-draft-release.yml
+++ b/.github/workflows/build-artifacts-and-draft-release.yml
@@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
- go-version: 1.21.5
+ go-version: 1.21.6
- uses: pnpm/[email protected]
with:
version: 8.12.1
@@ -43,7 +43,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
- go-version: 1.21.5
+ go-version: 1.21.6
- name: Make directories
run: |
mkdir -p ./bytebase-build/linux
diff --git a/.github/workflows/test_link.yml b/.github/workflows/test_link.yml
index ba00de2daa..215856ddc7 100644
--- a/.github/workflows/test_link.yml
+++ b/.github/workflows/test_link.yml
@@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
- go-version: 1.21.5
+ go-version: 1.21.6
cache: false
- name: Validate links
run: go test -timeout 600s -v ./scripts/... | tee test.log; exit ${PIPESTATUS[0]}
diff --git a/README.md b/README.md
index 38f01df3fc..f141c015e8 100644
--- a/README.md
+++ b/README.md
@@ -256,7 +256,7 @@ Below diagram describes a typical mapping between an engineering org and the cor
### Prerequisites
-- [Go](https://golang.org/doc/install) (1.21.5 or later)
+- [Go](https://golang.org/doc/install) (1.21.6 or later)
- [pnpm](https://pnpm.io/installation)
- [Air](https://github.com/bytebase/air) (**our forked repo @87187cc with the proper signal handling**). This is for backend live reload.
```bash
diff --git a/backend/api/gitops/webhook.go b/backend/api/gitops/webhook.go
index f3b3fbd0b7..4326cb2d1c 100644
--- a/backend/api/gitops/webhook.go
+++ b/backend/api/gitops/webhook.go
@@ -1014,7 +1014,7 @@ func (s *Service) sqlAdviceForFile(
if fileInfo.repoInfo.project.SchemaChangeType == api.ProjectSchemaChangeTypeSDL {
advisorMode = advisor.SyntaxModeSDL
}
- catalog, err := s.store.NewCatalog(ctx, database.UID, instance.Engine, store.IgnoreDatabaseAndTableCaseSensitive(instance), advisorMode)
+ catalog, err := s.store.NewCatalog(ctx, database.UID, instance.Engine, store.IgnoreDatabaseAndTableCaseSensitive(instance), nil /* Override Metadata */, advisorMode)
if err != nil {
return nil, errors.Errorf("Failed to get catalog for database %v with error: %v", database.UID, err)
}
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index 504e8d4587..d09d396e53 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -927,7 +927,11 @@ func (s *SQLService) Check(ctx context.Context, request *v1pb.CheckRequest) (*v1
return nil, status.Errorf(codes.NotFound, "environment %q not found", database.EffectiveEnvironmentID)
}
- _, adviceList, err := s.sqlReviewCheck(ctx, request.Statement, environment, instance, database)
+ var overideMetadata *storepb.DatabaseSchemaMetadata
+ if request.Metadata != nil {
+ overideMetadata, _ = convertV1DatabaseMetadata(request.Metadata)
+ }
+ _, adviceList, err := s.sqlReviewCheck(ctx, request.Statement, environment, instance, database, overideMetadata)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to do sql review check, error: %v", err)
}
@@ -1190,7 +1194,7 @@ func (s *SQLService) preCheck(ctx context.Context, instanceName, connectionDatab
}
// Run SQL review.
- adviceStatus, adviceList, err := s.sqlReviewCheck(ctx, statement, environment, instance, maybeDatabase)
+ adviceStatus, adviceList, err := s.sqlReviewCheck(ctx, statement, environment, instance, maybeDatabase, nil /* Override Metadata */)
if err != nil {
return nil, nil, nil, adviceStatus, adviceList, nil, err
}
@@ -1690,29 +1694,36 @@ func getReadOnlyDataSource(instance *store.InstanceMessage) *store.DataSourceMes
return dataSource
}
-func (s *SQLService) sqlReviewCheck(ctx context.Context, statement string, environment *store.EnvironmentMessage, instance *store.InstanceMessage, database *store.DatabaseMessage) (advisor.Status, []*v1pb.Advice, error) {
+// sqlReviewCheck checks the SQL statement against the SQL review policy bind to given environment,
+// against the database schema bind to the given database, if the overrideMetadata is provided,
+// it will be used instead of fetching the database schema from the store.
+func (s *SQLService) sqlReviewCheck(ctx context.Context, statement string, environment *store.EnvironmentMessage, instance *store.InstanceMessage, database *store.DatabaseMessage, overrideMetadata *storepb.DatabaseSchemaMetadata) (advisor.Status, []*v1pb.Advice, error) {
if !IsSQLReviewSupported(instance.Engine) || database == nil {
return advisor.Success, nil, nil
}
- dbSchema, err := s.store.GetDBSchema(ctx, database.UID)
- if err != nil {
- return advisor.Error, nil, status.Errorf(codes.Internal, "failed to fetch database schema: %v", err)
- }
- if dbSchema == nil {
- if err := s.schemaSyncer.SyncDatabaseSchema(ctx, database, true /* force */); err != nil {
- return advisor.Error, nil, status.Errorf(codes.Internal, "failed to sync database schema: %v", err)
- }
- dbSchema, err = s.store.GetDBSchema(ctx, database.UID)
+ dbMetadata := overrideMetadata
+ if dbMetadata == nil {
+ dbSchema, err := s.store.GetDBSchema(ctx, database.UID)
if err != nil {
return advisor.Error, nil, status.Errorf(codes.Internal, "failed to fetch database schema: %v", err)
}
if dbSchema == nil {
- return advisor.Error, nil, status.Errorf(codes.NotFound, "database schema not found: %v", database.UID)
+ if err := s.schemaSyncer.SyncDatabaseSchema(ctx, database, true /* force */); err != nil {
+ return advisor.Error, nil, status.Errorf(codes.Internal, "failed to sync database schema: %v", err)
+ }
+ dbSchema, err = s.store.GetDBSchema(ctx, database.UID)
+ if err != nil {
+ return advisor.Error, nil, status.Errorf(codes.Internal, "failed to fetch database schema: %v", err)
+ }
+ if dbSchema == nil {
+ return advisor.Error, nil, status.Errorf(codes.NotFound, "database schema not found: %v", database.UID)
+ }
}
+ dbMetadata = dbSchema.GetMetadata()
}
- catalog, err := s.store.NewCatalog(ctx, database.UID, instance.Engine, store.IgnoreDatabaseAndTableCaseSensitive(instance), advisor.SyntaxModeNormal)
+ catalog, err := s.store.NewCatalog(ctx, database.UID, instance.Engine, store.IgnoreDatabaseAndTableCaseSensitive(instance), overrideMetadata, advisor.SyntaxModeNormal)
if err != nil {
return advisor.Error, nil, status.Errorf(codes.Internal, "Failed to create a catalog: %v", err)
}
@@ -1735,8 +1746,8 @@ func (s *SQLService) sqlReviewCheck(ctx context.Context, statement string, envir
adviceLevel, adviceList, err := s.sqlCheck(
ctx,
instance.Engine,
- dbSchema.GetMetadata().CharacterSet,
- dbSchema.GetMetadata().Collation,
+ dbMetadata.CharacterSet,
+ dbMetadata.Collation,
environment.UID,
statement,
catalog,
diff --git a/backend/api/v1/sql_service_v2.go b/backend/api/v1/sql_service_v2.go
index 8c6f745f3e..7adc370500 100644
--- a/backend/api/v1/sql_service_v2.go
+++ b/backend/api/v1/sql_service_v2.go
@@ -57,7 +57,7 @@ func (s *SQLService) ExportV2(ctx context.Context, request *v1pb.ExportRequest)
}
// Run SQL review.
- if _, _, err = s.sqlReviewCheck(ctx, statement, environment, instance, maybeDatabase); err != nil {
+ if _, _, err = s.sqlReviewCheck(ctx, statement, environment, instance, maybeDatabase, nil /* Override Metadata */); err != nil {
return nil, err
}
@@ -127,7 +127,7 @@ func (s *SQLService) QueryV2(ctx context.Context, request *v1pb.QueryRequest) (*
}
// Run SQL review.
- adviceStatus, advices, err := s.sqlReviewCheck(ctx, statement, environment, instance, maybeDatabase)
+ adviceStatus, advices, err := s.sqlReviewCheck(ctx, statement, environment, instance, maybeDatabase, nil /* Override Metadata */)
if err != nil {
return nil, err
}
diff --git a/backend/runner/plancheck/statement_advise_executor.go b/backend/runner/plancheck/statement_advise_executor.go
index 1a035349f4..76a59f4788 100644
--- a/backend/runner/plancheck/statement_advise_executor.go
+++ b/backend/runner/plancheck/statement_advise_executor.go
@@ -146,7 +146,7 @@ func (e *StatementAdviseExecutor) runForDatabaseTarget(ctx context.Context, conf
}
}
- catalog, err := e.store.NewCatalog(ctx, database.UID, instance.Engine, store.IgnoreDatabaseAndTableCaseSensitive(instance), getSyntaxMode(changeType))
+ catalog, err := e.store.NewCatalog(ctx, database.UID, instance.Engine, store.IgnoreDatabaseAndTableCaseSensitive(instance), nil /* Override Metadata */, getSyntaxMode(changeType))
if err != nil {
return nil, common.Wrapf(err, common.Internal, "failed to create a catalog")
}
@@ -357,7 +357,7 @@ func (e *StatementAdviseExecutor) runForDatabaseGroupTarget(ctx context.Context,
}
}
- catalog, err := e.store.NewCatalog(ctx, database.UID, instance.Engine, store.IgnoreDatabaseAndTableCaseSensitive(instance), getSyntaxMode(changeType))
+ catalog, err := e.store.NewCatalog(ctx, database.UID, instance.Engine, store.IgnoreDatabaseAndTableCaseSensitive(instance), nil /* Override Metadata */, getSyntaxMode(changeType))
if err != nil {
return nil, common.Wrapf(err, common.Internal, "failed to create a catalog")
}
diff --git a/backend/store/catalog.go b/backend/store/catalog.go
index ba83ed0d11..70e0a68e4f 100644
--- a/backend/store/catalog.go
+++ b/backend/store/catalog.go
@@ -18,22 +18,25 @@ type Catalog struct {
}
// NewCatalog creates a new database catalog.
-func (s *Store) NewCatalog(ctx context.Context, databaseID int, engineType storepb.Engine, ignoreCaseSensitive bool, syntaxMode advisor.SyntaxMode) (catalog.Catalog, error) {
+func (s *Store) NewCatalog(ctx context.Context, databaseID int, engineType storepb.Engine, ignoreCaseSensitive bool, overrideDatabaseMetadata *storepb.DatabaseSchemaMetadata, syntaxMode advisor.SyntaxMode) (catalog.Catalog, error) {
c := &Catalog{}
if syntaxMode == advisor.SyntaxModeSDL {
return NewEmptyCatalog(engineType)
}
- databaseMeta, err := s.GetDBSchema(ctx, databaseID)
- if err != nil {
- return nil, err
+ dbMetadata := overrideDatabaseMetadata
+ if dbMetadata == nil {
+ databaseMeta, err := s.GetDBSchema(ctx, databaseID)
+ if err != nil {
+ return nil, err
+ }
+ if databaseMeta == nil {
+ return nil, nil
+ }
+ dbMetadata = databaseMeta.GetMetadata()
}
- if databaseMeta == nil {
- return nil, nil
- }
-
- c.Finder = catalog.NewFinder(databaseMeta.GetMetadata(), &catalog.FinderContext{CheckIntegrity: true, EngineType: engineType, IgnoreCaseSensitive: ignoreCaseSensitive})
+ c.Finder = catalog.NewFinder(dbMetadata, &catalog.FinderContext{CheckIntegrity: true, EngineType: engineType, IgnoreCaseSensitive: ignoreCaseSensitive})
return c, nil
}
diff --git a/go.mod b/go.mod
index a604ce8729..2449eb1135 100644
--- a/go.mod
+++ b/go.mod
@@ -1,6 +1,6 @@
module github.com/bytebase/bytebase
-go 1.21.5
+go 1.21.6
require (
cloud.google.com/go/spanner v1.55.0
diff --git a/scripts/.goreleaser-for-darwin.yaml b/scripts/.goreleaser-for-darwin.yaml
index 1f1ce770ca..443c3ef9c9 100644
--- a/scripts/.goreleaser-for-darwin.yaml
+++ b/scripts/.goreleaser-for-darwin.yaml
@@ -17,7 +17,7 @@ builds:
- embed_frontend
env:
- VERSION="development"
- - GO_VERSION="1.21.5"
+ - GO_VERSION="1.21.6"
- CGO_ENABLED=1
- GIT_COMMIT="unknown"
- BUILD_TIME="unknown"
@@ -41,7 +41,7 @@ builds:
- release
env:
- VERSION="development"
- - GO_VERSION="1.21.5"
+ - GO_VERSION="1.21.6"
- CGO_ENABLED=1
- GIT_COMMIT="unknown"
- BUILD_TIME="unknown"
diff --git a/scripts/.goreleaser-for-linux.yaml b/scripts/.goreleaser-for-linux.yaml
index 98dce21399..28325f63d1 100644
--- a/scripts/.goreleaser-for-linux.yaml
+++ b/scripts/.goreleaser-for-linux.yaml
@@ -17,7 +17,7 @@ builds:
- embed_frontend
env:
- VERSION="development"
- - GO_VERSION="1.21.5"
+ - GO_VERSION="1.21.6"
- CGO_ENABLED=1
- GIT_COMMIT="unknown"
- BUILD_TIME="unknown"
@@ -46,7 +46,7 @@ builds:
- release
env:
- VERSION="development"
- - GO_VERSION="1.21.5"
+ - GO_VERSION="1.21.6"
- CGO_ENABLED=1
- GIT_COMMIT="unknown"
- BUILD_TIME="unknown"
diff --git a/scripts/Dockerfile b/scripts/Dockerfile
index 46f9ed5f00..5615dd8ba1 100644
--- a/scripts/Dockerfile
+++ b/scripts/Dockerfile
@@ -18,14 +18,14 @@ COPY . .
RUN pnpm --dir frontend install --frozen-lockfile
RUN pnpm --dir frontend release-docker
-FROM golang:1.21.5 as backend
+FROM golang:1.21.6 as backend
ADD go.mod go.sum /
RUN go mod download
ARG TARGETARCH
ARG VERSION="development"
ARG VERSION_SUFFIX=""
-ARG GO_VERSION="1.21.5"
+ARG GO_VERSION="1.21.6"
ARG GIT_COMMIT="unknown"
ARG BUILD_TIME="unknown"
ARG BUILD_USER="unknown"
diff --git a/scripts/Dockerfile.aws b/scripts/Dockerfile.aws
index 89388b5b98..c42183c2e6 100644
--- a/scripts/Dockerfile.aws
+++ b/scripts/Dockerfile.aws
@@ -19,13 +19,13 @@ COPY . .
RUN pnpm --dir frontend install --frozen-lockfile
RUN pnpm --dir frontend release-aws
-FROM golang:1.21.5 as backend
+FROM golang:1.21.6 as backend
ADD go.mod go.sum /
RUN go mod download
ARG VERSION="development"
ARG VERSION_SUFFIX=""
-ARG GO_VERSION="1.21.5"
+ARG GO_VERSION="1.21.6"
ARG GIT_COMMIT="unknown"
ARG BUILD_TIME="unknown"
ARG BUILD_USER="unknown"
diff --git a/scripts/Dockerfile.bb b/scripts/Dockerfile.bb
index 55de1296c8..8f2e2ed609 100644
--- a/scripts/Dockerfile.bb
+++ b/scripts/Dockerfile.bb
@@ -5,12 +5,12 @@
# $ docker run --init --rm --name bb bytebase/bb
-FROM golang:1.21.5 as bb
+FROM golang:1.21.6 as bb
ADD go.mod go.sum /
RUN go mod download
ARG VERSION="development"
-ARG GO_VERSION="1.21.5"
+ARG GO_VERSION="1.21.6"
ARG GIT_COMMIT="unknown"
ARG BUILD_TIME="unknown"
ARG BUILD_USER="unknown"
diff --git a/scripts/Dockerfile.depot b/scripts/Dockerfile.depot
index 171f9adc8b..78454df7ed 100644
--- a/scripts/Dockerfile.depot
+++ b/scripts/Dockerfile.depot
@@ -1,10 +1,10 @@
-FROM golang:1.21.5 as backend
+FROM golang:1.21.6 as backend
ADD go.mod go.sum /
RUN go mod download
ARG VERSION="development"
ARG VERSION_SUFFIX=""
-ARG GO_VERSION="1.21.5"
+ARG GO_VERSION="1.21.6"
ARG GIT_COMMIT="unknown"
ARG BUILD_TIME="unknown"
ARG BUILD_USER="unknown"
diff --git a/scripts/Dockerfile.render-demo b/scripts/Dockerfile.render-demo
index bcabf57700..8ce059fd3f 100644
--- a/scripts/Dockerfile.render-demo
+++ b/scripts/Dockerfile.render-demo
@@ -12,11 +12,11 @@ COPY . .
RUN cd frontend && pnpm install --frozen-lockfile && pnpm release-docker
-FROM golang:1.21.5 as backend
+FROM golang:1.21.6 as backend
ADD go.mod go.sum /
RUN go mod download
-ARG GO_VERSION="1.21.5"
+ARG GO_VERSION="1.21.6"
# RENDER_GIT_COMMIT is a render.com provided env var during build.
# https://render.com/docs/environment-variables
ARG RENDER_GIT_COMMIT="unknown"
diff --git a/scripts/Dockerfile.sql-service b/scripts/Dockerfile.sql-service
index 569b1db15b..47712bc0d4 100644
--- a/scripts/Dockerfile.sql-service
+++ b/scripts/Dockerfile.sql-service
@@ -5,12 +5,12 @@
# $ docker run --init --rm --name sql-service --publish 8081:8081 bytebase/sql
-FROM golang:1.21.5 as sql
+FROM golang:1.21.6 as sql
ADD go.mod go.sum /
RUN go mod download
ARG VERSION="development"
-ARG GO_VERSION="1.21.5"
+ARG GO_VERSION="1.21.6"
ARG GIT_COMMIT="unknown"
ARG BUILD_TIME="unknown"
ARG BUILD_USER="unknown"
diff --git a/scripts/build_bytebase.sh b/scripts/build_bytebase.sh
index 6071401231..7bc215837a 100755
--- a/scripts/build_bytebase.sh
+++ b/scripts/build_bytebase.sh
@@ -14,9 +14,9 @@ OUTPUT_DIR=$(mkdir_output "$1")
OUTPUT_BINARY=$OUTPUT_DIR/bytebase
GO_VERSION=`go version | { read _ _ v _; echo ${v#go}; }`
-if [ "$(version ${GO_VERSION})" -lt "$(version 1.21.5)" ];
+if [ "$(version ${GO_VERSION})" -lt "$(version 1.21.6)" ];
then
- echo "${RED}Precheck failed.${NC} Require go version >= 1.21.5. Current version ${GO_VERSION}."; exit 1;
+ echo "${RED}Precheck failed.${NC} Require go version >= 1.21.6. Current version ${GO_VERSION}."; exit 1;
fi
NODE_VERSION=`node -v | { read v; echo ${v#v}; }`