Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions clients/python/docs/clone_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Overview

MatrixOne's cloning system provides:

* **Database Cloning**: Create complete copies of databases with all tables and data
* **Database Cloning**: Create copies of MatrixOne-managed database tables and data
* **Table Cloning**: Clone individual tables with or without data
* **Schema-Only Cloning**: Clone database structure without data
* **Selective Cloning**: Clone specific tables or schemas
Expand Down Expand Up @@ -154,7 +154,7 @@ Full Database Cloning

.. code-block:: python

# Clone entire database with all data
# Clone the entire database with MatrixOne-managed data
clone_result = clone_manager.clone_database(
source_database="production_db",
target_database="test_db",
Expand All @@ -180,6 +180,29 @@ Full Database Cloning
include_data=True
)

External Tables and Dependent Objects
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Database cloning intentionally omits external tables because their data and
files are managed outside MatrixOne. Views, SQL functions, and stored
procedures that directly or transitively depend on an omitted external table,
view, or routine are omitted as well. This closure is applied in both
directions, so a view that calls an omitted SQL function is omitted before view
restoration. Independent SQL routine families are copied and rewritten for the
target database. Supported non-SQL UDFs with inline bodies are opaque to the
SQL dependency walker and are copied without body rewriting; imported
non-SQL UDF packages are rejected before the target database is created because
their package objects are not snapshot-versioned. UDF overloads with
the same database, name, and kind are treated as one family: if dependency
inspection omits one overload, the whole family is omitted because clone-time
metadata inspection does not resolve overload types at every call site.
SQL routines whose bodies cannot be structurally inspected are omitted when the
source contains an omitted relation. Executable ``USE`` statements are treated
as uninspectable for this purpose because control-flow branches can make more
than one default database reachable; this avoids assigning an unqualified
reference to the wrong database. Explicit single-table cloning of an external
table remains unsupported.

Schema-Only Cloning
~~~~~~~~~~~~~~~~~~~

