logo

Manual for deltasql 1.4.4

Summary

Database Evolution Under Control

(how deltasql works)

A strong version control system for databases saves time to developers, who would like to keep their schema updated while developing with minimal effort. With a version control for databases, the number of mistakes dued to different schemas or missing scripts is considerably reduced. Critical errors in production due to schema inconsistencies disappear, too.

deltasql is an Open Source tool to synchronize databases with source code. While developing middle size or big applications, developers undertake changes to the data model which go along with changes to the source code. From time to time, branches of source code are done to stabilize the code which will go to production. A sort of data model branch is also needed.

deltasql provides a simple set of php scripts to be executed on an apache server backed by a mySQL database to collect all scripts which change the data model, and means to handle data model branches. The trick is to number the sql scripts and to create on each database instance a table which keeps the number of the latest executed script (the table deltasql uses is named TBSYNCHRONIZE).

A form allows the user to enter data from the synchronization table and thereafter the needed chain of datamodel updates is computed and shown to the user. The user has to manually execute all scripts. It is possible to update development schemas (the HEADs) and production schemas (the branches), to transform a production schema into a development schema and vice versa. Also in case of a schema dumped and imported into another database, it is still upgradeable as the synchronization table is contained into the copyed schema. However, it is not possible to downgrade a schema back to a previous version.

There are deltasql clients (listed in the table above), which automatically collect synchronization data from a given database schema. Though deltasql works best with Oracle, PostgreSQL and mySQL schemas, any other database type can use most of deltasql functionality.

Purpose

deltasql Server allows developers to submit their SQL scripts in a central place. Each time a script is submitted, deltasql assigns a number to it (the script's version). Database administrators can check at which version the database table is, by checking the last inserted row in the table TBSYNCHRONIZE. They also can look at the project name in this table. They can then ask the deltasql Server for the currently missing scripts.


With deltasql: A developer sends the script to the deltasql server only, the exact database state is always known and can be synchronized to any state.


When scripts are sent, a database module has to be chosen. A project can consist of one or several modules, as in some companies, they tipically use a three-level structure for their source code modules. The first database module is the lowest level and represents tables which are common for all projects in a company. The middle database module represents the tables of the application which is sold. The highest level module contains tables which are needed for the customization of one particular customer, and which are not needed for other customers.

For simple applications, it is enough to specify one module for each project, although deltasql was developed as help in a much more complex development environment.

deltasql also supports database branches, which are similar to source code branches. It is possible to choose, if a script belongs also to a particular branch. And it is possible to update a branch database to a newer branch, or to the development schema represented by the branch named HEAD.

deltasql Server is written in such a way, that clients can interface to it and query the database automatically. Deltasql is shipped with several clients which easy the interface to database schemas and sql clients, which show latest available scripts and which can perform continouus database integration tasks. People in the Open Source community are encouraged to write their own clients, or to partecipate in improvements of the server itself.


Without deltasql: A developer has to send his/her scripts to everyone, the exact database state is known only to the database owner.


If you find the project useful, or if you use deltasql for managing your Open Source project, you are encouraged to add this button to your homepage. Link it to the project wiki at http://www.deltasql.org/wiki/.This might bring some additional traffic and maybe involve others in the project who will improve the quality of this software. Thanks!

Install Guides

Install steps for the Server

deltasql runs on Apache webserver with the PHP module backed by a mySQL database.

If you want to run it on windows, you should first download from the Apachefriends.org website XAMPP, a package which bundles Apache, mySQL and phpMyAdmin (and more... :-) for Linux and Windows. deltasql was developed with XAMPP 1.5.5, but any newer version might work as well. Then you should install XAMPP. Using the XAMPP Control Panel start Apache and mySQL.

Of course, XAMPP is not a strict requirement, just a good entry point for newbies. Almost all Linux distributions come with options to install Apache, Apache PHP module and a mySQL database. deltasql works with them as well. deltasql.org runs on a Linux Ubuntu distribution with Apache 2.2, PHP 5.2.3 and mySQL 5.0.

As next step, download the deltasql PHP scripts, unzip them and copy them to the htdocs directory created by XAMPP or by your Linux distribution, so that you get a htdocs/deltasql directory full of PHP scripts.

If you visit the main page index.php, there is a link called Please Setup Deltasql.... Click on this link and follow the instructions there. Set a password for the admin user of deltasql. If you installed XAMPP, you do not need to define a mySQL password for the root account, as XAMPP sets the empty password for root by default. Else please use the correct root password of mySQL. Pressing the "Submit" button will install everything you need to run your deltasql server.

If after installing deltasql, you receive Undefined index errors, please read through this FAQ question.

This movie shows the steps described above, too.

If something should go wrong, you can read also through the next section.

Manual install steps for the server

Go to the directory deltasql/conf, take example_config.inc.php, copy it to config.inc.php. Edit the variables in config.inc.php depending on your hardware and network configuration.

As next step, you should visit http://localhost/phpmyadmin, to access phpMyAdmin, an interface that speaks with the mySQL database. In phpMyAdmin, create a user delta_user with a password you can set in htdocs/deltasql/conf/config.inc.php. If the deltasql server is published on the Internet, you should also modify the constant $dns_name with the IP number of your computer or with its DNS name.

Now take deltasql/db/script.sql and go back to http://localhost/phpmyadmin. Execute the SQL script with the phpMyAdmin interface. It will create a deltasql database.

.

If you did all steps right, you should get a working deltasql Server as in this webpage if you visit http://localhost/deltasql

Log as Administrator with username admin and password testdbsync, so that you can create your own users. Do not forget to change the admin password shortly after with this form.

Install steps for the deltaclient (optional step)

Deltaclient is a multipurpose deltasql client for the Windows platform. It can copy to clipboard the query to retrieve the current schema version and the generated script as well. The user still needs to paste the queries and the generation script into his/her own SQL client (TOAD, SQL Developer, SQL Server Management Studio, etc.).

At startup, deltaclient will download all projects and branches which are currently available on Deltasql Server.

Based on the Project selection, deltaclient will show only branches which belong to the selected project. Similarly, when selecting the From: field, it will show only valid branches and tags into the To: field.

The version field should be filled with the result retrieved from the query which gets copyed when pressing the 'Copy SQL script to retrieve version number' button. Please note that the query works only on a prepared database.

The branches and tags checkboxes are useful to filter branches and tags from the From: and To: comboboxes. The special branch HEAD will always appear, even if the branches checkbox is unchecked.

To install deltasql, download first deltaclient.zip from the deltasql main page (at the bottom!). Unzip the file and start deltaclient.exe. When running the first time, click on 'Settings...' button and configure the communication settings. The proxy parameter is important, if you run deltaclient in an enterprise behind a proxy. If the proxy parameter is set incorrectly, deltasql will hang the next time is running. In such a case, launch restart.bat and correct the proxy parameter.

The url setting should point to your inhouse deltasql server. For testing purposes, you can leave the predefined "www.deltasql.org/deltasql" value, just to see how deltaclient behaves.

Instead of notepad, you can also define another editor to view SQL scripts such as Notepad++, Ultraedit or similar.

The deltaclient user parameter is not very important: it is used by deltasql server to collect statistics on how many times deltaclient was used to retrieve the synchronization script.

Install steps for the dbredactor client (optional step)

The dbredactor client is a deltasql client which can retrieve from the database the current schema version, and using this information it retrieves from deltasql server the synchronization script. Said script is shown to the user either in pretty printed HTML format or in text format on your editor of choice.

Download first dbredactor.zip from the deltasql main page (at the bottom!). Unzip the file in the Eclipse workspace. Add the build.xml into the Ant window by right clicking and choosing 'Add build files...'. Copy sample-build.properties to build.properties and set the correct URL for the deltasql server in the deltasql.server.url property. Create in the /config directory a configuration for the database schema as they are done under /config/examples. Next, update config.set with the path to the directory you created.


Picture: the two Ant targets of dbredactor

If everything is configured correctly, you'll be able to check if a schema update is needed with a double click on the ant target dbsync info and to get the script needed to update with dbsync update.

Install steps for the ant client into Eclipse (optional step)

The dbredactor client is a lightweight deltasql client like dbredactor. It integrates best into your build.xml script for the Eclipse/Java development environment.

Download first ant-client.zip from the deltasql main page (at the bottom!) and unzip it somewhere. This zip file contains only two tiny files: deltasql-build.xml and sample-deltasql.properties.


Picture: the target of the Ant Client

Copy deltasql-build.xml into the Eclipse project where you are working (at best in the root directory of the Eclipse project). Add the deltasql-build.xml file into the Ant window by right clicking and choosing 'Add build files...'. Copy sample-deltasql.properties to deltasql.properties. Read through deltasql properties and adjust all properties. Create a lib folder in the Eclipse project (if not already there) and copy all the .jars you can find in the clients\java\dbredactor\lib directory of this package (or only the ones you enabled in deltasql.properties, at your wish).

Et voilą, now you should have a working ant client. By pressing on "RetrieveUpdates" you will receive the script with the latest db updates. The ant client is also easy to integrate in an existing build.xml!

How to install the Google Gadget (optional step)


Picture: the Google Gadget of deltasql showing the latest submitted scripts

Purpose of the Google Gadget is to display the latest submitted scripts on your iGoogle start page.

To install the Google Gadget is fairly simple: just click on the following button: . A new page will open. Follow instructions there to get the deltasql gadget on your iGoogle start page.

After that, the Google Gadget still points to the deltasql server which is used as showcase for new users. With the arrow that points down on the top right of the deltasql gadget, you should choose the menu entry "Change settings", and enter an URL that points to your local deltasql install. Look at the screenshot:


Finally, remember to save your settings. If deleting cookies forces you to reenter each time a new URL, just modify the default_value string in deltasql/deltasql_google_gadget.xml.

For an explanation of what coloured rows in the gadget mean, see this F.A.Q. question.

How to install the bash client (optional)

Bash client 
performing database sync

The bash client allows in combination with deltasql server to setup a continouus database integration.

Download first the Bash client (bash_client.tar.gz) from the deltasql main page (at the bottom!) onto your favourite GNU/Linux server.

Unpack it with gunzip bash_client.tar.gz and tar -xf bash_client.tar. Make sure all shell scripts (also in subdirectories) have executable rights with chmod 775 *.sh. Open deltasql.conf, and configure each variable of the file.

You can test the connection to the deltasql server by running ./deltaclient.sh 1. To test the full cycle (retrieving schema version, contacting deltasql server and retrieving synchronization script, executing the synchronization script) please launch ./continouusintegration.sh.

Then with crontab -e register continouusintegration.sh as a cron job. From time to time, read the logfile sync.log to check that everything is running as expected. syncscript.log contains the concatenation of all scripts executed on the database schema.

Usage

User Management

There are four types of users: Guests, Developers, Project Managers and Administrators. Guests can only browse through project's scripts, search among them and compute synchronizations for schemas. Developers can do everything Guests can do, but they also can submit new scripts, and edit old ones. Project Managers can create projects and add modules. They can create branches and tags as well. Administrators are Project Managers which can add new users to the deltasql database. They manage permissions as well.

This movie shows how user management works, too.

Quick Guide

This movie should be a good introduction tutorial.

Login first with a user which has 'Project Manager' or 'Administrator' rights.

Define a module 'mymodule' with Create DB Module. Then define a project 'myproject' with Create a project. Under List projects it is possible to add the module 'mymodule' to the project 'myproject' with the link 'Add Module'.

Now choose 'Table' under List projects. Specify your database type. This will generate a table which needs to be created in all involved database schemas, you would like to keep under version control with deltasql. Create this table in these database schemas with a sql client.

Developers can now submit scripts with this form. They have to choose to which module the script belongs. If branches are created, they can also choose if a script is only a script for development schemas (a HEAD script), or if it needs to be applied to a previously created branch. They can list all previously submitted scripts. In this list, they can also modify a submitted script, if it contains errors, or if it is incomplete.

To update a database schema with the submitted scripts, you should first query the database schema with SELECT * FROM TBSYNCHRONIZE ORDER BY VERSIONNR DESC. Retrieve in the first row the project name (in PROJECTNAME column), its current version (in VERSIONNR) and its source branch value (in BRANCHNAME). The source branch value HEAD is used for development schemas. For production databases (typically installed to a customer), the source branch value can be the value set when a branch is created. Just look at the first row and disregard the other rows, they are kept so that you have a little bit history of what happened to that database schema (it can be helpful in case you run a script intended for another schema by mistake).

Once the three parameters are retrieved from the database schema, it is possible to ask for a database synchronization with the form Synchronize database schema. The field 'From:' should be filled with the value found in TBSYNCHRONIZE.BRANCHNAME. The field 'To:' (the so called target branch) should be decided as follows: choose HEAD if you would like to update a development schema (= 'From: HEAD') or if you would like to turn a production schema (= 'From:'= name of a created branch) into a development schema. Choose defined branch name for the 'To:' field, if you would like to update a production schema with scripts sent to the production branch, or if you would like to update an older development schema to a newer production branch.

Synchronize database schema will return a page with the missing SQL scripts for that given database schema. Choose 'Edit->Select All' from your browser and then 'Edit->Copy'. Paste the script into your favourite SQL client, which is connected to the database schema and execute it.

The script from Synchronize database schema will create at the end an UPDATE statement, which will update the information in TBSYNCHRONIZE for the next time.

Please note, that if something goes wrong, deltasql server reports an error according to the following list.

This is deltasql in short. Have fun!

How to define projects and modules

Modules are collections of scripts and projects are collections of modules. On the main page of deltasql, you can create a module and a project. Linking modules to projects is done on the List Projects page by using the Add module link.

At best you should model your projects and modules to follow your source code structure. If your source code is simple, you can start with one project containing one module. It is not enough to define the deltasql module alone, as the synchronization works only at project level.

Deltasql is known to work best if you use it in this configuration: assume your company sells software A to customer X and Y and software B to customer Z.

You should define three projects called Project_X, Project_Y and Project_Z as in the table below:

Project_XProject_YProject_Z
module_utilsmodule_utilsmodule_utils
module_Amodule_Amodule_B
module_Cust_Xmodule_Cust_Ymodule_Cust_Z

module_utils contains scripts which are of support of all applications. Typically, scripts which relate to tables for user management, groups and roles belong here.

module_A contains scripts which belong to software A. All scripts which belong to the core of sotware A belong here.

module_B contains scripts which belong to software B. All scripts which belong to the core of sotware B belong here.

module_Cust_X contains scripts which belong to software A, and are of use only for customer X. This is a customization module.

module_Cust_Y contains scripts which belong to software A, and are of use only for customer Y. This is a customization module.

module_Cust_Z contains scripts which belong to software B, and are of use only for customer Z. This is a customization module.

This is just an example on how deltasql works in some companies where their source code is modeled as in the table above.

Other ways to define a project might be to separate scripts which alter database structure and scripts which change content in tables.

Additionally, it is possible to define modules without linking them to a project, so that they can be used as collections for maintenance tasks.

On projects and modules, there is also a FAQ.

When should one branch?

Branching is something difficult and powerful. In the beginning, do not branch any project. Just submit all scripts to HEAD and learn how deltasql synchronizes in this scenario.

Later on, you can branch a project in the List Projects page, if you click on the Branch link (visible only if your user has at least Project manager permissions).

In general, you should branch a project at the same time when the source code is branched. You can then submit scripts both to HEAD and to the branch you created, only to HEAD or only to the new branch, according to how the source code HEAD and branch were modified.

If you master deltasql, you will then be able to transform a developer schema into a production schema or to dump an existing production schema and upgrade it to the latest HEAD for further development.

This movie explains how branching works.

When should one tag?

Tagging is easier than branching. Simply create a new tag in the List Branches and Tags page, when the software you are developing is packed into a new release. Just be careful to tag the correct branch: If the release is for development (or if you are not using branches at all) simply tag HEAD. If the release is on a production schema, you must tag the branch which is followed by the production schema.

This movie explains how tagging works.

How synchronization works

Synchronization can get difficult if branches are involved. To warm up, imagine the following scenario:

Scenario 1: all scripts are submitted to HEAD

   Submit script 1 to HEAD to deltasql server
   Submit script 2 to HEAD to deltasql server
   Submit script 3 to HEAD to deltasql server
   Submit script 4 to HEAD to deltasql server
   Submit script 5 to HEAD to deltasql server
   Submit script 6 to HEAD to deltasql server

Example 1.1

Imagine we have an old database schema used for development, if we launch the query to retrieve the database version, it returns 3 for the version, and HEAD for branchname. The fact that the field branchname is set to HEAD indicates that the old database is a development database.

Please make sure you know the difference between a production and development database in deltasql before continuing to read.

If we ask deltasql to synchronize with the form where we enter version=3 and branchname=HEAD, deltasql will generate a synchronization script as follows:

   Synchronization scripts contains scripts 4, 5, 6 and updates version of old database schema to 6

This example is very easy to understand. If your project is simple and if you do not need to switch between development and production environments, then you do not need to branch.

Scenario 2: scripts are submitted to HEAD and/or BRANCH_1

Here, we have a more complex scenario. A branch is created, and some scripts belong to the branch, or to the branch and the HEAD

   Submit script 1 to HEAD
   Submit script 2 to HEAD
   Create new branch BRANCH_1
   Submit script 3 to HEAD
   Submit script 4 to BRANCH_1
   Submit script 5 to HEAD and BRANCH_1
   Submit script 6 to HEAD

Example 2.1

   Version in old database schema: 1 
   Branch name in old database schema: HEAD 
   Update to: HEAD in synchronization form

This example is very similar to example 1.1. The old database is still a development database. Only the branch complicates it a little bit.

   Synchronization scripts contains scripts 2, 3, 5, 6 and updates version of old database schema to 6

Script 4 is missing because it belongs to the branch only.

Example 2.2

   Version in old database schema: 2 
   Branch name in old database schema: BRANCH_1 
   Update to: BRANCH_1 in synchronization form

This time, the old database is a production database, as branchname is set to BRANCH_1. We just would like deltasql to update the database and to keep it in production. Therefore, we say in the synchronization form to update to BRANCH_1.

   Synchronization scripts contains scripts 4, 5 and updates version of old database schema to 6

The synchronization mechanism considered only scripts which were flagged with BRANCH_1.

Example 2.3

   Version in old database schema: 2 
   Branch name in old database schema: BRANCH_1 
   Update to: HEAD in synchronization form

The old database is a production database. We would like deltasql to transform it into a development database. Therefore we set Update to: HEAD into the synchronization form.

    Synchronization scripts contains scripts 3, 4, 5, 6 and updates version of old database schema to 6
	Additionally the synchronization script updates branchname to HEAD

The synchronization script executed all missing scripts to create a development schema and also changed the definition of the database from BRANCH_1 to HEAD, making it effectively a development schema.

This movie is an introduction tutorial on how synchronization works.

deltasql Concepts

Developers submit scripts which belong to a module. A project consists of one or several modules. A database schema is always related to a single project. To each script a unique version number is assigned. If a database shows a given version in TBSYNCHRONIZE, deltasql can compute which scripts are necessary to reach another version.

Normally, database schemas and in particular development ones, need the entire chain of module-related scripts to be updated. This chain, as in the everywhere known Control Version System (cvs), is called HEAD. However, deltasql allows to create and name branches for a project. Once a branch is created, developers can choose if a particular scripts belongs only to HEAD, or if it belongs also to the newly created branch. Internally, deltasql simply stores the version number when the branch was created.

When updating with the form, one has to choose the target branch. The target branch can be HEAD, if all scripts need to be executed, and therefore if the database schema has to get a development one. Specifying a new target branch which is not HEAD will update the database schema to the version number assigned to this branch (by inserting a new row in TBSYNCHRONIZE with the version number), no matter if the source branch is the same branch, or if it is a previous one. In principle, it is also possible to update a long forgotten development schema (source branch = HEAD), to another target branch.

Tips

Define script's collections for maintenance tasks

With deltasql it is also possible to keep a collection of scripts which are occasionally used for maintenance tasks. It is sufficient to create a database module with a name like 'collection of scripts for myProject' without adding the module to any project. Modules which are not added to a project do not partecipate in the synchronization process. So you can submit maintenace scripts to this module without fear that they will appear in the synchronization sequence. Typically, you will change the default title db update to what the script effectively does. deltasql underlines titles which are not default and allows to search also in the title field of a script.

By using the function Search among scripts, you can then specifically select the module 'collection of scripts for myProject' to search among the maintenance SQL scripts.

Filter script - output particular synchronization scripts

When listing scripts (in List Scripts), it is possible to filter the scripts (e.g. by date, module, author and more) by using the command Search among scripts on top of the page. Normally, the scripts are filtered and shown as a list. By checking the checkbox Output as text list before clicking on Submit, the scripts will be shown as a normal text file that can be copy pasted into your SQL client of choice. This can be sometimes useful, if you need to repair a database schema by applying some subset of the recorded scripts.

There are also users that like this feature as substitution to the database synchronization form.

Advanced Topics

Insights into the deltasql Algorithm


Picture: Deltasql timeline with source and target

If you install deltasql with the option Test System (with Test data), you can verify the deltasql correctness with this file.

How to write your own deltasql client

deltasql Server is written in PHP and can run whenever Apache runs. You might want to write your own deltasql client that integrates better into your own development environment. There are already several client available. They can be downloaded at the bottom of the main page.

A deltasql client should perform the following operations:

Client URL parameters

These are all possible parameters which can be passed by a client to the dbsync_automated_update.php script. String values need to be URL encoded.

XML examples as retrieved from clients

deltasql Server answers with two XML types, either an Error message or a synchronization script, if the option xmlformatted=1 is set. Click on the links below to see examples

Synchronization Script XML example ...
Error Message XML example...

List of error codes for deltasql server

The entire list is here.

Directory structure of the deltasql_1.x.y package

The structure itself is described in this text file.

Source code walkthrogh

While most of the .php files deal with creating, inserting, updating or deleting data, there are some units which lie outside this schema and that are worth a look:

Feedback on this document

If you need help in setting up your own deltasql server, you can contact us at the deltasql mailing list. We appreciate your feedback.

Have fun!


Back to main page