Skip to content

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

chore: move db schema to release mode (#3920)

Danny Xua72dbbce8cb3
diff --git a/server/database.go b/server/database.go
index 2b92148e87..a9170ceffd 100644
--- a/server/database.go
+++ b/server/database.go
@@ -1,7 +1,6 @@
package server
import (
- "bytes"
"encoding/json"
"fmt"
"io"
@@ -395,24 +394,6 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database not found with ID %d", id))
}
- if s.profile.Mode == common.ReleaseModeProd {
- driver, err := s.dbFactory.GetAdminDatabaseDriver(ctx, database.Instance, database.Name)
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get database driver").SetInternal(err)
- }
- defer driver.Close(ctx)
- var schemaBuf bytes.Buffer
- if _, err := driver.Dump(ctx, database.Name, &schemaBuf, true /*schemaOnly*/); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to dump database schema").SetInternal(err)
- }
-
- c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextPlainCharsetUTF8)
- if _, err := c.Response().Write(schemaBuf.Bytes()); err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to write schema response for database %v", id)).SetInternal(err)
- }
- return nil
- }
-
dbSchema, err := s.store.GetDBSchema(ctx, id)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to get dbSchema for database ID %v", id)).SetInternal(err)
diff --git a/server/runner/schemasync/syncer.go b/server/runner/schemasync/syncer.go
index 1bea5bfd54..61d81d1d6f 100644
--- a/server/runner/schemasync/syncer.go
+++ b/server/runner/schemasync/syncer.go
@@ -394,35 +394,9 @@ func (s *Syncer) SyncDatabaseSchema(ctx context.Context, instance *api.Instance,
}
database = createdDatabase
}
- // Sync database schema
- if s.profile.Mode == common.ReleaseModeDev {
- if err := syncDBSchema(ctx, s.store, database, schema, driver, force); err != nil {
- return err
- }
- } else {
- if err := syncTableSchema(ctx, s.store, database, schema); err != nil {
- return err
- }
- if err := syncViewSchema(ctx, s.store, database, schema); err != nil {
- return err
- }
- if err := syncDBExtensionSchema(ctx, s.store, database, schema); err != nil {
- return err
- }
- }
- return nil
-}
-func syncTableSchema(ctx context.Context, store *store.Store, database *api.Database, schema *db.Schema) error {
- return store.SetTableList(ctx, schema, database.ID)
-}
-
-func syncViewSchema(ctx context.Context, store *store.Store, database *api.Database, schema *db.Schema) error {
- return store.SetViewList(ctx, schema, database.ID)
-}
-
-func syncDBExtensionSchema(ctx context.Context, store *store.Store, database *api.Database, schema *db.Schema) error {
- return store.SetDBExtensionList(ctx, schema, database.ID)
+ // Sync database schema
+ return syncDBSchema(ctx, s.store, database, schema, driver, force)
}
func syncDBSchema(ctx context.Context, store *store.Store, database *api.Database, schema *db.Schema, driver db.Driver, force bool) error {
diff --git a/server/webhook.go b/server/webhook.go
index 26a3011ab4..dff08c6ffc 100644
--- a/server/webhook.go
+++ b/server/webhook.go
@@ -179,8 +179,8 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
}
token := c.Request().Header.Get("X-SQL-Review-Token")
- if token == s.workspaceID && s.profile.Mode == common.ReleaseModeDev {
- // We will use workspace id as token in integration test.
+ // We will use workspace id as token in integration test for skipping the check.
+ if token == s.workspaceID {
return true, nil
}
diff --git a/store/migration/dev/20221206000000##db_schema.sql b/store/migration/prod/1.10/0000##db_schema.sql
similarity index 100%
rename from store/migration/dev/20221206000000##db_schema.sql
rename to store/migration/prod/1.10/0000##db_schema.sql
diff --git a/store/migration/dev/20221214000000##add_idx_db_project_id.sql b/store/migration/prod/1.10/0001##add_idx_db_project_id.sql
similarity index 100%
rename from store/migration/dev/20221214000000##add_idx_db_project_id.sql
rename to store/migration/prod/1.10/0001##add_idx_db_project_id.sql
diff --git a/store/migration/prod/LATEST.sql b/store/migration/prod/LATEST.sql
index bfdb660226..d75b0f7375 100644
--- a/store/migration/prod/LATEST.sql
+++ b/store/migration/prod/LATEST.sql
@@ -337,6 +337,8 @@ CREATE INDEX idx_db_instance_id ON db(instance_id);
CREATE UNIQUE INDEX idx_db_unique_instance_id_name ON db(instance_id, name);
+CREATE INDEX idx_db_project_id ON db(project_id);
+
ALTER SEQUENCE db_id_seq RESTART WITH 101;
CREATE TRIGGER update_db_updated_ts
@@ -345,6 +347,29 @@ UPDATE
ON db FOR EACH ROW
EXECUTE FUNCTION trigger_update_updated_ts();
+-- db_schema stores the database schema metadata for a particular database.
+CREATE TABLE db_schema (
+ id SERIAL PRIMARY KEY,
+ row_status row_status NOT NULL DEFAULT 'NORMAL',
+ creator_id INTEGER NOT NULL REFERENCES principal (id),
+ created_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
+ updater_id INTEGER NOT NULL REFERENCES principal (id),
+ updated_ts BIGINT NOT NULL DEFAULT extract(epoch from now()),
+ database_id INTEGER NOT NULL REFERENCES db (id) ON DELETE CASCADE,
+ metadata JSONB NOT NULL DEFAULT '{}',
+ raw_dump TEXT NOT NULL DEFAULT ''
+);
+
+CREATE UNIQUE INDEX idx_db_schema_unique_database_id ON db_schema(database_id);
+
+ALTER SEQUENCE db_schema_id_seq RESTART WITH 101;
+
+CREATE TRIGGER update_db_schema_updated_ts
+BEFORE
+UPDATE
+ ON db_schema FOR EACH ROW
+EXECUTE FUNCTION trigger_update_updated_ts();
+
-- tbl stores the table for a particular database
-- data is synced periodically from the instance
CREATE TABLE tbl (
diff --git a/store/pg_engine_test.go b/store/pg_engine_test.go
index 1754d874fe..031c2a15eb 100644
--- a/store/pg_engine_test.go
+++ b/store/pg_engine_test.go
@@ -220,5 +220,5 @@ func TestMigrationCompatibility(t *testing.T) {
func TestGetCutoffVersion(t *testing.T) {
releaseVersion, err := getProdCutoffVersion()
require.NoError(t, err)
- require.Equal(t, semver.MustParse("1.9.3"), releaseVersion)
+ require.Equal(t, semver.MustParse("1.10.1"), releaseVersion)
}
diff --git a/tests/ghost_test.go b/tests/ghost_test.go
index c07f4fe633..b21857970f 100644
--- a/tests/ghost_test.go
+++ b/tests/ghost_test.go
@@ -12,7 +12,6 @@ import (
"testing"
"github.com/bytebase/bytebase/api"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/db"
"github.com/bytebase/bytebase/resources/mysql"
"github.com/bytebase/bytebase/tests/fake"
@@ -46,9 +45,6 @@ func TestGhostParser(t *testing.T) {
}
func TestGhostSchemaUpdate(t *testing.T) {
- if testReleaseMode == common.ReleaseModeProd {
- t.Skip()
- }
const (
databaseName = "testGhostSchemaUpdate"
mysqlMigrationStatement = `
@@ -205,9 +201,6 @@ func TestGhostSchemaUpdate(t *testing.T) {
}
func TestGhostTenant(t *testing.T) {
- if testReleaseMode == common.ReleaseModeProd {
- t.Skip()
- }
var (
databaseName = "testGhostSchemaUpdate"
mysqlMigrationStatement = `
diff --git a/tests/schema_update_test.go b/tests/schema_update_test.go
index c6d0dd5d1c..5ff2fd74e0 100644
--- a/tests/schema_update_test.go
+++ b/tests/schema_update_test.go
@@ -19,7 +19,6 @@ import (
"github.com/stretchr/testify/require"
"github.com/bytebase/bytebase/api"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/advisor"
"github.com/bytebase/bytebase/plugin/db"
"github.com/bytebase/bytebase/plugin/vcs"
@@ -650,9 +649,6 @@ func TestVCS(t *testing.T) {
}
func TestVCS_SDL(t *testing.T) {
- if testReleaseMode == common.ReleaseModeProd {
- t.Skip()
- }
tests := []struct {
name string
vcsProviderCreator fake.VCSProviderCreator
@@ -1361,9 +1357,6 @@ func TestWildcardInVCSFilePathTemplate(t *testing.T) {
}
func TestVCS_SQL_Review(t *testing.T) {
- if testReleaseMode == common.ReleaseModeProd {
- t.Skip()
- }
tests := []struct {
name string
vcsProviderCreator fake.VCSProviderCreator
@@ -1841,10 +1834,6 @@ func postVCSSQLReview(ctl *controller, repo *api.Repository, request *api.VCSSQL
}
func TestGetLatestSchema(t *testing.T) {
- if testReleaseMode == common.ReleaseModeProd {
- t.Skip()
- }
-
tests := []struct {
name string
dbType db.Type
diff --git a/tests/sensitive_data_test.go b/tests/sensitive_data_test.go
index 0d1f8101ac..cbcd2488ef 100644
--- a/tests/sensitive_data_test.go
+++ b/tests/sensitive_data_test.go
@@ -11,16 +11,12 @@ import (
"github.com/stretchr/testify/require"
"github.com/bytebase/bytebase/api"
- "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/db"
"github.com/bytebase/bytebase/resources/mysql"
"github.com/bytebase/bytebase/tests/fake"
)
func TestSensitiveData(t *testing.T) {
- if testReleaseMode == common.ReleaseModeProd {
- t.Skip()
- }
const (
databaseName = "sensitive_data"
tableName = "tech_book"
diff --git a/tests/sql_review_test.go b/tests/sql_review_test.go
index 26e37c3a53..dd9b8cfbdf 100644
--- a/tests/sql_review_test.go
+++ b/tests/sql_review_test.go
@@ -42,9 +42,6 @@ type test struct {
}
func TestSQLReviewForPostgreSQL(t *testing.T) {
- if testReleaseMode == common.ReleaseModeProd {
- t.Skip()
- }
var (
statements = []string{
`CREATE TABLE "user"(
@@ -378,9 +375,6 @@ func TestSQLReviewForPostgreSQL(t *testing.T) {
}
func TestSQLReviewForMySQL(t *testing.T) {
- if testReleaseMode == common.ReleaseModeProd {
- t.Skip()
- }
var (
databaseName = "testsqlreview"
statements = []string{