Tag: Redgate

  • Data Masker for SQL Server and Temporal Tables

    A customer had a question recently about using Data Masker for SQL Server and temporal (or system versioned) tables. I decided to make a quick demo that will show how this works.

    This is a simple demo, using just a few changes, but the concepts are useful for larger systems.

    Setting up a temporal table

    In the demo database for Data Masker, there is a dbo.dm_customer table. I’m going to make this a temporal table with a few schema changes. First, I’ll add two columns that will be used to store the valid datetime ranges.

    ALTER TABLE dbo.DM_CUSTOMER
    ADD StartTime DATETIME2(3) GENERATED ALWAYS AS ROW START
             DEFAULT GETUTCDATE(),
         EndTime DATETIME2(3) GENERATED ALWAYS AS ROW END
             DEFAULT CONVERT(DATETIME2(3), '9999-12-31 23:59:59.999'),
         PERIOD FOR SYSTEM_TIME(StartTime, EndTime);
    GO

    Next, I’ll enable system versioning, and specify the history table.

    ALTER TABLE dbo.DM_CUSTOMER SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE=dbo.DM_Customer_History));
    GO

    At this point, let’s test. I’ll query the history table and show it’s empty.

    2018-07-18 15_49_55-datamask_temporal.sql - dkrSpectre_sql2016.DataMaskerDev_Grant (DKRSPECTRE_way0u

    Now, let’s make a couple changes.

    UPDATE dbo.DM_CUSTOMER SET customer_firstname = 'Jillian' WHERE customer_id = 1000002
    UPDATE dbo.DM_CUSTOMER SET customer_firstname = 'Sian' WHERE customer_id = 1000004
    INSERT dbo.DM_CUSTOMER ( customer_id,customer_firstname,customer_lastname, customer_gender, customer_company_name,
         customer_street_address, customer_region, customer_country, customer_email, customer_telephone,
         customer_zipcode, credit_card_type_id, customer_credit_card_number)
    VALUES
    (   '1000', 'Steve', 'Jhones', 'M', 'Redgate', '123 My St', 'CO', 'US',
         'sjones@sqlservercentral.com', '30333333333', '80014', '1', '12`345566')
    GO
        
    UPDATE dbo.DM_CUSTOMER
          SET customer_lastname = 'Jones' WHERE customer_id = 1000
    UPDATE dbo.DM_CUSTOMER
          SET customer_lastname = 'Jilly' WHERE customer_id = 1000002
    UPDATE dbo.DM_CUSTOMER
          SET customer_lastname = 'Jill' WHERE customer_id = 1000002
    GO

    We requery, and you can see there is data in the history table (at the bottom). All of my changes are captured in a sequence of copies of the row.

    2018-07-18 16_09_26-datamask_temporal.sql - dkrSpectre_sql2016.DataMaskerDev_Steve (DKRSPECTRE_way0u

    There are a few more rows in the history table, but they got cut off in the image. That isn’t that important. Just note that the names match up, for the most part, between the tables.

    Using Data Masker

    Now the interesting part. I have a masking set that’s similar to ones I’ve used in other articles. In this case, I’m changing names in two substitution rules. However, these rules are set for the dbo.dm_customer table. What about the dbo.dm_customer_history table?

    To change this, I need to do a few things. First, I need a Command rule to disable system versioning. This is the way I disconnect the tables so that I can update data.

    2018-07-18 16_10_31-Edit Command Rule

    This has to take place before I change dbo.dm_customer, otherwise I’ll insert a bunch of new data in the history table. My ordering looks like this:

    2018-07-18 15_58_36-temporal_ Data Masker for SQL Server

    Once this is complete, I now need a Table-Table sync rule to ensure data in one table matches the other. When I create the rule, I need to have a way to join the two tables together. In this case, I use the customer_id column.

    2018-07-18 15_59_49-Edit Table-To-Table Rule

    Note that I’m ensuring the names and credit card numbers match here. This will wreck my history table slightly, in that all of the rows for a customer_id will have the same value, but this does ensure I don’t have sensitive information leaking.

    Once I’m done, I make this a child of the other rules. This ensures that the substitutions run on the main table, then are synchronized to the history table.

    2018-07-18 16_02_37-temporal_ Data Masker for SQL Server

    You’ll notice I also have another command rule at the bottom. The text for this rule is below, and this rule enabled the temporal link again. I’m assuming the application is somehow using this. If not, you can ignore this rule and leave the tables disconnected if that’s Ok in your application.

    2018-07-18 16_09_59-Edit Command Rule

    All I need to do now is run the rules. Let’s do that.

    2018-07-18 16_05_52-

    Once the rules are complete, let’s query the tables again. I’m only looking for those rows that were changed previously.

    2018-07-18 16_13_14-datamask_temporal.sql - dkrSpectre_sql2016.DataMaskerDev_Steve (DKRSPECTRE_way0u

    As you can see, the dbo.dm_customer (top) table and the dbo.dm_customer_history table (bottom) are synchronized. The multiple values I had for different versions of customer 1000002 are now all the same. I’ve lost history, but if this were sensitive information, that works out. I could have used random values instead for the history table, but here I can see linkages if I’m checking for similar data.

    This is a Hard Problem

    Trying to determine how to rewrite history for audit tables is hard. Data Masker can do pretty much anything you would like, but here we need to be sure that we have an idea of what we want to do.

    I’ve presented a simple version of setting all history to the current value, which will work in some cases. If not, let me know what you need to see, and I’ll build you a scenario.

    Data Masker for SQL Server is an amazing tool in the SQL Provision bundle. Combined with SQL Clone, you can deploy databases with masked data for dev and test environments in seconds. Give it a try today and see what you think.

  • Is this a SQL Provision Cloned Database?

    As I work with SQL Provision, I keep finding new questions and concerns from clients and customers. Recently I had someone wonder if we could determine whether or not a database on which they were working was a SQL Clone cloned copy.

    You can, and it’s easy to check. When SQL Clone creates a database, it will use the base image, and ensure there is an extended property set on the database itself.

    The function sys.fn_listextendedproperty() is used to return the database extended properties. We can use the DEFAULT keyword for the various parameters, like this:

    SELECT objtype ,
            objname ,
            name ,
            value
    FROM fn_listextendedproperty(DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT);
    GO

    This gives me an empty result set on a non-SQL Clone database if I have no extended properties set. If I had others set, I might get some result. For a database I’ve setup with SQL Provision, I’ll get this:

    2018-07-02 17_02_58-SQLQuery1.sql - (local)_SQL2016.StackOverFlow (PLATO_Steve (69))_ - Microsoft SQ

    For the most part, I don’t care that I’m using a clone rather than a native SQL Server database, but there could be places I do care, and certainly I want to filter out this extended property from my version control system.

    SQL Provision is a great tool for rapidly giving new environments to developers without the hassles of restoring copies and using lots of storage space. If you want to give it a try, download an evaluation today.

  • The Figleaf Data Masking Beta

    I’ve been using the Redgate Software Data Masker for SQL Server in a number of situations to help customers. This is a tool we purchased in 2017 and is a part of the SQL Provision bundle to make building safe, secure dev and test databases.

    Project Figleaf is the next version of our data masking product and you can help us make it better. There’s a beta out that you can download and play with. Our development team is conducting research to better understand how we can meet your data masking needs.

    As much as I like Data Masker, it’s a clunky, cumbersome tool that takes some getting used to working with. The UI is old, and at times, a bit of an impediment. It works well, and I’ve grown used to it, but we can do better, and we’re trying.

    If you work with sensitive information in your production systems, don’t bring that back to dev and test. Instead, give our new Figleaf beta a try and see if this is something that might be helpful to you. Or even download Data Masker and try that.

  • Updating VSTS to SQL Change Automation

    We (Redgate) released SQL Change Automation a few weeks back. This is the first stage to implementing a combined toolset that works with both state/comparison development and migrations based development. The change updated both our migrations toolset (previously ReadyRoll) and our build/release cmdlets (DLM Automation) to SQL Change Automation (SCA).

    I have pipelines that are setup with Redgate DLM Automation tasks. This post shows how I updated the first of these to SCA.

    Finding the Build

    Here’s my current VSTS build pipeline for a project. Note that these are DLM Automation 2 tasks. There is a single task that you configure with the Operation (on the right) to build, test, or sync a database.

    2018-06-28 16_56_49-SQL Server Builds - SOC-CI - Visual Studio Team Services

    Note that there is a version drop down at the top of the right side. Here I need to drop this down to find the SCA task, which is version 3.

    2018-06-28 16_58_05-SQL Server Builds - SOC-CI - Visual Studio Team Services

    Once I select this, you’ll see that my task changes to SCA, both on the left and right.

    2018-06-28 16_58_14-SQL Server Builds - SOC-CI - Visual Studio Team Services

    I can change both tasks to v3 and I’m set. This is really the change that we’ve done, and we’ve combined operations. If you drop this down you will see there are SCA (migrations) projects here along with SQL Source Control state projects.

    2018-06-28 16_59_28-SQL Server Builds - SOC-CI - Visual Studio Team Services

    This means that if I convert from a SQL Source Control/state project, my pipeline stays intact. I’ll just change the operation from a build with SQL Source Control to one with SCA.

    This mostly works, but there are some path differences for release, so I’ll talk about those in another post.

    This is a simple change, but it’s important for future strategy. You can watch a bit of our strategy in this SQL in the City video on the database deployment pipeline. If you want to give this a try, download SQL Source Control and/or SQL Change Automation and start a test project.