fix(oracle): export portable table DDL

This commit is contained in:
zipg authored and GitHub committed 2026-08-17 14:45:25 +08:00
1 parent 1cdee024c2
commit db7463eb70
13 files changed
+241 -30

No files matched your search

+91 -9
View File
@@ -4,6 +4,7 @@ import (
"bufio"
"context"
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
@@ -42,6 +43,9 @@ FROM PRODUCT_COMPONENT_VERSION
WHERE PRODUCT LIKE 'Oracle Database%'
AND ROWNUM = 1`
const oracleDisableSegmentAttributesSQL = `BEGIN DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM, 'SEGMENT_ATTRIBUTES', FALSE); END;`
const oracleEnableSegmentAttributesSQL = `BEGIN DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM, 'SEGMENT_ATTRIBUTES', TRUE); END;`
var (
oraclePlSQLBlockStartRegexp = regexp.MustCompile(`(?is)^\s*(?:DECLARE|BEGIN|CREATE\s+(?:OR\s+REPLACE\s+)?(?:(?:EDITIONABLE|NONEDITIONABLE)\s+)?(?:FUNCTION|PROCEDURE|TRIGGER|PACKAGE(?:\s+BODY)?|TYPE(?:\s+BODY)?))\b`)
oraclePlSQLBlockEndRegexp = regexp.MustCompile(`(?is)\bEND\s*;\s*$`)
@@ -843,7 +847,7 @@ func (s *server) dispatch(method string, params map[string]json.RawMessage) (any
schema := stringParam(params, "schema")
table := stringParam(params, "table")
objectType := stringParam(params, "object_type")
ddl, err := s.getTableDDL(schema, table, objectType)
ddl, err := s.getTableDDLWithOptions(schema, table, objectType, boolParam(params, "portable"))
return ddl, false, err
case "execute_query":
var opts queryOptions
@@ -2881,6 +2885,10 @@ func (s *server) normalizeSchemaForIdentity(schema string) (string, error) {
}
func (s *server) getTableDDL(schema, table, objectType string) (string, error) {
return s.getTableDDLWithOptions(schema, table, objectType, false)
}
func (s *server) getTableDDLWithOptions(schema, table, objectType string, portable bool) (string, error) {
var err error
schema, err = s.normalizeSchema(schema)
if err != nil {
@@ -2898,9 +2906,32 @@ func (s *server) getTableDDL(schema, table, objectType string) (string, error) {
return s.buildViewDDL(schema, table)
}
var ddl string
err = db.QueryRow("SELECT DBMS_METADATA.GET_DDL(:1, :2, :3) FROM DUAL", objectType, table, schema).Scan(&ddl)
var indexDDLs []string
if portable && objectType == "TABLE" {
var metadataErr error
err = withOraclePortableMetadataSession(db, func(conn *sql.Conn) error {
metadataErr = conn.QueryRowContext(
context.Background(),
"SELECT DBMS_METADATA.GET_DDL(:1, :2, :3) FROM DUAL",
objectType,
table,
schema,
).Scan(&ddl)
indexDDLs, _ = loadTableIndexDDLsFromConn(conn, schema, table)
return nil
})
if err != nil {
return "", err
}
err = metadataErr
} else {
err = db.QueryRow("SELECT DBMS_METADATA.GET_DDL(:1, :2, :3) FROM DUAL", objectType, table, schema).Scan(&ddl)
}
if err == nil && strings.TrimSpace(ddl) != "" {
if objectType == "TABLE" {
if portable {
return s.appendTableDependentDDLWithIndexes(schema, table, ddl, indexDDLs), nil
}
return s.appendTableDependentDDL(schema, table, ddl), nil
}
return ddl, nil
@@ -2910,12 +2941,41 @@ func (s *server) getTableDDL(schema, table, objectType string) (string, error) {
if fallbackErr != nil {
return "", fallbackErr
}
if portable {
return s.appendTableDependentDDLWithIndexes(schema, table, fallback, indexDDLs), nil
}
return s.appendTableDependentDDL(schema, table, fallback), nil
}
return "", err
}
func withOraclePortableMetadataSession(db *sql.DB, operation func(*sql.Conn) error) (err error) {
conn, err := db.Conn(context.Background())
if err != nil {
return err
}
if _, err = conn.ExecContext(context.Background(), oracleDisableSegmentAttributesSQL); err != nil {
_ = conn.Close()
return fmt.Errorf("failed to disable Oracle segment attributes: %w", err)
}
defer func() {
if _, resetErr := conn.ExecContext(context.Background(), oracleEnableSegmentAttributesSQL); resetErr != nil {
_ = conn.Raw(func(any) error { return driver.ErrBadConn })
if err == nil {
err = fmt.Errorf("failed to restore Oracle segment attributes: %w", resetErr)
}
}
_ = conn.Close()
}()
return operation(conn)
}
func (s *server) appendTableDependentDDL(schema, table, tableDDL string) string {
indexDDLs, _ := s.loadTableIndexDDLs(schema, table)
return s.appendTableDependentDDLWithIndexes(schema, table, tableDDL, indexDDLs)
}
func (s *server) appendTableDependentDDLWithIndexes(schema, table, tableDDL string, indexDDLs []string) string {
var builder strings.Builder
baseDDL := strings.TrimSpace(tableDDL)
builder.WriteString(baseDDL)
@@ -2931,10 +2991,8 @@ func (s *server) appendTableDependentDDL(schema, table, tableDDL string) string
dependentAppended = true
}
if indexDDLs, err := s.loadTableIndexDDLs(schema, table); err == nil {
for _, ddl := range indexDDLs {
appendDependent(ddl)
}
for _, ddl := range indexDDLs {
appendDependent(ddl)
}
if triggerDDLs, err := s.loadTableTriggerDDLs(schema, table); err == nil {
for _, ddl := range triggerDDLs {
@@ -2949,8 +3007,7 @@ func (s *server) appendTableDependentDDL(schema, table, tableDDL string) string
return builder.String()
}
func (s *server) loadTableIndexDDLs(schema, table string) ([]string, error) {
rows, err := s.queryRows(`
const oracleTableIndexDDLsSQL = `
SELECT DBMS_METADATA.GET_DDL('INDEX', i.INDEX_NAME, i.OWNER)
FROM ALL_INDEXES i
WHERE i.TABLE_OWNER = :1
@@ -2964,11 +3021,27 @@ WHERE i.TABLE_OWNER = :1
AND c.CONSTRAINT_TYPE IN ('P', 'U')
AND c.INDEX_NAME IS NOT NULL
)
ORDER BY i.INDEX_NAME`, []any{schema, table, schema, table})
ORDER BY i.INDEX_NAME`
func (s *server) loadTableIndexDDLs(schema, table string) ([]string, error) {
rows, err := s.queryRows(oracleTableIndexDDLsSQL, []any{schema, table, schema, table})
if err != nil {
return nil, err
}
defer s.closeRows(rows)
return scanOracleDDLs(rows)
}
func loadTableIndexDDLsFromConn(conn *sql.Conn, schema, table string) ([]string, error) {
rows, err := conn.QueryContext(context.Background(), oracleTableIndexDDLsSQL, schema, table, schema, table)
if err != nil {
return nil, err
}
defer rows.Close()
return scanOracleDDLs(rows)
}
func scanOracleDDLs(rows *sql.Rows) ([]string, error) {
var result []string
for rows.Next() {
var ddl sql.NullString
@@ -4745,6 +4818,15 @@ func intParam(params map[string]json.RawMessage, key string) int {
return value
}
func boolParam(params map[string]json.RawMessage, key string) bool {
if params == nil || len(params[key]) == 0 {
return false
}
var value bool
_ = json.Unmarshal(params[key], &value)
return value
}
func errorResponse(id json.RawMessage, err error) response {
return response{JSONRPC: "2.0", ID: id, Error: &rpcError{Code: -1, Message: err.Error()}}
}