Expand Down
102 changes: 58 additions & 44 deletions pkg/frontend/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,17 +502,14 @@ func lockDataBranchCloneDatabaseSources(
if source.snapshot != nil && source.snapshot.Tenant != nil {
fromAccountID = source.snapshot.Tenant.TenantID
}
tables := append([]*tableInfo(nil), source.srcTblInfos...)
tables := append([]*tableInfo(nil), source.sourceTableInfosForLifecycle()...)
sort.Slice(tables, func(i, j int) bool {
if tables[i].dbName != tables[j].dbName {
return tables[i].dbName < tables[j].dbName
}
return tables[i].tblName < tables[j].tblName
})
for _, table := range tables {
if table.typ == view {
continue
}
if err := lockDataBranchCloneSource(
ctx, ses, bh, fromAccountID, table.dbName, table.tblName,
); err != nil {
Expand Down Expand Up @@ -550,10 +547,7 @@ func forEachCloneDatabaseSourceTable(
source cloneDatabaseSource,
fn func(*tableInfo) error,
) error {
for _, table := range source.srcTblInfos {
if table.typ == view {
continue
}
for _, table := range source.sourceTableInfosForLifecycle() {
if err := fn(table); err != nil {
return err
}
Expand Down Expand Up @@ -1053,7 +1047,9 @@ func handleCloneDatabaseWithSource(

ctx1 context.Context

sortedViews []string
sortedViews []string
rewrittenViewMap map[string]*tableInfo
rewrittenViews []string

snapshotTS int64
source cloneDatabaseSource
Expand Down Expand Up @@ -1142,25 +1138,6 @@ func handleCloneDatabaseWithSource(
if err = revalidateTimestampDataBranchCloneDatabaseSource(reqCtx, ses, bh, source); err != nil {
return
}
if source.userDefinedFuncs, err = rewriteCloneUserDefinedFunctionBodies(
reqCtx,
source.userDefinedFuncs,
source.srcResolveDBName,
stmt.DstDatabase.String(),
parserLowerCaseTableNames(ses),
); err != nil {
return
}
if source.storedProcedures, err = rewriteCloneStoredProcedureBodies(
reqCtx,
source.storedProcedures,
source.srcResolveDBName,
stmt.DstDatabase.String(),
parserLowerCaseTableNames(ses),
); err != nil {
return
}

if source.hasFkCycle {
oldForeignKeyChecksReplayable, hadForeignKeyChecksReplayability :=
ses.getMigrationSystemVarReplayability("foreign_key_checks")
Expand Down Expand Up @@ -1283,15 +1260,15 @@ func handleCloneDatabaseWithSource(
return nil
}

for _, srcTbl := range source.srcTblInfos {
for _, srcTbl := range source.cloneableTableInfos() {
if isSequence(srcTbl) {
if err = cloneSequence(srcTbl); err != nil {
return
}
}
}

for _, srcTbl := range source.srcTblInfos {
for _, srcTbl := range source.cloneableTableInfos() {

key := genKey(srcTbl.dbName, srcTbl.tblName)
if _, ok := source.fkTableMap[key]; ok {
Expand All @@ -1313,6 +1290,9 @@ func handleCloneDatabaseWithSource(
// clone foreign key related table
for _, key := range source.sortedFkTbls {
if tblInfo := source.fkTableMap[key]; tblInfo != nil {
if !isCloneableCloneDatabaseTable(tblInfo) {
continue
}
if err = cloneTable(
stmt.DstDatabase.String(), tblInfo.tblName,
stmt.SrcDatabase.String(), tblInfo.tblName,
Expand All @@ -1322,6 +1302,54 @@ func handleCloneDatabaseWithSource(
}
}

lowerCaseTableNames := parserLowerCaseTableNames(ses)
// Build one omission closure before sorting views. Views and routines can
// depend on each other, so filtering routines after view planning would
// allow a view to bind an omitted UDF and fail during restoration.
omissions, err := collectCloneDatabaseOmissionSet(
reqCtx, source, lowerCaseTableNames,
)
if err != nil {
return
}
applyCloneDatabaseOmissionSet(&source, omissions, lowerCaseTableNames)

if len(source.viewMap) != 0 {
viewSnapshot := prepareCloneViewSnapshot(source.snapshot, restoreSnapshotTS)
fromAccount := source.opAccountId
if viewSnapshot != nil && viewSnapshot.Tenant != nil {
fromAccount = viewSnapshot.Tenant.TenantID
}

if sortedViews, err = sortedViewInfos(
reqCtx, ses, bh, "", viewSnapshot, source.viewMap, fromAccount, source.toAccountId,
); err != nil {
return
}
}

if err = validateCloneUserDefinedFunctions(source.userDefinedFuncs); err != nil {
return
}
if source.userDefinedFuncs, err = rewriteCloneUserDefinedFunctionBodies(
reqCtx,
source.userDefinedFuncs,
source.srcResolveDBName,
stmt.DstDatabase.String(),
lowerCaseTableNames,
); err != nil {
return
}
if source.storedProcedures, err = rewriteCloneStoredProcedureBodies(
reqCtx,
source.storedProcedures,
source.srcResolveDBName,
stmt.DstDatabase.String(),
lowerCaseTableNames,
); err != nil {
return
}

// Routines are catalog metadata rather than mo_tables. Restore functions
// before views so view binding can resolve function dependencies.
routineTenant := ses.GetTenantInfo()
Expand All @@ -1346,20 +1374,6 @@ func handleCloneDatabaseWithSource(

// clone view table
if len(source.viewMap) != 0 {
viewSnapshot := prepareCloneViewSnapshot(source.snapshot, restoreSnapshotTS)
fromAccount := source.opAccountId
if viewSnapshot != nil && viewSnapshot.Tenant != nil {
fromAccount = viewSnapshot.Tenant.TenantID
}

if sortedViews, err = sortedViewInfos(
reqCtx, ses, bh, "", viewSnapshot, source.viewMap, fromAccount, source.toAccountId,
); err != nil {
return
}

var rewrittenViewMap map[string]*tableInfo
var rewrittenViews []string
rewrittenViewMap, rewrittenViews, err = rewriteCloneViewInfos(
source.viewMap,
sortedViews,
Expand Down
Loading
Loading