Schema Synchronization

You changed the design offline, or someone changed the database with a migration script. Now the two no longer match. Schema synchronization compares the design with the database and lists every difference. For each difference, you choose: update the design to match the database, or update the database to match the design. DbSchema generates the SQL that updates the database, and shows it before it runs.

The design, also called the model, is your DbSchema project: the tables, columns and diagrams that you see in DbSchema. DbSchema saves it in a .dbs file. The database is the real database on the server. Synchronization needs DbSchema Pro. Database Terms explains model, schema and the other words on this page.

What you want to doWhat to useWhat changes
Edit the design without changing the databaseWork offlineThe design
Decide each difference yourselfReview the differencesWhat you choose
Update the design from the databaseRefresh the model from the databaseThe design
Update the database from the designWrite the model into the databaseThe database
Get the SQL without running itGenerate SQL script, in Apply the changesNothing

Work offline

While connected to a database, DbSchema applies each change to the database as soon as you confirm it with OK. Offline, your changes stay in the design, and the database doesn't change. So you can review the changes before they run, or work while the database is out of reach.

  1. Click the connection button in the toolbar. It shows the name of the connection, here PostgreSQL.
  2. Choose Offline (connect later).

Database → Offline (connect later) does the same. The connection button now reads Offline:

The DbSchema toolbar with the connection button reading Offline, next to the Refresh button

Edit tables, columns and keys as usual. Your changes are only in the design, so save it with File → Save Model to File.

Connect again and compare with the database

  1. Click the connection button and choose your connection, here PostgreSQL.
  2. DbSchema connects to the database and asks how you want to bring the database and the design together.
The dialog Connected to the Database, with the choices Compare, then decide difference by difference and Write the model into the database, and the Skip button
ChoiceWhat DbSchema doesWhat changes
Compare, then decide difference by differenceReads the database and lists every differenceOnly what you choose, row by row
Write the model into the databaseGenerates the CREATE and ALTER statements and shows them before they runThe database
SkipConnects and does nothing elseNothing

Both choices are also in the Database menu. You can choose Skip now and synchronize later.

Compare opens the Synchronize with the Database dialog. DbSchema ticks the tables that you selected on the diagram. If no table is selected, it ticks the whole schema. Tick or untick schemas and tables, then press Compare.

The Synchronize with the Database dialog with the schema shopdb.shop ticked, its tables listed below, and the Compare button

If there are differences, the Schema Synchronization dialog shows how many it found. Choose Review Differences.

Review the differences

The Synchronize Model and Database dialog has one row per difference. The DbSchema Model column shows each object as it is in the design. The Database column shows it as it is in the database.

In the picture, the design has a new column birth_date and a column renamed from carrier to carrier_name. The database has a new table suppliers.

The Synchronize Model and Database dialog with three differences: birth_date in customers and the rename of carrier_name set to the database, the table suppliers set to the model, and the ALTER TABLE statement of birth_date in the lower pane
  1. In the Action column of a row, choose what to update. The left button updates the design to match the database. Ignore leaves both as they are. The right button updates the database to match the design.
  2. Select a row. The lower pane shows the SQL that each choice would run. The label "this one runs" marks the side that you chose.
  3. To choose the same action for every row, use Set every row at the top right.

Each button is named after what it does to the row:

DifferenceLeft button: update the designRight button: update the database
birth_date exists only in the designDrop: remove the column from the designCreate: add the column to the database
carrier was renamed to carrier_name in the designRename: rename it back in the designRename: rename it in the database
suppliers exists only in the databaseCreate: add the table to the designDrop: remove the table from the database

When you choose to update the database for birth_date, DbSchema shows:

ALTER TABLE shop.customers ADD birth_date date;

The line above the list shows how many differences will update the design and how many will update the database. It also counts the ignored differences and those without a chosen action. To find a row in a long list, type in Find a table, column, index....

If a table exists only in the database, tick Merge as Virtual before you add it to the design. The table then comes in as a virtual table, which exists only in the model file.

For a view, the lower pane compares the script of the view on both sides, line by line. Previous Change and Next Change jump between the changed lines. With Ignore whitespace ticked, DbSchema compares the lines with all spaces and tabs removed.

