How to Generate Delta Scripts for Database Changes

To create delta scripts for database changes, you must identify the changes made to the schema. In this article, we will describe:

  • how to compare a project created in Luna Modeler database design tool with already existing database and
  • how to generate delta scripts – synchronization SQL scripts with ALTER statements and other necessary SQL – only for the modifications we have made.

Initial Database Design

Let’s have a look at the following sample project created by loading an existing structure from a MariaDB database.

Initial database design created in Luna Modeler | Sample project for generating delta scripts for database synchronization

Our goal is to make the following three common modifications:

  1. In the customer table, we rename the name column to first_name and the surname column to last_name. We will also add column middle_name to this table.
  2. Next, we modify the address table by removing the country column. We will also add a new table country to the project and create a new relationship between the country and address tables.
  3. The last change will be to edit the vendor table, in which we will change the parameter for the company_name column from the value 50 to 150.
Modified database design | Sample project for generating alter statements for database synchronization.

Now let’s see how to generate the synchronization SQL script.

Comparing the Project with the Database

Click the Advanced button and select the Generate synchronization script option.

Generate synchronization SQL | Delta script for MariaDB database

A new form will open, on which we select the target database connection.

Selecting database connection for schema comparison

The connection detail will be displayed and also the settings where we can choose how to perform the comparison and how to generate the script. Now we click on the Load database and detect differences button.

Settings and detection of database schema changes

Luna Modeler will run the comparison against the database and display a summary of the detected changes.

Comparison of database schema | A list of detected differences

Let’s take a look at the individual entries.

We will start with the customer table. In the columns section, we can see the changes. Since we have renamed two columns, we can use a mapping. For the first_name column, we click on the Change mappings button.

Column mapping

This will open a window in which we can select the target column. In this case, we will map the first_name column to the name column.

How to map renamed columns to existing columns in a database.

Similarly, we will map the last_name column to surname.
The middle_name column has been newly added, which is also indicated by the green icon.

Comparison and sychronization in Luna Modeler

The second change was to add the country table and modify the address table. These changes were also successfully detected. In the address table, the country column has been removed, which is also indicated by the icon.

Modifications detected - overview of changes

The last change was a modification in the vendor table. We can view the details of the change simply by clicking on the section we want to expand or collapse.

Differences in parameter values

To see all the details, just click on the Expand All button on the toolbar. To close the detail we can click on the Collapse All button.

Another way to change the view is to use different groupings of items. We can group items by Object type, Difference type or leave them without grouping.

Grouping of the list of detected database schema differences

Delta Scripts – Synchronization SQL with ALTER Statements

Now let’s take a look at the generated SQL script. Let’s click on the SQL Script tab, where the list of SQL statements is displayed. Here we see the statements that modify the customer table.

Delta scripts generated by Luna Modeler - database design tool

Also, the statements that create the country table and modify the address table and create a constraint for the relationship we defined between the country and address tables.

Alter statements created by Luna Modeler - data modeling tool for relational databases.

At the end, we see a statement to modify the company_name column in the vendor table. Here we have changed the parameter from a value of 50 to a value of 150.

Schema synchronization scripts for relational database - generated by Luna Modeler.
Generating HTML reports

Luna Modeler can also be used to generate HTML reports that contain a summary of detected changes and an SQL script. To generate the documentation, just click on the Report button and select the style and folder where to save the report.

In the report, you can open and close sections with differences and also use the standard Find function, which is available in all browsers.

Executing the Delta Scripts

You can copy or save this script and execute it in your preferred database management tool. Notes:

  • And always create a complete backup of your database and associated systems before executing the delta scripts.
  • Executing the generated delta scripts may result in changes to your data, including potential data loss. That’s why the scripts should only be executed in a test environment.

Get started today and try Luna Modeler

Download

#

DATA MODELING TOOLS
DRAW Diagrams and
GENERATE Scripts
Download data modeling tools
Blog Categories
Blog by Platforms