Continuous Deployments for SQL Server – Part 4

Ok so we have seen in parts 1, 2, and 3 how to go about adding your database to source control, as well as comparing the schema and the data held within our SQL Server databases.

Its time to go about releasing changes made from one database to another (again think of you deploying changes from UAT to Production). There are several ways to go about releasing the changes, here are two of them:-

  • Script the schema and data changes as 2 separate scripts, which you can easily combine yourself.
  • Use Redgate SQL Packager and even create an .exe to run which will allow us to update the database.

Ok so let me demo how to go about using option two using Redgate SQL Packager which comes with the Redgate SQL Toolbelt.

Start up SQL Packager and you’ll see this start-up screen:-

packager1

I want to package an upgrade to a database so I have selected that option already, the next screen shows this:-
packager2

Above I have chosen my Database server and the database I want to use as the source and the target database (one we want to update).

Below shows the database objects I wish to package and apply on the target database.

packager3

Below shows the tables whose data I want to package and apply to the new database.

packager4

Below shows me the script that has been generated for me, first tab is the schema script, second tab is the data script and the third tab is for any warnings.

packager5

And the last screen gives us a choice to either package the change as a .exe, package it as a c# project, launch the script in SQL Query Analyser or Save the script for further inspection.

packager6

Choose option 1, run the exe and your database updates are complete, that’s all there is to it, any issues found the changes will be rolled back as they are transactional, leaving your database in tact.

Once complete, just run SQL Compare and SQL Data Compare and you can verify all is good – and viola, you’ve just updated production with schema and database changes, and you’ve been given a few different ways to do using RedGate SQL Toolbelt.

Continuous Deployments for SQL Server – Part 3

In part 2, I covered how to compare the schemas of two databases, in part 3 I’ll to cover how to compare the data held within these two databases, for that I am going to use SQL Data Compare.

Again we will use the same two databases, imagine your comparing UAT to Production and wish to compare the data within these two databases:-

sqldatacompare1

sqldatacompare2

(If the links are too small click on the image and then when the new window appears click on Original size at 1896 × 316)

The above image shows us that the t_depot database table has 4 rows which are in the Stock_DB_Deisgn table which aren’t within the STOCK_DB_DESIGN_ORIGINAL database. Again same as with SQL Compare, if we now click on Synchronization wizard we will see the following screen:-

sqldatacompare3

And again as with SQL Compare, we get 2 options:-

  • Create a deployment script – use this option if you want to script the changes and review before running in the changes.
  • Synchronize Using SQL Data Compare – use this option if you want the tool to make the changes for you, you also get the option after its ran to automatically compare the 2 database data after the changes, to verify.

And that is it, easy, simple and straightforward, no manual steps involving creating scripts, no real chance for it to go wrong, plus once again you can source control the script if you really wanted to or share it with colleagues.

In part 4 I’ll cover how to deploy changes to your database whether its schema changes, data changes or both.

Continuous Deployments for SQL Server – Part 2

If you’re using SQL server at work and manually deploying changes to your database using manually crafted scripts then its time to stop, there is a better way to do this by automating it, remove the chance of human error, this will also ensure that before you deploy to production that your changes will work, guaranteed.

In this blog post, I’ll discuss comparing two SQL Server databases which you can use SQL compare to funnily enough compare them and see the differences between the two, very quickly and reliably, imagine you wanted to compare your local Development database with say your Staging database for example, or compare UAT to Production.

In this blog post I’ll go over the steps I went through and how to use Sql Compare to work out what’s changed between the two databases, schema wise, then in the second part I will show you how to use SQL Data Compare. to compare the data within both databases and in part 3 I will either create a script to update the database or run an exe which the tools will create for us to make both databases the exact same, both in schema and in data, so let’s get started.

I’ve made dome schema changes to STOCK_DB_DESIGN and I wish to see whats changed, so lets see how to compare the 2 databases using SQL Compare (I’m using version 11).
sqlcompare4

In the screen shot above I’ve chosen my databases to compare and the tool will now run and show me what the schema differences between the two. the screen shot below shows the results after the tool has been ran against both databases.

sqlcompare5

On the left pane we see the changed database, on the right the database without these changes, I clicked on the first row to show the changes in the t_depot table and in the split at the bottom it shows the differences per object – very quick and easy to see what’s changed.

Update time
In order to update the older database with the new changes, we simply click on Deployment Wizard at the top and we get the following screen with options:-

sqlcompare6

Here we get 2 options:-

  • Create a deployment script – use this option if you want to script the changes and review before running in the changes.
  • Deploy Using SQL Compare – use this option if you want the tool to make the changes for you, you also get the option after its ran to automatically compare the 2 database after the changes to verify.