The script of the view paid_orders on both sides, with the line ordered_at, which only the database has, marked

Apply the changes

The buttons at the bottom of the dialog apply your choices. Each button shows how many rows it applies, such as Apply 1 to the model:

ButtonWhat changes
Apply to the modelThe design, with the rows that update the design
Commit in the databaseThe database, with the rows that update the database
Generate SQL scriptNothing: DbSchema writes the statements without running them

Generate SQL script asks where the script goes, an SQL Editor or a File:

The Choose Output dialog with SQL Editor selected and the Generate button

For the rows in the picture above, DbSchema generated this script. It contains only the changes that you chose to apply to the database:

ALTER TABLE shop.customers ADD birth_date date    ;

ALTER TABLE shop.shipments RENAME COLUMN carrier TO carrier_name;

Commit in the database opens the Execute Script In Database Dialog, the same dialog as in Write the model into the database. After the statements run, DbSchema compares the design and the database again. It shows the differences that are left. For example, the suppliers row still waits for Apply to the model. Save the design afterwards, so that the file matches the database.

Refresh the model from the database

When someone changed the database outside DbSchema, a refresh shows you what changed.

  1. Press Refresh in the toolbar, or choose Database → Refresh Model from Database.
  2. DbSchema compares the whole design with the database. If nothing differs, it says "Schemas are identical".
  3. If there are differences, the Schema Synchronization dialog shows how many it found.
The Schema Synchronization dialog: Found 3 differences between the DbSchema model and the database, with the choices Review Differences and Refresh Model, and Add new tables to diagram ticked

Refresh Model updates the whole design from the database. The changes that you made only in the design are lost. To keep some of them, choose Review Differences instead, and decide each difference.

Refresh Model changes only the design. If the refresh would delete objects from the design, DbSchema asks before it deletes them. With Add new tables to diagram ticked, the tables that are new in the database are added to the open diagram.

To compare only some schemas or tables, choose Database → Selective Refresh Model Schemas from Database (F5). It opens the Synchronize with the Database dialog first.

Write the model into the database

Create or Upgrade Model in the Database creates a schema that you designed offline in an empty database. It can also update an existing database to match the design.

  1. Choose Database → Create or Upgrade Model in the Database. If you're offline, DbSchema first opens Database Connections: select the connection and press Connect.
  2. In Commit Changes into the Database, tick the schemas and tables to create or upgrade, and press OK.
  3. The Execute Script In Database Dialog shows one statement at a time. Execution Message shows what the database answered.
ButtonWhat it does
ExecuteRuns the statement on screen
Execute AllRuns every remaining statement
SkipGoes to the next statement without running this one
CloseStops. The remaining statements don't run
The Execute Script In Database Dialog showing statement 2 of 2, ALTER TABLE shop.shipments RENAME COLUMN carrier TO carrier_name, with Done in Execution Message

Writing the model into the database changes only the database. The design doesn't change.

Rename a table or a column

When you rename a table or a column in DbSchema, the design remembers its previous name. Synchronization then renames the object in the database with RENAME. Dropping the column and creating it again would delete its data.

The row carrier_name set to Rename into the database, and the statement ALTER TABLE shop.shipments RENAME COLUMN carrier TO carrier_name on the database side

To check the renames, press Renamed Items in the synchronization dialog. The Renamed Elements Dialog lists each renamed table, column or index with its Name and its Previous Name.

The Renamed Elements Dialog: the column carrier_name of shipments with the previous name carrier

To give an object a previous name that DbSchema doesn't know, select its row and press Edit Selected Cell. Then type the name in Previous Name. To treat a renamed object as a new one, press Clear Cell.

Compare the model with another model file

A second .dbs file can hold an older version of the schema, or the schema of another environment.

  1. Choose Model → Compare Model with Other Model From File.
  2. Choose the .dbs file.
  3. The same dialog opens, with Project From File on the right.
The dialog Compare the model with the file: the column birth_date, the rename of carrier_name, the table suppliers and the view paid_orders, with Project From File as the right column

Here the buttons are Apply to the model and Generate SQL script. Nothing runs in a database.

A logical design can be compared only with another logical design, and a physical model only with another physical model.

Write a Flyway migration file

