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 do | What to use | What changes |
|---|---|---|
| Edit the design without changing the database | Work offline | The design |
| Decide each difference yourself | Review the differences | What you choose |
| Update the design from the database | Refresh the model from the database | The design |
| Update the database from the design | Write the model into the database | The database |
| Get the SQL without running it | Generate SQL script, in Apply the changes | Nothing |
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.
- Click the connection button in the toolbar. It shows the name of the connection, here PostgreSQL.
- Choose Offline (connect later).
Database → Offline (connect later) does the same. The connection button now reads Offline:
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
- Click the connection button and choose your connection, here PostgreSQL.
- DbSchema connects to the database and asks how you want to bring the database and the design together.
| Choice | What DbSchema does | What changes |
|---|---|---|
| Compare, then decide difference by difference | Reads the database and lists every difference | Only what you choose, row by row |
| Write the model into the database | Generates the CREATE and ALTER statements and shows them before they run | The database |
| Skip | Connects and does nothing else | Nothing |
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.
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.
- 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.
- 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.
- 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:
| Difference | Left button: update the design | Right button: update the database |
|---|---|---|
birth_date exists only in the design | Drop: remove the column from the design | Create: add the column to the database |
carrier was renamed to carrier_name in the design | Rename: rename it back in the design | Rename: rename it in the database |
suppliers exists only in the database | Create: add the table to the design | Drop: 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.
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:
| Button | What changes |
|---|---|
| Apply to the model | The design, with the rows that update the design |
| Commit in the database | The database, with the rows that update the database |
| Generate SQL script | Nothing: DbSchema writes the statements without running them |
Generate SQL script asks where the script goes, an SQL Editor or a File:
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.
- Press Refresh in the toolbar, or choose Database → Refresh Model from Database.
- DbSchema compares the whole design with the database. If nothing differs, it says "Schemas are identical".
- If there are differences, the Schema Synchronization dialog shows how many it found.
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.
- Choose Database → Create or Upgrade Model in the Database. If you're offline, DbSchema first opens Database Connections: select the connection and press Connect.
- In Commit Changes into the Database, tick the schemas and tables to create or upgrade, and press OK.
- The Execute Script In Database Dialog shows one statement at a time. Execution Message shows what the database answered.
| Button | What it does |
|---|---|
| Execute | Runs the statement on screen |
| Execute All | Runs every remaining statement |
| Skip | Goes to the next statement without running this one |
| Close | Stops. The remaining statements don't run |
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.
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.
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.
- Choose Model → Compare Model with Other Model From File.
- Choose the
.dbsfile. - The same dialog opens, with Project From File on the right.
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.
- Choose Model → Flyway Migrations....
- In Folder, press Choose and select the folder of your migration files. DbSchema reads them, and never runs them.
- In Compare with, choose what to compare the design with, and press Compare.
- Check the script, then type a Version and a Description. DbSchema shows the file name, such as
V1__paid_orders_view.sql. - Press Write Migration File. If the folder already holds a file of that name, DbSchema asks before it overwrites it.
| Compare with | What the design is compared with |
|---|---|
| Flyway files | The schema that the folder's migration files would build if run in order |
| Database | The connected database |
| Git revision | A version of the design saved in a Git branch, tag or commit. The model file must be in a Git repository |
| Design model file | Another .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:
| Field | What it sets |
|---|---|
| Title | The heading of the report |
| Changes | The direction, such as From DbSchema Model to Database |
| Include | Only the differences that the filter shows, the ignored differences, and the SQL script as an appendix |
| Format | HTML, which opens in any browser and prints to PDF, or Markdown, for a Git repository, a pull request or a wiki |
| File | Where 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:
| Tab | What it sets |
|---|---|
| DDL Script Priorities | The order of the statements in a generated script, lowest Priority # first |
| Synchronization Filter | Java code that leaves out some differences or tables |
| Initialization Script | Java code that runs before each synchronization |
| SQL DDL Scripts | A 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.