And that is it, easy, simple and straightforward, no manual steps involving creating scripts, no real chance for it to go wrong, plus you can source control the script if you really wanted to or share it with colleagues.

In part 3 I’ll cover comparing the 2 databases when it comes to data, for this we will use SQL Data Compare.

Continuous Deployments for SQL Server – Part 1

Okay so your using a SQL Server database at work but you haven’t yet put the database into Source Control, here are some reasons as to why you want to do this:-

Source control your database
Trust me your going to want to do this if you haven’t already, it’s a good practice, it may save your skin some day, yes you can take backups which again is a good practice, here is an excellent article on why you want to do this:- Why put your database into source control?.

Okay now that we wish to put our database into source control – we still use Subversion at work so I downloaded Subversion and installed it locally as well as TortoiseSVN.
I have 2 databases called STOCK_DB_DESIGN and STOCK_DB_DESIGN_ORIGINAL within SQL Sever, the former had a few schema changes, imagine this is Development(STOCK_DB_DESIGN) and Staging(STOCK_DB_DESIGN_ORIGINAL).

I created a local repository within Subversion and downloaded RedGate’s Database Lifecycle Management Products.

This tool comes with SQL Compare and SQL Data compare as well as a host of other awesome SQL Server tools which you can find out about at the above link.

For this post I’ll be using Redgate’s SQL Source Control which adds a tab to SQL Server Management Studio and looks something like this:-

sqlsource1

So lets go ahead and add a database to our source control using this tool, Select the option above, and then Next

sqlsource2

I selected Subversion because at work we still use it, but you can use Git also and others, click Next

sqlsource3

I then give it a repository URL, I already created a repository within Subversion for my database and added the URL like above, click Next and that’s it, our database is now linked to source control. Now to get the database scripts into Subversion we need to right click on the Database in question within SQL Server Management studio like so and select Commit Changes to source control:-

sqlsource5

This will then add all of the database objects to SQL Source Control, once that’ completed, go into windows explorer and I use TortoiseSVN I can right click within my local SVN repository folder and then select Checkout or get latest version and this will result in the following folders being populated:-

sqlsource4

And when I go into say the tables folder I’ll see the following:-

sqlsource6

And that’s all there is to source controlling your SQL Server database. If I make change to an object say a table I’ll see whats changed locally in SQL Management Studio and I’ll know I still need to commit the change as seen below:-

sqlsource7

In Part 2 I’ll show you how to compare schemas on both databases to work out what changes are required, and how to script them using the tool.

Continous Deployment – Free Code Coverage tools Part 1

    OpenCover

If you’re looking for some free code coverage tools then the good news is look no further than OpenCover.

OpenCover can be run from the command line, which also means you can run it as part of your continuous deployment all for free, I like free.

If you’d like to get code coverage built into Visual Studio then look no further than the extension called OpenCover UI, this will give you code coverage results and show you which lines of code are covered by your tests, giving you no excuse to not test as much of your code where possible.

I created a very simple Calculator class library project and then add a class library for testing my calculator class, I’ll spare you showing the simple code, but here is a screen shot of me having ran the tests using OpenCover within Visual Studio.

coverage

The Above screen shows the lines in the class which are covered by tests in green, red are lines uncovered by any tests, below that is a break down showing the percentages of code covered and on the right is the test explorer showing the list of tests available.

If you want to run OpenCover within Visual Studio then add it via Extensions and Updates in the Tools menu of Visual Studio.

If you want to run OpenCover from the console then you can run it via the command prompt, doing it this way has much more flexibility as you can filter out test projects and generated code etc using the arguments you pass into the OpenCover executable.

opencover

Now once we have the code coverage within Visual Studio we can also get code coverage on our build server which is really what were after, due to OpenCover being an executable this is very easy to set up, you can find all the info you need to do this at the OpenCover Usage link.

In the second part of this blog post I’ll show how to generate a web page to show off these results which can be easily incorporated into your build server after each build, this will also show history of code coverage over time in case you want that feature too.

Squirrel – replace ClickOnce the easy way

Last week I was taking a look at Squirrel which states

Squirrel: It’s like ClickOnce but Works™

The goal of me looking at Squirrel was to find a decent replacement to ClickOnce, I read a tweet from @paulcbetts mentioning his work and how he had created Squirrel as a replacement to using ClickOnce.

Having spent some time implementing this and coming across one or two issues which I overcame, the good news is ClickOnce is hopefully no longer needed.

Squirrel works and does exactly what we require, our windows forms application’s can have a ‘check for updates’ option, which updates to the latest release, close the old application down after its updated, re-open from the original desktop or start menu shortcut and your done.

