Skip to main content
vsql_branch makes a named copy of a database on the same server, so you can run a migration or a test suite against the copy, reset it to match the source again, and drop it when you are done. A branch is an ordinary database, so any MySQL client or driver connects to it the same way it connects to the source. Branches in the vsql_branch extension work differently from how branches in the VillageSQL managed database service will work when it launches. A vsql_branch branch is one database on your own server, and you reach it through the same connection. A VillageSQL managed database service branch will be a separate server that copies every database of its source, and it has its own connection endpoint.
This extension declares preview capabilities, so vsql_allow_preview_extensions must be ON. Start the server with --vsql_allow_preview_extensions=ON, or run SET PERSIST vsql_allow_preview_extensions = ON.

Install

vsql_branch is not bundled with the server, so build it from the repository first. The build needs the Rust toolchain and cargo-vsql (cargo install cargo-vsql). Point VillageSQL_BUILD_DIR at your server build tree and run, from the repository:
cargo vsql install builds the extension and copies vsql_branch.veb into the server’s lib/veb directory. Then install it, and turn on the background worker that copies, resets, and drops branches:
Until the worker runs, every function except mcp_tools fails:
UNINSTALL EXTENSION vsql_branch deletes the persisted vsql_branch.worker_enabled value, so run the SET PERSIST again after you install the extension again.

What it adds

Every function except mcp_tools takes one JSON string and returns a JSON string. SQL NULL counts as {}. name is the branch name and the name of the new database: 1 to 64 characters from A-Z, a-z, 0-9, and _. src_branch_id is the source database, which can be any database except the system databases. mode is schema-only, the default, which copies every table, view, and trigger without their rows, or data, which copies the rows too. branch_id is the branch name. A SQL function cannot run DDL, so create_branch, reset_branch, and delete_branch queue the work and return at once. Call get_operation until its state is succeeded or failed.

Example

Start from a source database:
Copy it, rows included, into a branch named shop_test. The copy takes a moment, so wait before you check the operation, and check it again while its state is pending or running:
Change the branch. The source does not change:
Reset the branch to match the source again. reset_branch returns the operation itself, so read its id:
A branch takes one operation at a time, so wait for one to succeed before you start the next on the same branch. List the branches with JSON_TABLE:
Drop the branch, check that the operation finished, and drop the source database:

Limits

  • Every account can call the functions, including an account that has only USAGE. The worker runs its SQL without checking the caller’s privileges, so such an account can copy any database that is not a system database and reset or drop any branch. Install the extension only on a server whose accounts you trust with that.
  • delete_branch drops the database of any row in the table vsql_branches.branches. An account that can write that table can add a row for any database that is not a system database, and after the next restart the worker drops that database on request. Grant write access to vsql_branches.branches only to accounts you trust with that.
  • Stored procedures, stored functions, and events are not copied. Tables, views, and triggers are, and the worker changes every name of the source database in a view or trigger to the branch, including text inside a string literal, such as 'see shop.items'.
  • In data mode, writes to the source tables wait while the worker copies their files, and the worker fails the operation if it waits more than 30 seconds for a lock. The tables of a data branch can come from different moments, so branch an idle source when the tables must agree.
  • Operations live in memory, so a restart forgets them.

See also