Flyway is a tool that changes a database by running numbered SQL files, called migrations, in order. If your team uses Flyway, DbSchema can write the next migration file from the difference between the design and another source.

  1. Choose Model → Flyway Migrations....
  2. In Folder, press Choose and select the folder of your migration files. DbSchema reads them, and never runs them.
  3. In Compare with, choose what to compare the design with, and press Compare.
  4. Check the script, then type a Version and a Description. DbSchema shows the file name, such as V1__paid_orders_view.sql.
  5. Press Write Migration File. If the folder already holds a file of that name, DbSchema asks before it overwrites it.
The Flyway Manager comparing the design with the Database: the script that drops and creates the view paid_orders, Version 1, Description paid orders view, and the file name V1__paid_orders_view.sql
Compare withWhat the design is compared with
Flyway filesThe schema that the folder's migration files would build if run in order
DatabaseThe connected database
Git revisionA version of the design saved in a Git branch, tag or commit. The model file must be in a Git repository
Design model fileAnother .dbs file

Review in Synchronization opens the same comparison in the synchronization dialog. Writing the migration file needs DbSchema Pro.

Export the differences as a report

A change report lists what was added, removed and changed. You can keep it with a release, or send it to people who don't use DbSchema. Press Export Report in the synchronization dialog:

The Export Change Report dialog with the title, the direction From DbSchema Model to Database, the SQL script ticked, HTML selected, and the file shop-changes.html
FieldWhat it sets
TitleThe heading of the report
ChangesThe direction, such as From DbSchema Model to Database
IncludeOnly the differences that the filter shows, the ignored differences, and the SQL script as an appendix
FormatHTML, which opens in any browser and prints to PDF, or Markdown, for a Git repository, a pull request or a wiki
FileWhere the report goes

Press Export to write the file. With Open the report after export ticked, DbSchema opens it.

The report starts with a table of the tables, columns, indexes and foreign keys that were added, removed or changed. Then it lists the changes of each table:

### Table shipments

| Change | Item | Property | Before | After |
| --- | --- | --- | --- | --- |
| Changed | Column carrier_name | Name | `carrier` | `carrier_name` |

Change the synchronization settings

Choose Database → Synchronization Settings, or press Sync Settings in the synchronization dialog:

The Synchronization Settings dialog on the DDL Script Priorities tab, with the tables of the shop schema and their default priorities
TabWhat it sets
DDL Script PrioritiesThe order of the statements in a generated script, lowest Priority # first
Synchronization FilterJava code that leaves out some differences or tables
Initialization ScriptJava code that runs before each synchronization
SQL DDL ScriptsA Pre Script and a Post Script, run before and after the script that creates the schema

Press Apply & Close to keep the settings.

Common problems

A renamed column shows as a drop and a create

DbSchema has no previous name for the column, for example because the column was renamed outside DbSchema. Open Renamed Items and set the Previous Name, as Rename a table or a column shows.

The columns are in another order after a refresh

DbSchema sorts the columns alphabetically when Order Columns Alphabetically is ticked in Edit → Settings → Reverse Engineering. Untick it to keep the order that the database lists. The order of the columns in DbSchema never changes their order in the database.

Dropping a view fails because other views depend on it

A view depends on another view when its query reads from that view. When the SQL of a view differs, the script drops the view and creates it again. Here the view paid_totals reads from paid_orders:

CREATE VIEW shop.paid_orders AS
SELECT order_id, customer_id, total FROM shop.orders WHERE status = 'PAID';

CREATE VIEW shop.paid_totals AS
SELECT customer_id, sum(total) AS paid FROM shop.paid_orders GROUP BY customer_id;

When paid_orders differs, the script that DbSchema generates for PostgreSQL starts with a plain DROP VIEW:

DROP VIEW shop.paid_orders;

PostgreSQL 17 refuses it:

ERROR:  cannot drop view shop.paid_orders because other objects depend on it
DETAIL:  view shop.paid_totals depends on view shop.paid_orders
HINT:  Use DROP ... CASCADE to drop the dependent objects too.

Press Generate SQL script, send it to an SQL Editor, and edit it before you run it: drop the dependent views first, and create them again after.

Next steps