Steps to get Squirrel working

  1. Sourcecode is on GitHub.
  2. Create a simple Windows Forms Application
  3. Add the nuget package called squirrel.windows nugetinstalllike so:-
  4. Add the following to the program.cs like so (line 19 is the only one I added) :-
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    using Squirrel;
    
    namespace WindowsFormsApplication1
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                SquirrelAwareApp.HandleEvents(onAppUpdate: Form1.OnAppUpdate, 
                onAppUninstall: Form1.OnAppUninstall, 
                onInitialInstall: Form1.OnInitialInstall); 
                
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }
    
  5. Add some content like a simple button or a menu with the choice to check for updates
  6. in the code behind we implement squirrel like so:-
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    using System.Configuration;
    using Squirrel;
    using System.Reflection;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            private const ShortcutLocation DefaultLocations = ShortcutLocation.StartMenu 
           | ShortcutLocation.Desktop; 
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
            {
                // Check for Squirrel Updates
                var t = UpdateApp();
            }
    
            public async Task UpdateApp()
            {
                var updatePath = ConfigurationManager.AppSettings["UpdatePathFolder"];
                var packageId = ConfigurationManager.AppSettings["PackageID"];
    
                using (var mgr = new UpdateManager(updatePath, packageId, FrameworkVersion.Net45))
                {
                    var updates = await mgr.CheckForUpdate();
                    if (updates.ReleasesToApply.Any())
                    {
                        var lastVersion = updates.ReleasesToApply.OrderBy(x => x.Version).Last();
                        await mgr.DownloadReleases(new[] { lastVersion });
                        await mgr.ApplyReleases(updates);
                        await mgr.UpdateApp();
    
                        MessageBox.Show("The application has been updated - please close and restart.");
                    }
                    else
                    {
                        MessageBox.Show("No Updates are available at this time.");
                    }
                }
            }
    
            public static void OnAppUpdate(Version version)
            {
                // Could use this to do stuff here too.
            }
    
            public static void OnInitialInstall(Version version)
            {
                var exePath = Assembly.GetEntryAssembly().Location;
                string appName = Path.GetFileName(exePath);
    
                var updatePath = ConfigurationManager.AppSettings["UpdatePathFolder"];
                var packageId = ConfigurationManager.AppSettings["PackageID"];
    
                using (var mgr = new UpdateManager(updatePath, packageId, FrameworkVersion.Net45))
                {
    
                    // Create Desktop and Start Menu shortcuts
                    mgr.CreateShortcutsForExecutable(appName, DefaultLocations, false);
                }
            }
    
            public static void OnAppUninstall(Version version)
            {
                var exePath = Assembly.GetEntryAssembly().Location;
                string appName = Path.GetFileName(exePath);
    
                var updatePath = ConfigurationManager.AppSettings["UpdatePathFolder"];
                var packageId = ConfigurationManager.AppSettings["PackageID"];
    
                using (var mgr = new UpdateManager(updatePath, packageId, FrameworkVersion.Net45))
                {
                    // Remove Desktop and Start Menu shortcuts
                    mgr.RemoveShortcutsForExecutable(appName, DefaultLocations);
                }
            }
        }
    }
    
  7. We now need to create a .nuspec file which we will use to package up our application, Squirrel needs to have every file within the lib\net45 folder.

    To create a .nuspec file I simply copied nuget.exe into the solution and then went to a command prompt and typed:- nuget spec, this creates a .nuspec file which we can then hand edit inside Visual Studio and looks like this:-

    <?xml version="1.0"?>
    <package >
      <metadata>
        <id>WindowsFormsAppliation1</id>
        <version>1.0.0.0</version>
        <authors>Gregor Suttie</authors>
        <title>WindowsFormsAppliation1 Squirrel Tester</title>
        <description>Testing out Squirrel</description>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <releaseNotes>None</releaseNotes>
        <copyright>Copyright 2015</copyright>
      </metadata>
      <files>
        <file src="bin\Debug\*.*" target="lib\net45\"></file>
      </files>
    </package>
    
  8. Next we need to pack the .nuspec file to generate the nuget package, so run the following:-
    Nuget pack WindowsFormsApplication1.nuspec
  9. From the package manager console inside Visual Studio we now need to run the following command:-

    squirrel –releasify C:\Squirrel\WindowsFormsApplication1\WindowsFormsApplication1\WindowsFormsApplication1.1.0.0.0.nupkg

  10. In the folder above we should now have a folder called Releases, containing a setup.exe which we use to install the application.
  11. Run setup.exe and your app will install with shortcuts on the desktop and the start menu if you use the code above which has defined these as shortcut options.
  12. If you then run check for updates, the code will go off and check the folder you specify in the app.config like so:-
    <add key="UpdatePathFolder" value="c:\updatesForMyApp\"/>
    
  13. In order to update our app we also need the following appsetting which is the id of the Nuget package we created earlier, be careful not to have the id with a space or a dot (.) within the Nuget package Id.:-
    <add key="PackageID" value="WindowsFormsApplication1"/>
    
  14. Also add this one line to the assmblyinfo.cs:-
    [assembly: AssemblyMetadata("SquirrelAwareVersion", "1")]
    
  15. Once you install the application, go back and make a change to the code, save it, update the .nuspec file version number from 1.0.0 to 1.0.1 and then redo steps 7 and 8 (with the path to the new WindowsFormsApplication1.1.0.0.1.nupkg file)
  16. Almost done, copy the content of the releases folder to our location where we store the updates (our app.config setting for UpdatePathFolder which was set to c:\updatesForMyApp\)
  17. Run the app and choose the menu option ‘Check for Updates’ and you’ll get the latest version.
  18. Also note of you add a Debugger.Launch() statement in the code you can debug the code and step through line by line.

