Help › Change a database's structure

Compare two schemas

Find what differs between two schemas of the same kind of database, choose what to carry over, and get the SQL that changes the target to match the source.

Querlyn 0.1.0 is not released yet. This help describes it ahead of release.

Schema compare reads two schemas, lists what differs, and writes the SQL that would make one of them, the target, look like the other, the source. It compares structure only: tables, views and other objects, never the rows in the tables.

A typical use: your development database has the changes of the last weeks, and you want to know exactly what to run on staging.

Start a comparison

ToDo this
Compare a schema with anotherRight-click a schema, database or connection in the sidebar and choose Compare With… (Command Palette › Database: Compare With…). It becomes the source.
Open an empty comparisonCompare… at the top of the sidebar, or Query › Compare Schemas…

In the tab, pick the two sides:

  • Source (what the target should become)
  • Target (what the script changes)

Each list holds the schemas of your connections. A connection that is not connected is listed so that you can connect it. Then choose Compare.

A side can also be a schema as it was: a snapshot kept by schema history. A snapshot is never changed, so the script is applied only when the target is a schema as it is now. What a comparison reads of a schema is kept in that schema's history.

Both sides must be the same kind of database. PostgreSQL cannot be compared with MySQL, and MySQL and MariaDB count as two kinds. Two schemas of one connection can be compared.

Both schemas are read fresh from their databases, with a progress count. Stop (Command Palette › Database: Stop Comparing) ends the reading. Compare Again (⌘R on macOS or Ctrl+R on Windows and Linux) reads both again. Swap exchanges source and target (Command Palette › Database: Swap Source and Target).

Read the differences

The list groups the objects:

GroupMeaningWhat the script does
Only in SourceThe target does not have it.Creates it.
Only in TargetThe source does not have it.Drops it.
DifferentBoth have it, defined differently.Changes it.
IdenticalThe same on both sides. Hidden until you choose Show Identical (Command Palette › Database: Show Identical Objects).Nothing.

Select an object to see its definition on both sides, side by side, with the differing lines marked. The source's definition is shown as it would read in the target's schema, so that only real differences stand out.

For more room for the definitions, hide the list with the chevron on its edge or ⌘7 on macOS or Ctrl+7 on Windows and Linux (Command Palette › Database: Toggle Compared Objects); the strip it leaves brings it back. Drag its edge to make it wider.

Options

Above the list, under Ignore, you choose what does not count as a difference:

OptionAt the start
Whitespace and comments in definitionsOn
Column orderOn
CollationOff
CommentsOff
Constraint namesOff
Match names ignoring caseOff; offered only where the database itself ignores the case of names

Under Compare you choose the kinds of object: Tables, Views, Functions and Procedures, Sequences, Triggers, and on Oracle Packages and Types.

Changing an option compares again at once, without reading the databases again. The options are not kept when the tab closes.

Choose what goes into the script

Every difference is included at the start. Each object in the list has a check box; clear it to leave that difference out (Space in the list). Include All (Command Palette › Database: Include All Differences in Script) and Include None (Command Palette › Database: Leave All Differences Out of Script) set them all.

What you leave out stays out while the tab is open, also when you compare again.

Renamed tables and columns

Querlyn matches tables and columns by name. A column renamed in the source therefore looks like a column to drop and a column to add.

Warning: Applied as it stands, such a script drops the old column with its data and adds an empty new one.

To have the script rename instead, select the table. Above its definition, Querlyn lists likely pairs as hints, each with a Treat as Rename button (Command Palette › Database: Treat as Rename), and two lists (Only in target, Only in source) to pair any two yourself. Undo Rename Pairing (Command Palette › Database: Undo Rename Pairing) takes a pairing back.

Tables can be paired the same way. Hints are only offered; Querlyn never pairs anything by itself. Views, functions, indexes and constraints cannot be paired.

Use the script

Show Script (Command Palette › Database: Show Migration Script) shows the migration script in place of the object's definition; Show Object goes back. The script's statements carry the same warnings as the table designer's, for example Drops table or Rewrites table; see Read the preview. Dropping a view, function, sequence or trigger is marked Drops object.

ButtonWhat it does
Open in EditorOpens the script in a new query tab on the target's connection and database, without running it (Command Palette › Database: Open Migration Script in Editor).
Copy ScriptCopies it (Command Palette › Database: Copy Migration Script).
Save Script…Saves it as a .sql file (⌘S on macOS or Ctrl+S on Windows and Linux).
Apply to TargetRuns it on the target (⌘↩ on macOS or Ctrl+Enter on Windows and Linux).

Warning: Apply to Target changes the target database and can drop tables and columns. Read the script first. For anything that matters, open it in the editor and review it there, or hand it to whoever reviews your migrations.

Applying works as in the table designer. Querlyn first checks that the target's tables are still as it read them, and asks if they are not (Apply Anyway or Compare Again). On a Production connection it asks before statements that delete something or that it cannot classify. The script runs in one transaction where the database can; see Apply the changes for which databases those are. A read-only target refuses it.

After a successful apply the target is read again, and the list shows what still differs.

What is compared

Tables with their columns, keys, indexes, foreign keys and checks, and then, depending on the database:

DatabaseBesides tables
PostgreSQLViews, materialized views, functions, procedures, sequences, triggers
MySQL, MariaDBViews, functions, procedures, triggers
SQL ServerViews, procedures, functions, triggers, sequences
SQLiteViews, triggers
DuckDBViews, sequences. Macros are not compared.
Amazon RedshiftViews, materialized views, functions, procedures
ClickHouseViews, materialized views, dictionaries. A table whose engine or keys differ is reported, not changed by the script.
Oracle DatabaseViews, materialized views, functions, procedures, packages, types, sequences, triggers
SnowflakeViews, materialized views, functions, procedures, sequences

Not compared on any database: the rows (see Compare the data of two tables), privileges, and partitions. Partitioned tables are compared, but the script does not create them.

Things to know about the script:

  • Where a database refuses to change a table while a view uses it (PostgreSQL, and SQL Server for some views), the script drops those views and creates them again. The summary lists them.
  • A changed view, function or trigger is replaced in place where the database can. Where it must be dropped and created, privileges granted on it must be granted again; the warning says so.

Help for Querlyn 0.1.0. All help topics