SQL Compare command line and /migrationfolder switch
robermar2
Posts: 7 Bronze 1
My company just purchased a large set of licenses for sql source control and sql compare for our automated build agents that use team city and svn.
We are attempting to include our databases in our continuous integration environment using sql red gate tools.
We have been fairly successful in setting this up, but have come across an issue when comparing databases that only a migration script can solve.
We have successfully committed migration scripts through sql source control and they are part of our svn repository. We are using team city to check out the database script folders as well as the migration scripts.
We have 10.1 of sql compare installed and our attempting to use the /migrationfolder switch.
We immediately receive this error:
Error: The /migrationfolder switch cannot be used without the /scriptsfolderxml
switch. Please use SQLCompare.exe /? or SQLCompare.exe /verbose /? for more
information.
The /? and /verbose /? switches do not offer any additional help/guidance at all.
Your online documentation does not offer any either on this particular feature.
I see documentation on using migration scripts using the sql compare user interface, but that is not what we are attempting to do...we are attempting to automate this process using the command line.
Any help on this would be greatly appreciated as I don't understand why the scriptsfolderxml switch has anything to do with the migrationfolder switch, much less what I would need to put in the xml file to allow this to work.
If the migrationfolder switch is not fully supported, a workaround would be greatly appreciated. Our database change requires us to drop a table, recreate it with an additional column and insert default data into it upon creation. The table exists in the target database and has data in it and a column needs to be added that does not allow nulls and does not have default data specified...we need to populate it with specific data and migration scripts have allowed us to do that in our development environment, but we need to automate these changes to our QA environment. Thanks.
We are attempting to include our databases in our continuous integration environment using sql red gate tools.
We have been fairly successful in setting this up, but have come across an issue when comparing databases that only a migration script can solve.
We have successfully committed migration scripts through sql source control and they are part of our svn repository. We are using team city to check out the database script folders as well as the migration scripts.
We have 10.1 of sql compare installed and our attempting to use the /migrationfolder switch.
We immediately receive this error:
Error: The /migrationfolder switch cannot be used without the /scriptsfolderxml
switch. Please use SQLCompare.exe /? or SQLCompare.exe /verbose /? for more
information.
The /? and /verbose /? switches do not offer any additional help/guidance at all.
Your online documentation does not offer any either on this particular feature.
I see documentation on using migration scripts using the sql compare user interface, but that is not what we are attempting to do...we are attempting to automate this process using the command line.
Any help on this would be greatly appreciated as I don't understand why the scriptsfolderxml switch has anything to do with the migrationfolder switch, much less what I would need to put in the xml file to allow this to work.
If the migrationfolder switch is not fully supported, a workaround would be greatly appreciated. Our database change requires us to drop a table, recreate it with an additional column and insert default data into it upon creation. The table exists in the target database and has data in it and a column needs to be added that does not allow nulls and does not have default data specified...we need to populate it with specific data and migration scripts have allowed us to do that in our development environment, but we need to automate these changes to our QA environment. Thanks.
Comments
/versionusername1 and /versionpassword1 are the credentials required to connect to your source control system. You use /migrationfolder if you are adding your own custom migration scripts to source control and wish for these to be pulled into your deployment scripts.
The <repo_location_xmlfile> is a text file that you must create to accompany the command line. It contains the information required to connect to the source control repository. Create a new text file and paste the contents of the SQLSourceControl Scripts Location extended property of your source controlled database. To find this, right click on the database in the Object Explorer, select Properties, Extended Properties page, and copy the Value text from the SQLSourceControl Scripts Location property to your text file. It should look something like this:
We're aware of the lack of documention and are currently working to resolve this.
I will email you the complete document which I copied the above extract from.
David Atkinson
Product Manager
Red Gate
Product Manager
Redgate Software
I also am wondering if maybe the scripts are being excluded to a perceived wrong db version .. I don't know how this is stored. Maybe db extended properties? I have a new column this exists in svn and a migration script. Staging server does not have the column. I want my command line SQL compare to grab the script and use it, but I don't see how.
David
Product Manager
Redgate Software
Created a project in the SQL Compare UI, specifying source control as the source (not using scripts), mirroring exactly what I was doing in the command line. Created a deployment script. Saved the project. Opened the project file (in '[documents]\SQL Compare\SharedProjects') in a text editor. Copied the contents of the 'ScriptFolderLocation' and 'MigrationsFolderLocation' elements to separate XML files, replacing '<' and '>' with '<' and '>' respectively and saving in UCS-2 Little Endian (using Notepad++) - this is required due to the UTF-16 in the header. Specified the above XML files as the '/scriptsfolderxml' and '/migrationfolderxml' parameters, along with '/sourcecontrol1' parameter (no value despite what command line help says), '/revision1:Latest' and all my other usual parameters (except '/scripts1' of course).
And it worked (migration script finally included). I'm not sure if registering the source control in SQL Compare (as part of creating the project), actually running the comparison, creating the deployment script, or getting those two XML files exactly right is what fixed it, but I no longer get an 'object reference' error (due to one or more of these).[/list]Ah ha. No, it's not the case. We are testing this as part of a new process. The databases downstream from dev (staging, prod) have never had any changes rolled out from these tools yet, so they don't have these extended properties. How do they get into the database the first time?
Product Manager
Redgate Software
script folder -> database
using the checkout folder on the build server. I used the new options in 10.1 to change this to
svn server -> database
This took a while as I had to install SQL Source Control on the build server, which meant I had to installed SQL Management Studio on the build server, etc. Currently What I have going includes a custom c# console app that wraps calls to SQL Compare and SQL Data Compare. Basically:
1) MSBuild file on build server has a list of databases to compare (17 for us).
2) MSBuild file calls our custom CompareDatabases console app once per database.
3) That app calls SQL Compare and SQL Data Compare. For SQL Compare it dynamically generates the XML config file mentioned above. For Data Compare it takes a stored list of tables to compare (static data) and dynamically builds an XML config file that specifies a destination server and either /sync or a script file name. The script file name comes from a build label set in the build server (happens to be TeamCtiy).
4) The build has a step to check in the generated SQL scripts, if applicable, for a DBA to review before we run them in staging/prod.
Thanks to everyone for the help. I'm happy to share details of how I did all this.
Having to install SQL Source Control and SSMS is tedious and in fact there is a (hacky) way of avoiding this which I can share with you should it help. The longer term plan is for us to provide a single installer that installs all the command lines and resources required for them to work, so you won't have to install the UI components separately.
Would you be able to share the contents of your xml file. I'm not 100% sure why this is required.
Database continuous integration is very much in its infancy so you may consider yourself a pioneer!
I've recently started blogging about SQL development and plan to focus primarily on continuous integration.
http://geekswithblogs.net/SQLDev/Default.aspx
David
Product Manager
Redgate Software
The latest problem is that when comparing svn (HEAD revision) to a database, SQL Compare is trying to apply all historical changes instead of just those that differ between svn and the target database. This fails due to objects no longer existing, etc.
Command line is like this:
sqlcompare /exclude:role /exclude:user /exclude:a
ssembly /sourcecontrol1 /Server2:MYSERVER /Database2:MyDb /sync /migrationfolder:d:\code\trunk\database\migrationscripts\MyDb /scriptsfolderxml:d:
\code\trunk\Build\Shared\config\SQLCompareSvnConfig_MyDb_temp.xml /revision1
:HEAD /versionusername1:builduser /versionpassword1:mypass /verbose
There are just 3 objects that differ, but the sync is trying to run through earlier changes. If I change to script generation mode, it generates a ton of older stuff. It seemed like this was working, so I'm mystified. The target db shows svn revision 5018 in its extended properties, and the HEAD revision in svn is 5034. But changes scripted go way back before that.
David
Product Manager
Redgate Software
1) I choose to compare subversion HEAD to a shared database server.
2) The comparison results screen correctly shows objects that changed since the last sync with that server. In my case subversion is at 5034 and the dev SQL server is at 5018. The database extended property on the SQL server shows 5018.
3) But ... once I click Deployment Wizard, the "Review Migration Scripts" page shows pre-selected migration scripts from earlier revision. Also, the little circle-graph at the bottom says "The deployment script will be created as follows: 0 --> 4673 --> 4783 --> 5018 --> 5034" and if I click Next, it generates a script with all the old, unneeded changes.
If I uncheck the older migration scripts, it still scripts back to rev 0, just without the scripts.
It seem obvious that it's not "getting" that the target server is on rev 5018. There are no RedGate tools on the target SQL Server, but I'm not aware that they need to be there.
Any clues are appreciated ... I ma y be missing something obvious.
John
Can someone else confirm this? And if it's true, can someone explain why it's necessary? It's not at all clear to me why this would be the case, considering that everything in the scripts folder has already come from source control.
What is the exact form of this extended property? I think I perhaps need to prepopulate it on the production database so that I can start performing migrations.
Thanks!
David
Product Manager
Redgate Software
Also, the solutions suggested here for enabling command-line support for the migration scripts are very clever but seem to require a considerable amount of hoop-jumping. Are these the best way to approach the problem, or do you know of any tutorials or blog posts that demonstrate how this kind of thing can/should be done?
In practice it seems that it's not straightforward to troubleshoot when it goes wrong. We'll have to improve that.
The extended property gets set in SQL Compare's deployment script. If you generate a script where the source database is a source controlled database, you'll see some SQL statements at the bottom of the script that sets the extended properties. If you database hasn't had a SQL Compare script run against it recently (since we implemented the feature), you won't have the extended property. The solution is to add it manually by copying it from a source controlled database to the target.
David
Product Manager
Redgate Software
Ok, I think this piece is key. Do you draw a distinction here between a scripts folder that was generated from a source controlled database, and pointing to the URL of the repository itself?
I see the extended property in my local source-controlled DB -- thanks for that tip. What does SQL Compare do with that 'SQLSourceControl Scripts Location' value?
Can you try again using 'Direct from source control' rather than 'scripts folder' as the source?
It needs the scripts location as it builds up the deployment script from the revisions in source control. For example, if you go from revision 1 to revision 10 and there is a migration script from 5 to 6 it will:
Fetch revision 1 and revision 5 from source control and generate the diff script. Then it does the same for 5 -> 6 and 6->10, and attaches them together.
Product Manager
Redgate Software
Local:
<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<ISOCCompareLocation version="1" type="SvnLocation">
<RepositoryUrl>http://tco-dev-scm.xxx.internal/svn/code/trunk/Database/MigrationScripts/Intranet/</RepositoryUrl>
</ISOCCompareLocation>
Dev:
<?xml version="1.0" encoding="utf-16" standalone="yes"?> <ISOCCompareLocation version="1" type="SvnLocation"> <RepositoryUrl>http://tco-dev-scm.xxx.internal/svn/code/trunk/Database/MigrationScripts/Intranet/</RepositoryUrl> </ISOCCompareLocation>
They seem the same. Is it possible the way I call the command line, or the exclude filters, is causing a problem? Here are the command line params:
/exclude:role /exclude:user /exclude:assembly /sourcecontrol1 /Server2:TCO-DEV-SQL-02 /Database2:Intranet /sync /migrationfolder:C:\TeamCity\buildAgent\work\946b3977434c5179\database\migrationscripts\Intranet /scriptsfolderxml:C:\TeamCity\buildAgent\work\946b3977434c5179\Build\Shared\config\SQLCompareSvnConfig_Intranet_temp.xml /revision1:HEAD /versionusername1:builduser /versionpassword1:xxx
Thanks,
John
I'm thinking that we should ideally expose the repo using a command line switch rather than relying on the xml file. I take it that the xml file isn't ideal for you?
Product Manager
Redgate Software
Can you experimentally try putting an explicity revision after /revision1: instead of HEAD to see if that's causing issues?
Product Manager
Redgate Software
I've been using SQL Compare and SQL Data Compare to do our continuous integration for a little over a year now, and we too have been finding an increasing need for migration scripts so I'd love to get them working.
I tried following the steps outlined in this thread, and I'm getting an error saying "Using source controlled databases requires SQL Source Control to be installed." I'd be interested in hearing the "hacky" way of running this without SQL Source Control since I'd prefer not to install that and SSMS on our build server.
I'd like to cast a vote for the command line feature. We use the command line for all the other parameters so that would work nicely for us. (The XML file is workable, though slightly less convenient.)
David
Product Manager
Redgate Software
Our staging environment is the last step before production and deployments are done a little differently. First we baseline it to match Production (by deploying Trunk; we keep trunk in sync with production) and then we deploy the entire release branch to the staging environment. Sometimes if a deployment fails or we find bugs, we need to redeploy, which involves first deploying Trunk and then deploying the branch again. I'm curious if the migration scripts will get deployed again in this scenario (we would like them to be).
Update: I tested the above scenario, and it seems that the migration scripts do not get run the second time. I could probably get around this by changing the property so it reflects the first revision of the branch before we do the sync.
For now, just add the extended property as an extra step and see if it works.
David
Product Manager
Redgate Software
When used like this SQLCompare.exe generates a drop of the migrationscript extended property. After the first synch or execution of the generated script, every following run of SQLCompare will ignore migration scripts and fail unless you re-add the extended property manually.
This surely looks as a BUG, and its working like this in the last version of SqlCompare & Automation Pack! :shock: