One of the caveats of having your SQL database under a source control system, is the overhead when the time comes to deploy a new database build. Even if a single copy of the database isn’t shared among the developers, but rather each developer has its own local copy of the database objects’ scripts which are synchronized with source control on a regular basis, building a deployment SQL script may prove to be a rather challenging task.
Namely, simply executing the database object’s scripts without taking into account objects interrelationships will result in the:
Msg 208, Level 16, State 1
error message and a failed deployment. To fix this issue, the contents of the deployment SQL script need to be reviewed and the individual object scripts need to be rearranged within the deployment script. Doing this manually can be a long and tedious process, not to mention error prone, even if the database that is to be rolled out contains less than 50 objects and is borderline impossible to get right the first time if dependencies of hundreds or thousands of objects need to be checked. The good news is, there is a better way to prevent dependency error on database deployment than by checking them manually.
When building a database deployment script from source control:
- Make sure to have the latest versions of the scripts for all of the objects that are to be deployed
- Organize the build script in the following order:
- Start with user defined types, if any, and follow up with
- User defined functions
- Stored procedures
- Indexes, etc.
However, although the guidelines mentioned above are a good rule of the thumb, they don’t address scenarios when objects of the same type depend on one another (e.g. a view which depends on a view). In those cases, objects of the same type still need to be ordered manually.
Another solution is to use a 3rd party tool that creates deployment scripts from source control.
ApexSQL Build is a SQL database deployment tool that builds new databases or updates existing ones by packaging SQL scripts, script folders, database snapshots and even scripts under source control into a single deployment SQL script, a C# solution or an executable installer. It allows specifying the objects to be created or updated, ensures error-free deployment and more.
To generate a deployment script without any dependency problems:
Start ApexSQL Build
Click the Build button under the Database section in the Home tab:
Select the Database option as an output type:
Select Source control as the input source and click the Setup button:
The Source control wizard will be opened and in the first step a source control system should be chosen:
- Enter the login information and source control system parameters:
Learn more about setting up the Source control wizard from here.
Click the Finish button
ApexSQL Build will retrieve all files from the source control system to a local working folder:
Click the Next button
Specify the SQL Server where a database will be created along with the authentication type:
In the Database options step, provide a database name that will be built from the specified source control repository. To copy the database properties from the database on the source control repository, click the Get database properties from input source button:
Once everything is set, click the Next button
The list of the objects that will be created is shown in the Build objects step. By default, all objects are checked, but if some objects are not needed, uncheck them and they won’t be included in the building process:
Once the Next button is clicked, the list of actions that will be executed is shown in the Summaries tab, under the Preview step:
Switch to the Dependencies tab and any dependent objects will be automatically added to ensure the error free build process:
In the next step, add Post deployment scripts if there is something that should be executed upon the building process is finished. In this case, two tables, a view, and a procedure will be created after the database is built:
In the last step choose whether to save a copy of a build script locally or not:
Before the Finish button is clicked, click the Save button from the Home tab, to create a project file that can be used later for automating the build process:
Click the Finish button
Once the build process is finished, the Results window will be shown:
Automating the build process from the source control repository with ApexSQL Build CLI
The previously described process can be also run unattended, by creating a project file at the end of the process and using it in the ApexSQL Build’s CLI.
The saved project file can be called from the CLI, along with the optional switches that can provide additional information of the build process:
|“ApexSQLBuild.com”||# application path|
|/pr:”MyProject.axbd”||# project file path|
|/v||# prints all messages in the console|
|/f||# overwrites existing script files|
|/out:”Output.txt”||# redirects console output to a specified file|
|@echo ApexSQL Build return code is %errorlevel% >> /out:”Output.txt”||# return code upon finished building process|
This process can be also run with an option to show the prompt window (attended) for database/source control credentials. Learn more about it from article on this link.
The CLI switches can be saved as a batch file or to create a PowerShell script. In either way, this job can be scheduled in four ways of scheduling ApexSQL tools.
Creating a database from a specific version of DDL scripts stored in a source control system doesn’t have to be a problem. ApexSQL Build generates a script that creates all objects successfully and eliminates all problems with missing dependencies.
April 4, 2013