Skip to content

Structure sync

  • PostgreSQL
  • MySQL
  • MariaDB

Structure sync compares the schema of a source database with a target and turns the differences into operations: create, alter, drop and rename. You tick the ones you want, Querybara writes a script in dependency order, applies it to the target, and compares again. Use it to bring staging in line with development, or to check that two servers match.

Structure compare of the Larchwood database against its staging copy: eight differences grouped by extensions, tables, columns, indexes and foreign keys, the destructive drop of shop.customers.loyalty_tier left unticked, and the source and target definitions of shop.products.price side by side with the ALTER TABLE statement below.Structure compare of the Larchwood database against its staging copy: eight differences grouped by extensions, tables, columns, indexes and foreign keys, the destructive drop of shop.customers.loyalty_tier left unticked, and the source and target definitions of shop.products.price side by side with the ALTER TABLE statement below.
Structure sync groups every difference, leaves destructive operations unticked, and shows both definitions before you apply anything.
Sync to zeroStructure sync compares two databases, lists create, alter and drop operations with destructive ones unticked, generates a dependency-ordered script, applies exactly the reviewed script in one transaction on PostgreSQL, and re-compares until the applied differences are gone.larchwoodsourcelarchwood_stagingtargetDifferences5 operations1 left: the drop you kept+ create table shop.reviews~ alter shop.products add weight_kg~ alter shop.products price numeric(10,2)+ create index orders_placed_at_idx− alter shop.customers drop loyalty_tiersync.sqlBEGIN;CREATE TABLE shop.reviews (…);ALTER TABLE shop.products ADD COLUMN weight_kg …;… COMMIT;sha-256 ✓schemaschemaorderedsync.sql

Sync to zero

  1. Compare reads the structure of both databases.
  2. The differences become operations to create, alter and drop. You see both definitions side by side.
  3. Destructive operations start unticked: a drop runs only if you choose it.
  4. The selection becomes one script in dependency order, so a table exists before its foreign keys need it.
  5. Apply runs only the reviewed script (its hash must match), in one transaction on PostgreSQL.
  6. Compare runs again. Everything you applied is gone; only the drop you left unticked remains.
  1. Choose Compare in the title bar, then Compare structure…. Or right-click a connection, a database or a PostgreSQL schema in the sidebar and choose Compare structure with…, which fills in the source.
  2. Under Source, pick the Source connection and Source database. The source is the structure you want.
  3. Under Target, pick the connection and database to change. List databases connects and fills the list. On PostgreSQL you can limit both sides to some schemas, such as shop.
  4. Open Options and tick what to ignore (see below).
  5. Choose Compare.

The compare runs as a job in the job runner, so it shows in the Jobs panel with progress and can be cancelled. ⇄ swaps the source and target.

The result lists the operations grouped by object kind, each with a create, alter, drop or rename badge, a tick box, and a summary of what changes. The header counts the differences, the destructive ones and how many are selected.

  • Destructive operations, which lose data or code, arrive unticked and are marked destructive.
  • Select safe ticks every non-destructive operation; Select all and Select none do what they say.
  • Click an operation to see the source and target definitions side by side on the Source and target tab.
  • The Script tab shows the script for the ticked operations, in dependency order.

If a ticked operation needs one that is not ticked (a view on a new table, say), a warning names the missing operations.

A table, column, index, constraint or view renamed in the source would otherwise show as a drop and a create. Under Rename mapping, choose Add a rename and give the object, its name in the target and its name in the source; the target is then renamed instead. With Detect renamed indexes and constraints on, identical drop and create pairs of those become renames.

  1. Choose Apply…. The Review and apply dialog shows the target, the script, and every destructive operation by name.
  2. If the dialog asks for it, tick I reviewed this script and want to run it.
  3. Choose Apply (or Apply to production for a production connection).

Querybara runs only the script you reviewed: it sends the script’s SHA-256 hash with the selection, the job runner generates the script again, and it refuses to run a script that differs. After applying, it reads the target again and compares it with the source: the notice says the target now matches the source, or how many differences remain.

Target How the script runs
PostgreSQL In one transaction: if a statement fails, nothing is changed
MySQL and MariaDB Statement by statement. DDL is not transactional, so statements before a failure stay applied, and you must tick the confirmation
Option What it does
Ignore comments Comments do not count as differences
Ignore collation Collations do not count; character sets still do
Ignore auto-increment values MySQL and MariaDB counters
Ignore DEFINER MySQL and MariaDB routines and views
Ignore ownership PostgreSQL owners
Ignore privileges Grants do not count
Ignore partitions Partitioning does not count
Ignore column order MySQL and MariaDB
Ignore name case MySQL and MariaDB
Ignore names of generated constraints and indexes Matches them by definition
Ignore extension versions PostgreSQL
Detect renamed indexes and constraints Renames instead of drop and create
  • Save comparison… keeps the two connections, databases, schemas, options and rename mapping under a name. Reopen it from Compare › Saved comparisons… with Open, and compare again. A saved comparison does not keep results; the comparison itself lasts while its panel is open.
  • Schedule… next to a saved comparison in Saved comparisons… runs it on a schedule. A run that finds differences writes the HTML report.
  • Export script… writes the script for the ticked operations to a .sql file.
  • Export report… writes an HTML report of the comparison.

querybara compare compares a source and a target. It exits 0 with no differences, 1 with differences (or differences remaining after --apply), and 2 on errors.

Terminal window
querybara compare dev staging --schema shop --out deploy.sql --html report.html
querybara compare dev staging --include-destructive --apply --yes

Destructive operations start unselected here too; --include-destructive selects them. --apply on MySQL and MariaDB needs --yes.

Documents Querybara 0.1.1 · built frombc9f5aa