Clone Manager

This section documents the clone management classes that provide database cloning capabilities.

CloneManager

class matrixone.clone.CloneManager(client, executor=None)[source]

Bases: BaseCloneManager

Synchronous clone management for MatrixOne database operations.

This class provides comprehensive database and table cloning functionality, enabling efficient data replication, testing environments, and data distribution scenarios. Cloning operations can work with current data or point-in-time snapshots.

Key Features:

  • Database cloning: Clone entire databases with all tables and data

  • Table-level cloning: Clone specific tables for targeted data replication

  • Snapshot-based cloning: Clone from specific point-in-time snapshots

  • Efficient native operations: Uses MatrixOne’s native CLONE functionality for performance

  • Transaction-aware: Full integration with transaction contexts via executor pattern

  • Flexible naming: Support for IF NOT EXISTS clause to prevent conflicts

  • Cross-database cloning: Clone tables between different databases

Executor Pattern:

  • If executor is None, uses self.client.execute (default client-level executor)

  • If executor is provided (e.g., session), uses executor.execute (transaction-aware)

  • This allows the same logic to work in both client and session contexts

  • All operations can participate in transactions when used via session

Usage Examples:

from matrixone import Client

client = Client(host='localhost', port=6001, user='root', password='111', database='test')

# Clone entire database (current state)
client.clone.clone_database(
    target_db='production_backup',
    source_db='production'
)

# Clone database with IF NOT EXISTS
client.clone.clone_database(
    target_db='dev_environment',
    source_db='production',
    if_not_exists=True
)

# Clone database from a specific snapshot
client.clone.clone_database(
    target_db='test_environment',
    source_db='production',
    snapshot_name='daily_backup_2024_01_01'
)

# Clone specific table
client.clone.clone_table(
    target_table='users_backup',
    source_table='users'
)

# Clone table across databases
client.clone.clone_table(
    target_table='dev_db.users',
    source_table='prod_db.users'
)

# Clone table from snapshot with validation
client.clone.clone_table_with_snapshot(
    target_table='users_202401',
    source_table='users',
    snapshot_name='monthly_backup',
    if_not_exists=True
)

# Using within a transaction (all operations are atomic)
with client.session() as session:
    # Clone multiple databases atomically
    session.clone.clone_database('backup_db1', 'source_db1')
    session.clone.clone_database('backup_db2', 'source_db2')
    # Both clones succeed or fail together

Version Requirements:

Clone functionality requires MatrixOne version 1.0.0 or higher. Earlier versions do not support native cloning operations.

See also

  • SnapshotManager: For creating snapshots before cloning

  • RestoreManager: For restoring databases from snapshots

  • MetadataManager: For managing database metadata

clone_database(target_db: str, source_db: str, snapshot_name: str | None = None, if_not_exists: bool = False) None[source]

Clone a database.

Parameters:
  • target_db – Target database name

  • source_db – Source database name

  • snapshot_name – Optional snapshot name for point-in-time clone

  • if_not_exists – Use IF NOT EXISTS clause

Raises:
  • ConnectionError – If not connected to database

  • CloneError – If clone operation fails

clone_table(target_table: str, source_table: str, snapshot_name: str | None = None, if_not_exists: bool = False) None[source]

Clone a table.

Parameters:
  • target_table – Target table name (can include database: db.table)

  • source_table – Source table name (can include database: db.table)

  • snapshot_name – Optional snapshot name for point-in-time clone

  • if_not_exists – Use IF NOT EXISTS clause

Raises:
  • ConnectionError – If not connected to database

  • CloneError – If clone operation fails

clone_database_with_snapshot(target_db: str, source_db: str, snapshot_name: str, if_not_exists: bool = False) None[source]

Clone a database using a specific snapshot.

Parameters:
  • target_db – Target database name

  • source_db – Source database name

  • snapshot_name – Snapshot name for point-in-time clone

  • if_not_exists – Use IF NOT EXISTS clause

Raises:
  • ConnectionError – If not connected to database

  • CloneError – If clone operation fails or snapshot doesn’t exist

clone_table_with_snapshot(target_table: str, source_table: str, snapshot_name: str, if_not_exists: bool = False) None[source]

Clone a table using a specific snapshot.

Parameters:
  • target_table – Target table name (can include database: db.table)

  • source_table – Source table name (can include database: db.table)

  • snapshot_name – Snapshot name for point-in-time clone

  • if_not_exists – Use IF NOT EXISTS clause

Raises:
  • ConnectionError – If not connected to database

  • CloneError – If clone operation fails or snapshot doesn’t exist