To find out more about further options available and much more about Squirrel click here.

Code coverage results for your JavaScript

I wanted to find out if there were any tools that can display code coverage results for JavaScript code (without the need for Java on the server or having to use Node), I experimented with a few different frameworks, test runners and what not and have spent a couple of hours looking in to this, in the end I opted for the following:-

jasmine_logo At work we currently use Jasmine which is a behaviour-driven development framework for testing JavaScript code along with PhantomJS which we use to run the tests using a specrunner (there are several ways to do it but this works for now)

I have installed the Visual Studio Extension called Chutzpah which is a JavaScript Test Runner which gives you a few nice things such as integration with TeamCity and the ability to right-click and then select Run Tests similarly to unit testing your C# code with your favourite unit testing framework.

chutzpah

Ok so back to code coverage for our JavaScript code – Matthew the guy who created Chutzpah is working on integrating code coverage into the next release so in the mean time here is how you get it to work – enter one further library called Blanket.js – Blanket.js is used to give us the code coverage results and currently supports Qunit, Mocha and Jasmine. An adapter API exists for supporting other test runners.

At this point you might be thinking its a fair amount of work to get this going but I will simplify the steps required to get your JavaScript code outputting code coverage results.

  • Download Blanket.js and look for the file called blanket-jasmine.js, add this to your Scripts folder inside your web application. This file is blanket version that works with Jasmine.
  • Add the Nuget package jasmine.js jasminenuget
  • Add some JavaScript code as below for example this is code.js
  • var stringLib = {
        vowels: function (a) {
            count = 0;
            for (var i = 0; i &lt; a.length; i++) {
                if (&quot;aeiou&quot;.indexOf(a[i]) &gt; -1) {
                    count++;
                }
            }
            return count;
        }
    }
    var mathLib = {
        add5: function (a) {
            return a + 5;
        },
        mult5: function (a) {
            return a * 5;
        }
    }
    
  • Add a JavaScript file with your Jasmine tests for the code above like so :-
    describe("general", function () {
        it("A basic test", function () {
            expect(true).toBeTruthy();
            var value = "hello";
            expect("hello").toEqual(value);
        });
    });
    
    describe("stringLib", function () {
        it("will get vowel count", function () {
            var count = stringLib.vowels("hello");
            expect(count).toEqual(2);
        });
    });
    
    describe("mathLib", function () {
        it("will add 5 to number", function () {
            var res = mathLib.add5(10);
            expect(res).toEqual(15);
        });
    
        it("will multiply 5 to number", function () {
            var res = mathLib.mult5(10);
            expect(res).toEqual(50);
        });
    });
    
  • Ok so we have some JavaScript code and Jasmine tests for the code, lets add some required links to our Jasmine specrunner page so we can see the code coverage
    results output to the screen, edit the spec runner like so:-

    specrunner2

    The above screen shot shows that we have added a reference to both our JavaScript code file and the file containing the Jasmine tests.

  • Now if we browse to the specrunner page for Jasmine we will see the results of the test – green for passed and red for failed (no code coverage results yet).

    jasmineresults1

  • Now lets show the JavaScript code coverage results – which is as simple as adding in the line to reference Blanket for Jasmine:-

    specrunner3

  • Refresh the specrunner page and you will now see your JavaScript code coverage results like so:-

    specrunner4

    The screen shot above shows us 100% code coverage, so no lines of JavaScript are coloured red (not covered)

  • If I comment out one of the tests we can see the difference below:-
    specrunner5
  • And there we have it code coverage results of your JavaScript, brief post but enough to get you started I hope, next time I will add this to TeamCity and see what that gives us.