Tag: Redgate

  • Git Push in SQL Source Control

    I’m excited. Git support is growing in SQL Source Control and the release recently contains Git push/pull support. If you don’t see the release of 4.1.x, you need to update. The update should be available for everyone.

    I do like Git. I’m working with SVN, TFS, and Git on a regular basis. However I tend to prefer Git overall. Now I can do most things I want to do in SOC, which is exciting. No forgetting to push or the need to drop to the command line or another interface.

    The Git Additions

    In a previous post, I set up a link between a database and a Git repository. Let’s see how the Git push now works.

    I’ve got this database on my local instance.

    soc_git_push_2

    It’s linked with SQL Source Control to a local Git repository.

    soc_git_push_3

    Let’s change something. I’ll pop open a stored proc.

    soc_git_push_4

    Let’s make a simple, but real, change. I’ll add a NOCOUNT setting in there, which is a good idea.

    soc_git_push_5

    In the Commit tab for SQL Source Control, I see my change. Note that most everything looks the same here and I can see the code differences like I always could.

    soc_git_push_6

    Now I commit this change. In this case, it goes to my local Git repo, but isn’t pushed.

    NOTE: In the real world, I’d pull before I commit to be sure I wasn’t causing a merge issue.

    Once that’s done, my Commit tab changes.

    soc_git_push_7

    SQL Source Control (SOC) detects the change and knows we have an unsynced Git repo. I can now push the “Push” button. Once I do this, I get a warning if I haven’t turned this off. I turn it off, but you should always be aware that SOC isn’t managing your Git repo. It’s linking to it. If you make changes in the repo with some other app, like Visual Studio, the entire repo is pushed, not just the change SOC committed.

    soc_git_push_8

    Once this is done, I once again see a clean Commit tab.

    soc_git_push_1

    However, if I go to the remote repo, which is on GitHub in this case, I see my change.

    soc_git_push_9

    I love Git. I think it’s really cool, and it’s a great way to work with distributed Version Control. I am playing with TFS as well, but I prefer Git, and I think this support is great.

    There are still things needed, things I want, and more, so be sure you add your votes to the SQL Source Control UserVoice page and let the SOC team know what’s important to you.

    If you’re not using SQL Source Control, you might check it out and give it a try for a month: http://www.red-gate.com/products/sql-development/sql-source-control/

  • Traveling in Wales

    It’s SQL Relay Cardiff day today. This is my first time in Wales, and it’s a busy day at SQL Relay. It’s a bit of deja vu with me delivering the same session on Version Control today that I presented 18 ours ago at SQL Relay Bristol.

    It’s interesting being on part of the SQL Relay tour, with some of the same speakers, some different, the rapid travel from one city to the next, and orienting myself in a new environment. The venues are much different, with a set of hotel conference rooms yesterday and a large arena facility (the Motorpoint Arena) today.  It’s surprisingly confusing trying to find toilets, speaker rooms, and more.

    However the people are nice, and friendly, although quiet. I got lots of good questions both days, on a semi-confusing topic, and that will help me revisit the session and make some changes over the next few days.

    I get to help on a second session today as we have a speaker that’s ill. Then it’s some work as I prep for travel back to London and SQL in the City in London on Friday.

     

  • Testing Sum By Month

    I’ve been on a testing kick, trying to formalize the ad hoc queries I’ve run into something that’s easier to track. As a result, when I look to solve a problem, I’ve written a test to verify that what I think will happen, actually happens.

    The Problem

    I saw a post recently where someone wasn’t sure how to get the sum of a series of data items by month, so I decided to help them. They asked for a year number, a month number, and a total, so something like this:

    Year   Month   Sales

    2012       1   1500.23

    2012       2   1480.00

    2012       3   1945.00

    2015       7   8933.11

    They mentioned, however, that the had sales data stored as an integer. Not as 201201, but as 1, 2, 3, with a base date being Jan 1, 2012. That’s strange, but it’s a good place to write a test.

    I like to start with the results, since if I don’t know the results, how can I tell if my query works? Let’s get a test going. I’ll start by created my expected results. I’ve come to like using temporary tables, and limited data. I also like to test some boundaries, so Iet’s cross a year.

    CREATE PROCEDURE [tArticles].[test sum of sales by month for multiple months]
    AS
    BEGIN
    -- Assemble
    CREATE TABLE #Expected (
    yearnum INT
    , monthnum TINYINT
    , salestotal NUMERIC(10,2)
    )


    SELECT *
    INTO #actual
    FROM #Expected AS e



     

    INSERT INTO #Expected
    ( yearnum
    , monthnum
    , salestotal
    )
    VALUES
    ( 2012, 11, 2500.23 )
    , ( 2012, 12, 2200.15 )
    , ( 2013, 1, 2656.75 )

    I like to create the actual results table here as well, which allows me to then easily insert into this table from a procedure as well as a query. In this case, I’ll use a query, but I could use insert..exec.

    Once I have results, I need to setup my test data. In this case, I’d probably go grab the rows from a specific period and put them in a temp table and use Data Compare to get them. Or make them up. It doesn’t matter. I just need the data that allows me to test my query.


    EXEC tsqlt.FakeTable @TableName = N'MonthlySales';

    INSERT MothlySales
    VALUES
    ( 11, 1000.00)
    , ( 11, 1500.23)
    , ( 12, 2200.15)
    , ( 13, 1000.00)
    , ( 13, 1656.00)
    , ( 13, 0000.75);

    I don’t try to make this hard. I use easy math, giving myself a few cases. One, two, three rows of data for the months. If I think this isn’t representative, I can add a few more. I don’t try to be difficult, I’m testing a query. If I had rows that might not matter, or I wanted to test if 0 rows are ignored, I could do that.

    Now I need a query. Something simple, a SUM() with a GROUP by is needed. However I need to also change 11 into 2012 11, so that’s an algorithm.

    An easy way to do this is start with a base date. I’d prefer this is in a table, but I can do it inline.

    INSERT #actual

    SELECT
    yearnum = DATEPART( YEAR, DATEADD( MONTH, datenum, '20120101'))
    , MONTHNUM = DATEPART(MONTH, DATEADD( MONTH, DATENUM, '20120101'))
    , SALESTOTAL = SUM(ms.salesamount)
    FROM dbo.MonthlySales AS ms
    GROUP BY
    DATEPART( YEAR, DATEADD( MONTH, datenum, '20120101'))
    , DATEPART(MONTH, DATEADD( MONTH, DATENUM, '20120101'))
    ORDER BY
    DATEPART( YEAR, DATEADD( MONTH, datenum, '20120101'))
    , DATEPART(MONTH, DATEADD( MONTH, DATENUM, '20120101'))
    ;
    GO

    I’ll insert this data into #actual, which tests my query.

    The final step is to assert my tables are equal.

    -- Assert
    EXEC tsqlt.AssertEqualsTable
    @Expected = N'#EXPECTED',
    @Actual = N'#actual',
    @FailMsg = N'The calculations are incorrect';

    The Test

    What happens when I execute this test? I can use tsqlt.run, or my SQL Test plugin.

    2015-09-28 16_07_49-Photos

    In either case, I’ll get a failure.

    2015-09-28 16_08_11-Photos

    When I check the messages, I see the output from tSQLt. In this case, none of my totals seem to match.

    2015-09-28 16_15_23-Photos

    What’s wrong? In my case, I’m adding the integer to the base month, but that means a 1 means 2012 02, not 2012 01. I’m a month off. Let’s adjust the query.

    -- Act
    INSERT #actual
    SELECT

    yearnum = DATEPART( YEAR, DATEADD( MONTH, datenum, '20111201'))
    , MONTHNUM = DATEPART(MONTH, DATEADD( MONTH, DATENUM, '20111201'))
    , SALESTOTAL = SUM(ms.salesamount)
    FROM dbo.MonthlySales AS ms
    GROUP BY
    DATEPART( YEAR, DATEADD( MONTH, datenum, '20111201'))
    , DATEPART(MONTH, DATEADD( MONTH, DATENUM, '20111201'))
    ORDER BY
    DATEPART( YEAR, DATEADD( MONTH, datenum, '20111201'))
    , DATEPART(MONTH, DATEADD( MONTH, DATENUM, '20111201'))

    Now when I run my test, it passes.

    2015-09-28 16_18_09-Photos

    Why Bother?

    This seems trivial, right? What’s the point of this test? After all, I can easily check this with a couple quick queries.

    Well, let’s imagine that we decide to move this base date into a table, or that we alter it. We want our queries to continue to work. I can have this test as part of an automated routine that ensures this test will run each time the CI process runs. Or each time a developer executes a tsqlt.runall in this database (shared or populated from a VCS). I prevent refactoring queries.

    More importantly, I can take results and alter them first, say if someone decides to change this to a windowing query. I could plug a new query in the test (or better yet, use a proc and put that call in the test) , and if I change code, I can verify it still works.

    Write tests. You need them anyway, so why not formalize them? The code around this query, mocking test data, is something I do anyway, so this gets me a few more minutes to verify that the code works. I can tune the query, alter indexes, perf test, and be sure that code is still running cleanly.

    http://www.sqwhere lservercentral.com/Forums/Topic1716471-1292-1.aspx#bm1716535

  • SQL Source Control and Git–Getting Started

    It seems as though Git is taking the world by storm as the Version Control System (VCS) of choice. TFS is widely used in the MS world, but Git is growing, Subversion is shrinking, as are most of the other platforms.

    As a result, I wanted to do a quick setup using SQL Source Control (SOC) and Git, showing you how this works. SOC supports Git in a few ways, so this is the primary way I’d see most people getting started.

    Update: Since this was published, the SQL Source Control team released an updated version (v4.1) with support for Git that allows push/pull within the client. I’ve got an updated post here.

    Scenario

    Here’s the scenario that I’ll use. I’ve got a database, WindowDemo, that has a few tables, some data, and a few procs. As you can see below this isn’t linked to a VCS.

    2015-09-24 16_34_56-Cortana

    I want to store my DDL code in c:\git\WindowDemo\trunk. I’ve got that folder created, but it’s empty. I’ll keep related database stuff (docs, scripts, etc) in c:\git\WindowDemo if I need it.

    2015-09-24 16_37_36-Photos

    Git Setup

    The first thing you need to do is get your Git repository setup. There are many ways to do this, but I’ll use the command line because I like doing that. The commands in the various client GUIs will be very similar.

    I’m going to set the git repository here at c:\windowdemo to keep all my database stuff in one place. To setup the repository, I run a git init in the command prompt. This initializes my repository.

    2015-09-24 16_41_02-Photos

    Now I have a git VCS, I need to get code in there.

    SQL Source Control Setup

    Now I move to SSMS to link my database to the repository. In SSMS, I right click my database and select “link database to source control”.

    2015-09-24 16_42_27-Start

    This will open the SOC plugin on the setup tab. I’ve filled in the path to the place in the repository I want the code to go. This is the trunk folder. I’ve also selected Git, using the “Custom” selection on the left and Git in the dropdown.

    2015-09-24 16_43_54-Link to source control

    Once I click the link button, I’ll get a dialog showing progress and then return to the setup tab.

    2015-09-24 16_44_15-Start

    Notice the balloon near the top. This lets me know the link is active and I have changes in my database that aren’t in the VCS. There’s a pointer to the “Commit changes” tab, so I’ll click that.

    2015-09-24 16_48_00-New notification

    In the image above, I see I have a number of “new” objects from the perspective of the VCS. I can see the name, and the type of object in the middle. At the bottom, I see the version in my database (highlighted code) on the left and the version in my VCS (blank) on the right.

    This is where I commit my changes. I enter a comment at the top and click the “commit” button on the right (not shown). When I do that, I’ll get a clean “commit tab” that shows that my VCS is in sync with my database DDL.

    2015-09-24 16_50_18-SQL Source Control - Microsoft SQL Server Management Studio

    Inside Git

    What’s happened in my VCS? Let’s look in the file system. Here I see my trunk folder.

    2015-09-24 16_51_49-Photos

    SOC has created a structure for my DDL code and included some meta data. If I look in one of these folders, such as Stored Procedures, I see

    2015-09-24 16_58_56-Photos

    This is the .SQL code that matches what’s compiled in my database. SOC stores the current CREATE statement for all my objects so that they can easily be examined.

    Inside Git, I see a clean status with all my files as committed objects.

    2015-09-24 17_02_57-Start

    This is what I want. Now I can continue on with database development, tracking all my changes. I’ll look at the flow and tracking changes in another post.