In order to version static data, just like database objects e.g. Tables, a mechanism needs to be created to identify only those tables designated as “static” (see below) and to come up with a reproducible process, that will Commit the data in these tables to source control. This article covers this process
Static data is non-transactional data that does not change over time, or even if there are changes, they are not as frequent as for other categories data. Examples of static data include names of cities, zip codes and propriety codes used internally in a company.
ApexSQL Data Diff is a 3rd party tool for comparing and synchronizing SQL database data between various data sources. It compares data between live databases, backups as well as between source control projects/labels and script files. ApexSQL Data Diff natively supports all popular source control systems such as Git, Team Foundation Server, Subversion, Mercurial and Perforce.
For the purpose of this article, we’ll use a Subversion repository where an existing database schema is already committed. Pubs database will be used as an example. The goal is to commit the static data to the repository so it can be versioned as well.
In order to commit the static data of the Pubs database to source control, the following steps should be performed:
- Start ApexSQL Data Diff
In the New project form, select the Database option from the Source drop down list and point to the Pubs database
- As a destination, select the Source control option from the drop down list and click the Edit button in order to configure source control connection. In the source control setup wizard choose the Subversion option from the Source control system drop down list:
- In the next step, provide source control credentials, repository path and eventually a folder inside the repository where the static data will be committed:
- After the repository is set, click the Finish button to close the source control wizard. Before initiating the comparison process, tables and even columns can be filtered so only tables and columns that contain static data can be included. In order to filter tables, expand the Advanced tab, and choose the Object filter tab. Tables that contain static data can be checked (1), and for each table, columns that do not contain static data can be unchecked (2):
- Once the tables and columns containing the static data are checked, clicking the Compare button initiates the comparison process
- In case of the initial commit of the static data, all selected tables will be shown as missing in the ApexSQL Data Diff main grid:
- At this point, specific records (rows) from any table can be filtered out if needed, just by unchecking the box for the appropriate row:
When all tables with static data are set, a project file can be saved, in order to be used later in the automation process. To save the project file, use the Save/Save as buttons from the main ribbon:
Once everything is set, we can synchronize static data in a SQL Server database with the source control repository. To initiate the process, click the Synchronize button from the main ribbon:
- This initiates the Synchronization wizard. In the Output options tab, select the Update files in source control option, from the Action dropdown list:
When the process is finished, the Results dialog shows what is committed to the repository:
- Checking the Subversion repository, we can verify that the selected static data has been successfully committed. In order to verify this, navigate to the Tables folder for the selected database:
Opening any of the SQL files (for example dbo.stores.sql) gives the INSERT script which represents the way static data is stored on the repository:
INSERT [dbo].[stores] ([stor_id], [stor_name], [stor_address], [city], [state], [zip])
VALUES (N'6380', N'Eric the Read Books', N'788 Catamaugus Ave.', N'Seattle', N'WA', N'98056') INSERT [dbo].[stores] ([stor_id], [stor_name], [stor_address], [city], [state], [zip])
VALUES (N'7066', N'Barnum''s', N'567 Pasadena Ave.', N'Tustin', N'CA', N'92789') INSERT [dbo].[stores] ([stor_id], [stor_name], [stor_address], [city], [state], [zip])
VALUES (N'7067', N'News & Brews', N'577 First St.', N'Los Gatos', N'CA', N'96745') INSERT [dbo].[stores] ([stor_id], [stor_name], [stor_address], [city], [state], [zip])
VALUES (N'7131', N'Doc-U-Mat: Quality Laundry and Books', N'24-A Avogadro Way', N'Remulade', N'WA', N'98014') INSERT [dbo].[stores] ([stor_id], [stor_name], [stor_address], [city], [state], [zip])
VALUES (N'7896', N'Fricative Bookshop', N'89 Madison St.', N'Fremont', N'CA', N'90019') INSERT [dbo].[stores] ([stor_id], [stor_name], [stor_address], [city], [state], [zip])
VALUES (N'8042', N'Bookbeat', N'679 Carson St.', N'Portland', N'OR', N'89076')
Using the described workflow, static data can be initially committed to the repository.
Let’s create a PowerShell script that contains ApexSQL Data Diff CLI switches in order to automate the process of committing static data. In addition, the script contains data stamped output summary:
|$appLocation = “ApexSQLDataDiff”||Specifies variable for storing the path to the CLI executable file of ApexSQL Data Diff|
|$timeStamp = (Get-Date -Format “MMddyyyy_HHmmss”)||Specifies variable for storing the time stamp value used in the output file name|
|$appCLIcommand = “/pr:””Pubs_StaticDataCommit.axdd”” /sync /v /f /out:””Output_$timeStamp.txt”””||Variable for the CLI command to call the project file and generate the date stamped output file containing the application execution summary|
|(Invoke-Expression (“& `”” + $appLocation +”`” ” +$appCLICommand))||Call the specified CLI executable including the additional switches|
|“ApexSQL Data Diff return code is $LASTEXITCODE” >> “Output_$timeStamp.txt”||Including the return code in the time stamped summary|
After this is set, save the PowerShell script as CommitStaticData.ps1
In the abovementioned PowerShell script, previously saved project file includes encrypted password for the used Subversion repository, as well as for the SQL Server connection. In case there is a need to handle credentials in a different way, here are 4 ways of handling database/login credentials during automated execution via the CLI
In order to schedule the saved script, various methods can be used, such as Windows task scheduler, SQL Agent Job, or a PowerShell project. All of these are explained in the Four ways of scheduling ApexSQL tools.
Once the script is executed, it will create the summary containing the return code at the end of the output file:
----------------------------------------------------------------------- Name Schema Updated Added Deleted ----------------------------------------------------------------------- pub_info dbo 0 8 0 stores dbo 0 6 0 Total: 0 14 0 ----------------------------------------------------------------------- ApexSQL Data Diff return code is 0
To see a full list of return codes and a description for each one of them, check the Common return error code section part of the General usage and common CLI switches article.
December 14, 2016