fork_database - Fork a database
fork_database() copies a specified database at a consistent snapshot time to create a destination database that can be read and written independently. Fork Database can be used through SQL or the SDK; this topic describes the fork_database() interface in the pyseekdb SDK.
-
This interface can only be used when you are connected using
AdminClient. For more information aboutAdminClient, see Admin Client. -
Fork Database is currently an experimental feature. Use it cautiously in production environments.
Prerequisites
-
The seekdb version must be
1.2.0.0or later. -
You have installed pyseekdb. For more information about how to install pyseekdb, see Get Started.
-
You have connected to the seekdb instance using
AdminClient. For more information about how to connect, see Admin Client. -
The source database already exists and the destination database does not. We recommend checking whether the destination database exists beforehand using list_databases() or get_database().
-
The limitations that apply to
fork_database()are the same as forFORK DATABASE. Before you call this interface, confirm that the source database satisfies theFORK DATABASElimitations; otherwise the call may fail. For details, see FORK DATABASE.
Request parameters
fork_database(source_name, destination_name, tenant=DEFAULT_TENANT)
| Parameter | Type | Required | Description | Example value |
|---|---|---|---|---|
source_name | string | Yes | The source database name. | db_src |
destination_name | string | Yes | The destination database name. The destination database must not already exist. | db_fork |
tenant | string | No | Tenant name. Use this only in OceanBase mode. When using embedded seekdb or server mode seekdb, you do not need to set this parameter. | test_tenant |
Request examples
Basic call
import pyseekdb
admin = pyseekdb.AdminClient(
host="127.0.0.1",
port=2881,
user="root",
password="***",
)
# Equivalent to executing the SQL statement `FORK DATABASE db_src TO db_fork`
forked_db = admin.fork_database("db_src", "db_fork")
print(forked_db.name)
Verify that the destination database is reachable
After a successful call, you can connect to the destination database with pyseekdb.Client and run ordinary queries. The following example uses count_collection() to verify that the destination database db_fork is accessible.
import pyseekdb
client = pyseekdb.Client(
host="127.0.0.1",
port=2881,
database="db_fork",
user="root",
password="***",
)
print(client.count_collection())
Response
On success, the call returns a Database object.
Response parameters
| Parameter | Type | Required | Description | Example value |
|---|---|---|---|---|
name | string | Yes | The destination database name. | db_fork |
tenant | string | No | The tenant that owns the destination database. In embedded seekdb and server mode seekdb, this value is None; in OceanBase mode, it is the actual tenant name. | test_tenant |
charset | string | No | The character set of the destination database. | utf8mb4 |
collation | string | No | The collation of the destination database. | utf8mb4_general_ci |
metadata | dict | No | Additional metadata. | {} |
Response example
forked_db = admin.fork_database("db_src", "db_fork")
print(forked_db.name)
On success, the output is:
db_fork
Considerations
- The behavior of
fork_database()is equivalent to executing the SQLFORK DATABASE source_name TO destination_name. - Data in the destination database is a consistent snapshot of the source database at fork time. You can read and write it independently afterward; it does not affect the source database.
- If the seekdb version is lower than
1.2.0.0, or the destination database already exists, the interface raisesValueError. - If the source database does not satisfy the limitations (for example, it contains unsupported objects), or the current user lacks sufficient privileges, the call fails.