Skip to content

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

fix(schema/oracle): serve the single-object definitions GetSchemaString asks for (#21348)

Danny Xue34d553f71a8
diff --git a/backend/plugin/schema/oracle/generate_migration_record_test.go b/backend/plugin/schema/oracle/generate_migration_record_test.go
index f84d54b6c9..43b086ca43 100644
--- a/backend/plugin/schema/oracle/generate_migration_record_test.go
+++ b/backend/plugin/schema/oracle/generate_migration_record_test.go
@@ -35,7 +35,6 @@ import (
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/testing/protocmp"
- "github.com/bytebase/bytebase/backend/common/testcontainer"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/db"
"github.com/bytebase/bytebase/backend/plugin/schema"
@@ -57,8 +56,7 @@ var oracleGeneratedName = regexp.MustCompile(`ISEQ\$\$_[0-9]+|SYS_C[0-9]+`)
func TestRecordGenerateMigrationFixtures(t *testing.T) {
ctx := context.Background()
- container := testcontainer.GetTestOracleContainer(ctx, t)
- t.Cleanup(func() { container.Close(ctx) })
+ container := sharedOracleContainer(t)
systemDB := container.GetDB()
require.NoError(t, os.MkdirAll(generateMigrationFixtureDir, 0o755))
diff --git a/backend/plugin/schema/oracle/get_database_definition.go b/backend/plugin/schema/oracle/get_database_definition.go
index b6c2030635..dc011504bc 100644
--- a/backend/plugin/schema/oracle/get_database_definition.go
+++ b/backend/plugin/schema/oracle/get_database_definition.go
@@ -8,9 +8,24 @@ import (
"github.com/bytebase/bytebase/backend/plugin/schema"
)
+// Two of GetSchemaString's object types are deliberately left unregistered.
+//
+// SCHEMA: an Oracle schema is a user, one per database, so its definition is the
+// database definition GetDatabaseDefinition already renders. Only pg registers a
+// schema definition; MSSQL, MySQL and TiDB have real schemas and do not.
+//
+// SEQUENCE: getSequences in backend/plugin/db/oracle/sync.go selects only
+// SEQUENCE_NAME, so synced sequences carry no Start, Increment, MaxValue or
+// Cycle. Rendering one would emit a bare CREATE SEQUENCE that silently drops the
+// real increment and bounds. Registering it has to wait for the sync to read
+// those columns.
func init() {
schema.RegisterGetDatabaseDefinition(storepb.Engine_ORACLE, GetDatabaseDefinition)
schema.RegisterGetTableDefinition(storepb.Engine_ORACLE, GetTableDefinition)
+ schema.RegisterGetViewDefinition(storepb.Engine_ORACLE, GetViewDefinition)
+ schema.RegisterGetMaterializedViewDefinition(storepb.Engine_ORACLE, GetMaterializedViewDefinition)
+ schema.RegisterGetFunctionDefinition(storepb.Engine_ORACLE, GetFunctionDefinition)
+ schema.RegisterGetProcedureDefinition(storepb.Engine_ORACLE, GetProcedureDefinition)
}
func GetDatabaseDefinition(_ schema.GetDefinitionContext, to *storepb.DatabaseSchemaMetadata) (string, error) {
@@ -177,6 +192,46 @@ func GetTableDefinition(_ string, table *storepb.TableMetadata, _ []*storepb.Seq
return buf.String(), nil
}
+// The single-object definitions below all ignore the schema argument: Oracle has
+// one schema per database and the writers emit unqualified names, matching
+// GetTableDefinition and the whole-database output.
+
+func GetViewDefinition(_ string, view *storepb.ViewMetadata) (string, error) {
+ var buf strings.Builder
+ if err := writeView(&buf, view); err != nil {
+ return "", err
+ }
+
+ return buf.String(), nil
+}
+
+func GetMaterializedViewDefinition(_ string, view *storepb.MaterializedViewMetadata) (string, error) {
+ var buf strings.Builder
+ if err := writeMaterializedView(&buf, view); err != nil {
+ return "", err
+ }
+
+ return buf.String(), nil
+}
+
+func GetFunctionDefinition(_ string, function *storepb.FunctionMetadata) (string, error) {
+ var buf strings.Builder
+ if err := writeFunction(&buf, function); err != nil {
+ return "", err
+ }
+
+ return buf.String(), nil
+}
+
+func GetProcedureDefinition(_ string, procedure *storepb.ProcedureMetadata) (string, error) {
+ var buf strings.Builder
+ if err := writeProcedure(&buf, procedure); err != nil {
+ return "", err
+ }
+
+ return buf.String(), nil
+}
+
func writeTable(buf *strings.Builder, table *storepb.TableMetadata) error {
if _, err := buf.WriteString(`CREATE TABLE "`); err != nil {
return err
@@ -250,14 +305,7 @@ func writeTable(buf *strings.Builder, table *storepb.TableMetadata) error {
}
}
- // Write triggers for this table
- for _, trigger := range table.Triggers {
- if err := writeTrigger(buf, trigger); err != nil {
- return err
- }
- }
-
- return nil
+ return writeTriggers(buf, table.Triggers)
}
func writeIndex(buf *strings.Builder, table string, index *storepb.IndexMetadata) error {
@@ -641,6 +689,13 @@ func writeView(buf *strings.Builder, view *storepb.ViewMetadata) error {
if _, err := buf.WriteString(view.Name); err != nil {
return err
}
+ // The synced column names are deliberately not written back as an explicit
+ // list. They would restore the aliases of CREATE VIEW V (RENAMED) AS SELECT
+ // BASE ..., which ALL_VIEWS.TEXT does not carry, but getTableColumns drops
+ // invisible columns (COLUMN_ID IS NULL, sync.go:485) while the query keeps
+ // their expressions -- so a view with an invisible column would get fewer
+ // names than output columns and fail with ORA-01730. Losing an alias beats
+ // emitting DDL that cannot run.
if _, err := buf.WriteString(`" AS `); err != nil {
return err
}
@@ -655,7 +710,10 @@ func writeView(buf *strings.Builder, view *storepb.ViewMetadata) error {
if _, err := buf.WriteString("\n\n"); err != nil {
return err
}
- return nil
+
+ // An INSTEAD OF trigger carries the view's DML behavior, so it belongs with
+ // the view the way writeTable emits a table's triggers.
+ return writeTriggers(buf, view.Triggers)
}
// writeMaterializedView writes a CREATE MATERIALIZED VIEW statement
@@ -680,7 +738,8 @@ func writeMaterializedView(buf *strings.Builder, view *storepb.MaterializedViewM
if _, err := buf.WriteString("\n\n"); err != nil {
return err
}
- return nil
+
+ return writeTriggers(buf, view.Triggers)
}
// writeFunction writes a CREATE FUNCTION statement
@@ -729,6 +788,16 @@ func writeProcedure(buf *strings.Builder, procedure *storepb.ProcedureMetadata)
return nil
}
+// writeTriggers writes the CREATE TRIGGER statements owned by a table or view.
+func writeTriggers(buf *strings.Builder, triggers []*storepb.TriggerMetadata) error {
+ for _, trigger := range triggers {
+ if err := writeTrigger(buf, trigger); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
// writeTrigger writes a CREATE TRIGGER statement
func writeTrigger(buf *strings.Builder, trigger *storepb.TriggerMetadata) error {
// The trigger body should already contain the full CREATE TRIGGER statement
diff --git a/backend/plugin/schema/oracle/get_database_definition_test.go b/backend/plugin/schema/oracle/get_database_definition_test.go
index 755d191dba..85dc420afa 100644
--- a/backend/plugin/schema/oracle/get_database_definition_test.go
+++ b/backend/plugin/schema/oracle/get_database_definition_test.go
@@ -6,6 +6,7 @@ import (
"github.com/stretchr/testify/require"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/schema"
)
func TestGetTableDefinition(t *testing.T) {
@@ -109,3 +110,159 @@ func TestGetTableDefinition(t *testing.T) {
})
}
}
+
+// TestGetObjectDefinition covers the single-object definitions GetSchemaString
+// serves. The metadata is shaped the way Oracle reports it: view and
+// materialized-view definitions are a bare SELECT, and routine definitions
+// start at FUNCTION/PROCEDURE with no CREATE of their own.
+func TestGetObjectDefinition(t *testing.T) {
+ tests := []struct {
+ name string
+ get func() (string, error)
+ want string
+ }{
+ {
+ name: "view",
+ get: func() (string, error) {
+ return GetViewDefinition("", &storepb.ViewMetadata{
+ Name: "DEPT_EMPLOYEE_COUNT",
+ Definition: "SELECT D.ID AS DEPT_ID, COUNT(E.ID) AS EMP_COUNT\nFROM DEPARTMENTS D",
+ })
+ },
+ want: `CREATE VIEW "DEPT_EMPLOYEE_COUNT" AS SELECT D.ID AS DEPT_ID, COUNT(E.ID) AS EMP_COUNT
+FROM DEPARTMENTS D;
+
+`,
+ },
+ {
+ name: "materialized view",
+ get: func() (string, error) {
+ return GetMaterializedViewDefinition("", &storepb.MaterializedViewMetadata{
+ Name: "PRODUCT_STATS",
+ Definition: "SELECT PRODUCT_ID, COUNT(*) AS ORDER_COUNT FROM ORDERS GROUP BY PRODUCT_ID",
+ })
+ },
+ want: `CREATE MATERIALIZED VIEW "PRODUCT_STATS" AS SELECT PRODUCT_ID, COUNT(*) AS ORDER_COUNT FROM ORDERS GROUP BY PRODUCT_ID;
+
+`,
+ },
+ {
+ // ALL_SOURCE hands back the body starting at FUNCTION, so the
+ // CREATE OR REPLACE prefix has to be supplied here.
+ name: "function gains the CREATE OR REPLACE prefix",
+ get: func() (string, error) {
+ return GetFunctionDefinition("", &storepb.FunctionMetadata{
+ Name: "CALCULATE_DISCOUNT",
+ Definition: "FUNCTION CALCULATE_DISCOUNT(AMOUNT NUMBER)\nRETURN NUMBER\nIS\nBEGIN\n RETURN AMOUNT * 0.1;\nEND;",
+ })
+ },
+ want: `CREATE OR REPLACE FUNCTION CALCULATE_DISCOUNT(AMOUNT NUMBER)
+RETURN NUMBER
+IS
+BEGIN
+ RETURN AMOUNT * 0.1;
+END;
+
+`,
+ },
+ {
+ name: "procedure gains the CREATE OR REPLACE prefix",
+ get: func() (string, error) {
+ return GetProcedureDefinition("", &storepb.ProcedureMetadata{
+ Name: "LOG_AUDIT",
+ Definition: "PROCEDURE LOG_AUDIT(P_TABLE_NAME VARCHAR2)\nIS\nBEGIN\n NULL;\nEND;",
+ })
+ },
+ want: `CREATE OR REPLACE PROCEDURE LOG_AUDIT(P_TABLE_NAME VARCHAR2)
+IS
+BEGIN
+ NULL;
+END;
+
+`,
+ },
+ {
+ // An INSTEAD OF trigger carries the view's DML behavior. Dropping it
+ // would hand back schema text that recreates the view read-only.
+ // The body shape is constructTriggerBody's in the Oracle sync: it
+ // prepends CREATE OR REPLACE TRIGGER to ALL_TRIGGERS.DESCRIPTION.
+ name: "view keeps its INSTEAD OF trigger",
+ get: func() (string, error) {
+ return GetViewDefinition("", &storepb.ViewMetadata{
+ Name: "EMPLOYEE_VIEW",
+ Definition: "SELECT EMP_ID, EMAIL FROM EMPLOYEES",
+ Triggers: []*storepb.TriggerMetadata{
+ {
+ Name: "EMPLOYEE_VIEW_INSERT_TRG",
+ Body: "CREATE OR REPLACE TRIGGER EMPLOYEE_VIEW_INSERT_TRG\nINSTEAD OF INSERT ON EMPLOYEE_VIEW\nFOR EACH ROW\nBEGIN\n NULL;\nEND;",
+ },
+ },
+ })
+ },
+ want: `CREATE VIEW "EMPLOYEE_VIEW" AS SELECT EMP_ID, EMAIL FROM EMPLOYEES;
+
+CREATE OR REPLACE TRIGGER EMPLOYEE_VIEW_INSERT_TRG
+INSTEAD OF INSERT ON EMPLOYEE_VIEW
+FOR EACH ROW
+BEGIN
+ NULL;
+END;
+
+`,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got, err := tt.get()
+ require.NoError(t, err)
+ require.Equal(t, tt.want, got)
+ })
+ }
+}
+
+// TestObjectDefinitionsRegisteredForOracle goes through the schema registry
+// rather than calling the functions directly, because the registry lookup is
+// what was missing: GetSchemaString served only DATABASE and TABLE for Oracle
+// and returned "engine ORACLE is not supported" for the rest, while
+// supportGetStringSchema on the frontend lists ORACLE and offers "view schema
+// text" on views.
+func TestObjectDefinitionsRegisteredForOracle(t *testing.T) {
+ tests := []struct {
+ name string
+ get func() (string, error)
+ }{
+ {
+ name: "view",
+ get: func() (string, error) {
+ return schema.GetViewDefinition(storepb.Engine_ORACLE, "", &storepb.ViewMetadata{Name: "V", Definition: "SELECT 1 FROM DUAL"})
+ },
+ },
+ {
+ name: "materialized view",
+ get: func() (string, error) {
+ return schema.GetMaterializedViewDefinition(storepb.Engine_ORACLE, "", &storepb.MaterializedViewMetadata{Name: "MV", Definition: "SELECT 1 FROM DUAL"})
+ },
+ },
+ {
+ name: "function",
+ get: func() (string, error) {
+ return schema.GetFunctionDefinition(storepb.Engine_ORACLE, "", &storepb.FunctionMetadata{Name: "F", Definition: "FUNCTION F RETURN NUMBER IS BEGIN RETURN 1; END;"})
+ },
+ },
+ {
+ name: "procedure",
+ get: func() (string, error) {
+ return schema.GetProcedureDefinition(storepb.Engine_ORACLE, "", &storepb.ProcedureMetadata{Name: "P", Definition: "PROCEDURE P IS BEGIN NULL; END;"})
+ },
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got, err := tt.get()
+ require.NoError(t, err)
+ require.NotEmpty(t, got)
+ })
+ }
+}
diff --git a/backend/plugin/schema/oracle/get_database_definition_testcontainer_test.go b/backend/plugin/schema/oracle/get_database_definition_testcontainer_test.go
index d428dabbe3..4063c5a98e 100644
--- a/backend/plugin/schema/oracle/get_database_definition_testcontainer_test.go
+++ b/backend/plugin/schema/oracle/get_database_definition_testcontainer_test.go
@@ -11,7 +11,6 @@ import (
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/testing/protocmp"
- "github.com/bytebase/bytebase/backend/common/testcontainer"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/schema"
)
@@ -28,9 +27,7 @@ func TestGetDatabaseDefinitionWithTestcontainer(t *testing.T) {
ctx := context.Background()
- // Get Oracle container from testcontainer utility
- container := testcontainer.GetTestOracleContainer(ctx, t)
- t.Cleanup(func() { container.Close(ctx) })
+ container := sharedOracleContainer(t)
// Get SYSTEM database connection for user management
systemDB := container.GetDB()
@@ -66,6 +63,28 @@ CREATE INDEX IDX_EMP_NAME ON EMPLOYEES(NAME);
`,
description: "Basic tables with primary keys, foreign keys, unique constraints, check constraints, and indexes",
},
+ {
+ name: "view_with_instead_of_trigger",
+ initialSchema: `
+CREATE TABLE EMPLOYEES (
+ EMP_ID NUMBER PRIMARY KEY,
+ EMAIL VARCHAR2(100)
+);
+
+CREATE VIEW EMPLOYEE_VIEW AS
+SELECT EMP_ID, EMAIL
+FROM EMPLOYEES;
+
+CREATE OR REPLACE TRIGGER EMPLOYEE_VIEW_INSERT_TRG
+INSTEAD OF INSERT ON EMPLOYEE_VIEW
+FOR EACH ROW
+BEGIN
+ INSERT INTO EMPLOYEES (EMP_ID, EMAIL) VALUES (:NEW.EMP_ID, :NEW.EMAIL);
+END;
+/
+`,
+ description: "A view whose INSTEAD OF trigger must survive the definition round trip",
+ },
}
for _, tc := range testCases {
@@ -144,6 +163,20 @@ CREATE INDEX IDX_EMP_NAME ON EMPLOYEES(NAME);
// normalizeOracleMetadata handles Oracle-specific normalization for metadata comparison
func normalizeOracleMetadata(metadata *storepb.DatabaseSchemaMetadata) {
for _, schema := range metadata.Schemas {
+ // A view's dependency columns record their owning Oracle user, and this
+ // test recreates the schema under a second user by construction, so the
+ // two can never match.
+ for _, view := range schema.Views {
+ for _, dependency := range view.DependencyColumns {
+ dependency.Schema = ""
+ }
+ }
+ for _, view := range schema.MaterializedViews {
+ for _, dependency := range view.DependencyColumns {
+ dependency.Schema = ""
+ }
+ }
+
for _, table := range schema.Tables {
for _, column := range table.Columns {
// Normalize system-generated sequence references in default expressions
diff --git a/backend/plugin/schema/oracle/get_database_metadata_test.go b/backend/plugin/schema/oracle/get_database_metadata_test.go
index b793f3d4b7..be7baf4a57 100644
--- a/backend/plugin/schema/oracle/get_database_metadata_test.go
+++ b/backend/plugin/schema/oracle/get_database_metadata_test.go
@@ -9,7 +9,6 @@ import (
"github.com/google/uuid"
"github.com/stretchr/testify/require"
- "github.com/bytebase/bytebase/backend/common/testcontainer"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/db"
)
@@ -744,9 +743,7 @@ COMMENT ON COLUMN ORDER_LINE_ITEMS.FULFILLMENT_STATUS IS 'Status: PENDING, PICKE
func TestGetDatabaseMetadataWithTestcontainer(t *testing.T) {
ctx := context.Background()
- // Start Oracle container
- container := testcontainer.GetTestOracleContainer(ctx, t)
- t.Cleanup(func() { container.Close(ctx) })
+ container := sharedOracleContainer(t)
host := container.GetHost()
port := container.GetPort()
diff --git a/backend/plugin/schema/oracle/main_test.go b/backend/plugin/schema/oracle/main_test.go
new file mode 100644
index 0000000000..4c22b3453d
--- /dev/null
+++ b/backend/plugin/schema/oracle/main_test.go
@@ -0,0 +1,52 @@
+package oracle
+
+import (
+ "context"
+ "sync"
+ "testing"
+
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
+)
+
+// The package shares one Oracle container, started on first use.
+//
+// Sharing it is worth a container start: the metadata and definition tests took
+// one each, and an Oracle Free start is around 12 s. Starting it lazily is what
+// keeps that saving from turning into a tax on everything else -- the
+// generate-migration goldens, the omni parser cases and the topological-order
+// cases need no engine at all, and a package-wide eager start would have made
+// `go test -run TestGenerateMigration` pay 12 s for a container it never opens.
+var (
+ oracleOnce sync.Once
+ oracleContainer *testcontainer.Container
+ oracleErr error
+)
+
+func TestMain(m *testing.M) {
+ defer func() {
+ if oracleContainer != nil {
+ oracleContainer.Close(context.Background())
+ }
+ }()
+ m.Run()
+}
+
+// sharedOracleContainer returns the package's Oracle container, starting it on
+// the first call. Tests keep isolating themselves with a per-test Oracle user,
+// which is what makes one container enough.
+func sharedOracleContainer(t *testing.T) *testcontainer.Container {
+ t.Helper()
+
+ oracleOnce.Do(func() {
+ container, err := testcontainer.GetOracleContainer(context.Background())
+ if err != nil {
+ oracleErr = err
+ return
+ }
+ oracleContainer = container
+ })
+ if oracleErr != nil {
+ t.Fatalf("failed to start the shared Oracle container: %v", oracleErr)
+ }
+ return oracleContainer
+}