Tag: Redgate

  • RegEx Those Dates

    This is a series on SQL Data Generator, covering some interesting scenarios I’ve run into. If you’ve never tried it, SQL Data Generator is a part of the SQL Toolbelt. Give it a try today with an evaluation today.

    Recently I was working on transforming some dates, and wanted to generate a large n number of dates for testing. I decided to use SQL Data Generator, and a little RegEx to meet my needs.

    The format I needed was CYYMMDD, which is the century as a 0 or 1 (1900 or 2000) and then the yymmdd format. While there are some pre-made expressions to build dates, there wasn’t an easy one to handle the century like this. I could have used a date expression in T-SQL and randomly allocated a century, but I decided to play around with RegEx.

    I know that the brackets allow a choice of values to be used. The regular expression can choose any of the values to match. An example is for the first part of my date, the century. It can be zero or one, so I can do this:

    [01]

    When Data Generator runs, it will randomly build expressions that match this pattern, which in my case results in

    regex01

    That makes it easy for me to pick numbers, and I could do something like this for the years:

    [01][0-0][0-9]

    That works, as any number from 00, as in 2000, up to 99, as in 1999, is valid. That gets me this:

    regex02

    Months

    However that causes issues when I get to the month. I need a two digit month, but I can’t have some combinations of two digits. If I were to write [01][1-9], I’d get months like 18, which aren’t valid. Instead, I need a pattern that only matches a 0 with 1 to 9, and only allows a 1 with a 1 or 2.

    To do that, I’ll use an OR. That’s a pipe (|) in regular expressions. I’ll say (in pseudocode), give me a (01 to 09) OR a (10-12). The easy way to build that is like this:

    (0[1-9]|1[0-2])

    This says that if we match the first half (before the pipe), then we literally have a 0 there, with a second character in the range 1-9. That gives us 01 to 09. The second half, after the pipe, does the same thing, but it matches a literal “1”, and then a 0, 1, or 2. As you can see, I have random months (only showing this expression).

    regex03

    Days

    Now the hard part: days.

    Days are strange in the calendar because the possible days depend on the months. Years and months are consistently in ranges, but the days are not. Let’s start with the most common days: 31.

    I have 31 days in months 1, 3, 5, 7, 8, 11, 12. In order to match these up, I’ll need to combine the month and day items. Let’s first change our months to be just those particular months. That gives me:

    (0[13578]|1[02])

    With these months, I am going to allow up to 31 days. The patterns for the first 29 days of the month are the same. A 0, 1, or 2, with any combination of 1-9. Putting that together gives me:

    (0[13578]|1[02])([012][1-9])

    This handles the first 29. The next two, 30 and 31, are an OR expression like the months. I’ll use a literal 3 and a choice of zero or one. That gives me:

    regex04

    Whew! This is a lot of work, but it matches things up well. Now I need to handle 30 days. I’ll do that the same way, but I’ll now OR both expressions together. The expression is:

    ((0[13578]|1[02])([012][1-9]|3[01]))|((0[2469]|11)([012][1-9]|30))

    And the data:

    regex05

    That gets me almost all the months. The last part of February, the hardest. Now I could worry about leap years, but I’m not going to. Proper handling here means verifying the year (and century here) and doing math to ensure a leap year is viable. Instead, I’m going to just ignore the 30s and manage days 1 to 29.

    ((0[13578]|1[02])([012][1-9]|3[01]))|((0[2469]|11)([012][1-9]|30))|((02([012][1-9])))

    Now I have a nice set of random dates if I put everything together.

    [01][0-0][0-9](((0[13578]|1[02])([012][1-9]|3[01]))|((0[2469]|11)([012][1-9]|30))|((02([012][1-9]))))

    regex06

    References

    I leaned on a few examples to decode a few of the expressions and also to check that I wasn’t messing up.

  • SQL in the City 2014 – Washington DC

    I’m off today, traveling to SQL in the City 2014 – Washington DC to meet Grant and deliver another database delivery seminar. We’ll be talking about Version Control, testing, continuous integration, and database delivery, and showing off some of the Red Gate tools that can make the process easier.

    I really think that you will reap benefits if you start to build a software delivery pipeline and incorporate some of the agile/ALM/DLM processes into your software build system. It’s some work up front, and maintenance ongoing, but it does allow you to regression test, rapidly review changes, and consistently deploy software to your production systems.

    Not easy, but it does build solid engineering habits and should help you deliver software reliably, and quickly.

    We’re looking at doing more seminars in 2015 and are planning things now. If you’re interested in having us some to your city, perhaps around a SQL Saturday, or maybe just because you don’t have any events, send a request to Red Gate. Use sqlinthecity@red-gate.com and let them know you want Grant and myself to swing by.

  • How Often Does Production Drift?

    One of the new products that Red Gate Software is working on is SQL Lighthouse, which is designed to track and alert you on “database drift“, which are the changes in your schema. I hadn’t really heard the concept in the past, though I’ve found there are a few people using the term to describe the changes being made to production that aren’t inline with the development project that created (and updates) the database.

    Vendors that sell products backed by a SQL Server database, like Microsoft Dynamics, often consider database drift to be a violation of their EULA. When a customer changes the database, even by just adding indexes, it causes them support costs, though arguably those costs are lower than if no indexes were ever added by enterprising DBAs.

    However this also happens in databases built by companies. It seems there’s no shortage of “quick fixes” in production, in addition to the “improvements” that DBAs and others seem to make to their live databases in order to solve some short term issue. I know there’s a need for this, and even seen recommendations to ensure production differs from development schemas. However I haven’t had many issues with this in the past and I’ve tried to prevent any drift.

    Maybe because I’m a controlling DBA, or maybe because I’ve been lucky. In either case, while I’ve had to implement hot fixes and changes in production, it’s been rare. Most of the time I’ve been able to backfill these changes in development (and test) environments, or delay the fix until it could be tested.

    However I’m sure there’s a need for a way to manage drift, and detect it as the number of databases you manage grows, so I’m excited by SQL Lighthouse and hope it helps DBAs avoid those late night phone calls by being aware of what’s changing in their systems.

    Steve Jones

    The Voice of the DBA Podcast

    Listen to the MP3 Audio ( 2.2MB) podcast or subscribe to the feed at iTunes and LibSyn.

  • Off to London

    Today I’m off across the Atlantic again. SQL in the City 2014 kicks off in London and I have 3 talks to give on Friday. Plus a keynote.

    Today is keynote practice, last minute packing, and then off to the airport for my flight overnight to London. This will be another quick trip, and although I think it would be good to spend a few days in Cambridge at the Red Gate offices, I’m so focused on SQL in the City, as well as this being my third trip this month, I didn’t think it made sense. I still have a week in Seattle coming, so this has been a tough time.

    I am a bit excited about the event. We have a new venue, and we’ve worked to fit a number of talks together in a sequence that I think can help get people moving forward in a Continuous Delivery software+database development process. I really think this is the way to build better software, and I want to see us jumpstart development on SSC in this manner in 2015, and hopefully get continuous work being done.

    I’ve also got a new talk based on the DBA Team articles, and I hope everyone likes it. I tried for a bit of humor, so we’ll see. If it does well, I’ll do it in Seattle. If not, I’ll be reworking things next week.

    Once again work blends into the weekend, though not by much. I’ll be heading back from the UK Saturday morning, so I’ll miss a half day in Denver, though it will be a long day in all for me. My flight will leave around 1am Denver time, so by the time we hit Saturday night, I’ll be a bit worn out.