Database Schema Version Control with Git: How DbSchema Does It
How DbSchema keeps a database schema in Git as one text file, shows every change as a diff, and writes the SQL that brings a database up to date.
On this page
Words in this article
Words in this article
- Git
- A version control system: it keeps every version of your files and who changed what.
- Commit
- A saved version of your changes, with a message.
- Branch
- A separate line of work, merged back when it is ready.
- Pull request
- A request to merge a branch, where a teammate reviews the changes first.
- Diff
- The list of lines that changed between two versions of a file.
- Merge conflict
- Two branches changed the same lines, and a person picks the result.
- Model file (.dbs)
- The file where DbSchema saves tables, keys and diagrams, written as XML.
- XML
- A plain-text format with tags, one item per line here.
- Migration script
- The SQL statements that change a database from one version of the schema to the next.
The short answer
Your application code goes through branches and reviews, but the database still changes through scripts someone ran by hand. Nobody can say who added a column, or which version runs on the test server. Database schema version control puts the schema in Git as a file, and the database changes only from that file. DbSchema, a desktop tool for database design, solves this in three parts:
One text file per schema
DbSchema saves tables, keys and diagrams in one
.dbsmodel file. The file is plain XML, so Git compares it line by line.Git inside DbSchema
Commit, push, pull, branch and solve conflicts in File → Git Collaboration, with no other Git program.
SQL from the change
DbSchema compares two versions of the schema and writes the
ALTER TABLEstatements, so you read them before they run.
What DbSchema adds to database version control
Why DbSchema
The model is DbSchema's copy of the schema: tables, columns, keys and diagram layouts, saved in the .dbs file. Git versions that file, and DbSchema turns a merged change into SQL.
Git without a second program
Clone, commit, push, pull and branch from the Git Collaboration dialog.
A diff a reviewer can read
Each column is one line of XML. Model History, a list in the Git dialog, shows every commit with the tables it changed.
Conflicts solved side by side
When two people changed the same lines, DbSchema shows both versions next to each other, and you pick the result.
SQL you read before it runs
Schema synchronization compares the model with a database or an older model file and writes the SQL.
Then, from the same model file:HTML5 documentationChange report in MarkdownModel validation
Key takeaways
- DbSchema saves the whole schema in one XML model file, so a schema change is a short Git diff.
- DbSchema's Git Collaboration dialog commits, pulls and solves merge conflicts without a second program.
- DbSchema compares two versions of the model and writes the migration script for the difference.
- Git changes only the file. The database changes when you run the script DbSchema generated.
What users say
DBSchema stores its models in an XML file that can easily be added to a git repo. This allows tracking of the model versions.
Then I can easily make updates either to the model or to the database and keep them synchronized. I can easily switch connections in order to push changes to multiple environments.
How database schema version control works with a model file
Git is a version control system. It keeps every version of your files, with who changed what and when[1]. Git shows a change as the lines of text that differ. For a binary file it can only say that the file changed, so the schema needs to be a text file.
Schema versioning comes in two styles. A migration-based repository holds numbered change scripts, such as 004_add_archived_at.sql. A state-based repository holds the full schema as it should be, and a compare step writes the SQL for the difference. DbSchema works state-based. The model file is the target, and DbSchema writes the SQL by comparing that file with a database or with an older version of the file.
A change then travels in five steps. You edit the model on a branch, a separate line of work. A teammate reviews it in a pull request, which is a request to merge the branch into main, the shared branch. After the merge, DbSchema compares and writes the SQL:
Figure 1. Steps 1 to 3 change only the file. The database changes in step 5.
Reviewing a database schema diff in a pull request
A diff is the list of lines that changed between two versions of a file. To see one, we committed a 14-table PostgreSQL model as shop.dbs. The macOS file command reports it as XML, 28,621 bytes. XML is a plain-text format with tags, and DbSchema writes one column per line, as in this excerpt:
<table name="projects" row_count="0" spec="" >
<column name="team_id" type="integer" mandatory="y" />
<column name="name" type="varchar" length="120" mandatory="y" />
<column name="description" type="text" />
On a branch, we added an archived_at column to projects, of type timestamptz (PostgreSQL's short name for a timestamp with time zone). We typed the line in the format the file already uses. git diff shows one added line:
@@ -128,6 +128,7 @@
<column name="is_archived" type="boolean" mandatory="y" >
<defo><![CDATA[false]]></defo>
</column>
+ <column name="archived_at" type="timestamptz" />
<column name="created_by" type="integer" />
A reviewer reads the column name, the type and whether it may be empty, without opening a database. The pull-request side of the review is in reviewing a schema change in a pull request.
The model file also holds the diagram layout. Moving the projects table in the diagram changed one line, its position:
- <entity schema="dbschema_demo.public" name="projects" color="C7F4BE" x="76" y="1558" >
+ <entity schema="dbschema_demo.public" name="projects" color="C7F4BE" x="76" y="1620" >
Figure 2. Both changes are one line in Git. Only the column reaches the database.
Team collaboration on one schema model with Git branches
Each teammate changes the shared model file on a branch and merges it back. A merge conflict happens when two branches change the same lines. We made one: a second branch added owner_id to projects at the same place as archived_at. Merging both into main stopped with CONFLICT (content): Merge conflict in shop.dbs. Git wrote its usual conflict markers into the model file[2]:
<<<<<<< HEAD
<column name="archived_at" type="timestamptz" />
=======
<column name="owner_id" type="integer" />
>>>>>>> add-owner
DbSchema shows the conflicted file in red in the Git dialog. Ours keeps your version, Theirs keeps your teammate's version, and Merge... opens both side by side. Here, take both lines in the Merge window, then press Commit the merge.
In our run, changes to different tables merged with no conflict. A third branch added due_on to tasks, a fourth moved projects in the diagram, and Git merged both on its own.
Turn the merged model into a migration script in DbSchema
A migration script is the SQL that changes a database from one version of the schema to the next. DbSchema writes it from the model file:
-
Save and commit the model Pro
Save with File → Save Model to File. Open File → Git Collaboration, tick
shop.dbs, type a Commit message and press Commit, then Push. -
Pull the merged model Pro
After the merge, press Check for incoming changes, then Pull. Press Reload Project when DbSchema asks.
-
Compare with the version the database has Pro
Save the version that the database runs as a second file, such as the commit of your last release. In a terminal, run
git show <commit>:shop.dbs > shop-before.dbs. Then choose Model → Compare Model with Other Model From File and pick that file. -
Pick the side and generate the script Pro
The dialog shows your model on the left and the file on the right, under Project From File. Set every row to the Project From File side, so each difference becomes SQL for the older version. Press Generate SQL script and choose SQL Editor or File.
01 3 differencesDbSchema found the three new columns and nothing for the diagram move.
02 CreateCreate adds each column to the file, the version the database still has.
03 this one runsThe statement that runs for the selected row.
04 Generate SQL scriptWrites the statements without running them.
DbSchema generated one statement per new column:
ALTER TABLE "public".projects ADD archived_at timestamptz;
ALTER TABLE "public".projects ADD owner_id integer;
ALTER TABLE "public".tasks ADD due_on date;
We ran the script on PostgreSQL 17.6, on a database at the first commit's schema with two rows in projects. All three statements succeeded:
| table_name | column_name | data_type | is_nullable |
|---|---|---|---|
| projects | archived_at | timestamp with time zone | YES |
| projects | owner_id | integer | YES |
| tasks | due_on | date | YES |
The two existing projects kept their rows, with NULL in both new columns. PostgreSQL 17 uses NULL when an added column has no default, and needs no table rewrite[3].
To change a database directly, connect to it and let DbSchema compare the model with the database. The button Commit in the database, a database commit and not a Git commit, then runs the statements one at a time. More on reading a script before it runs is in turning a schema diff into a safe migration script.
Database schema version control best practices
- Keep each database in its own model file. Several model files can share one repository.
- Commit one change at a time, with a message that says what changed and why, such as "Add archived_at to projects for the archive filter".
- Pull often, so your branch stays close to the team's.
- Review the model diff in a pull request before anyone changes a shared database.
- Generate the migration script from the merged file only. A branch can still change during the review.
Each commit carries the author's name and email, shown under Committing as in the Git dialog. With clear messages, the Git history records who changed the schema and why, and Model History shows which tables each commit touched.
Which DbSchema edition fits a team that versions its schema
DbSchema Pro fits developers and DBAs who keep the schema in Git. Git needs a saved model file, and Pro saves the model to a file. Pro also compares and synchronizes schemas. It also generates the HTML5 or Markdown documentation for the same repository. DbSchema Architect adds logical and conceptual design, which plans tables and relations before you choose a database. Every download starts with a 15-day trial of all features, and the free Community Edition stays after it. Prices are on the purchase page.
Download DbSchema and let DbSchema read your database into a model. Save it with DbSchema Pro into your repository, and make your first commit in File → Git Collaboration.
Frequently asked questions
Does a Git pull change my database?
No. A pull changes only the model file. The database changes when you run the script DbSchema generated, or commit the changes from the synchronization dialog.
How do I find schema drift between staging and production?
Schema drift means two databases that should match no longer do. Connect DbSchema to each database and choose Database → Refresh Model from Database, and DbSchema lists every difference from the model. Export Report saves the list as HTML or Markdown, to keep in the repository.
How do I deploy the same schema change to several databases?
Connect DbSchema to each database in turn, compare the model with it, and press Commit in the database. DbSchema compares each database with the model, so each one gets only the statements it still needs.
Can I design the schema offline and still use Git?
Yes. Choose Database → Offline (connect later), and your changes stay in the model file while the database stays as it is. Commit the file as usual, and synchronize when you connect again.
Can I use the schema model in a CI/CD pipeline?
Commit the script that Generate SQL script writes next to the model, and let the pipeline run that file. DbSchemaCLI, the command-line client in every DbSchema installation, runs SQL script files, for example in a scheduled task.
Sources
Keep the schema in Git as a readable file
DbSchema saves the whole design as one XML model file, shows each change as a diff, and writes the SQL for every merged change. Saving the model to a file and schema synchronization are Pro Edition features.
