Tag: Redgate

  • Exporting SQL Data Compare Results to Excel

    I had someone ask recently about getting SQL Data Compare results in Excel. It’s easy to do and this post looks at the process.

    Exporting a Comparison

    I won’t go into the details of making a comparison. I have another post that looks at this with joins, but the tool is fairly intuitive (ingeniously simple) to use.

    Once you have a comparison, you should see something like this image. Here I have two tables that are different (I selected all tables). The first, dbo.CountryCodes, has a difference in a row, different values in the name.

    2023-04-14 13_47_56-SQL Data Compare - E__Documents_SQL Data Compare_SharedProjects_(local)_SQL2017.

    The second, dbo.Status, has some rows in the source (left) that are not in the target.

    2023-04-14 13_48_02-SQL Data Compare - E__Documents_SQL Data Compare_SharedProjects_(local)_SQL2017.

    To export these results, I use the Tools menu. There is an option you can see below:

    2023-04-14 13_48_08-

    Once I pick this, I get a dialog with options. I can pick certain tables, or all. The defaults are all tables, and only show differences. Note the identical button is not selected.

    2023-04-14 13_48_17-Export CSV files

    If I open the folder in the dialog above (after clicking Generate), I see my files. There are separate files for each table and one with a summary.

    2023-04-14 13_48_24-Reports

    If I double click the dbo.CountryCodes.csv file, Excel opens, but not the way I like it. I see this:

    2023-04-14 13_58_46-dbo.CountryCodes.csv - Excel

    However, if I File | Open the file, I get the wizard for delimited files.

    2023-04-14 13_59_00-Expenses 2023.xlsx - Excel

    When I go to the second page and click “comma” as the delimiter, I see a better preview.

    2023-04-14 13_59_05-Text Import Wizard - Step 2 of 3

    I can finish this and I see my data. In this case, the first column lets me know this is changed data that has the same row with the same PK in both databases.

    2023-04-14 13_59_22-dbo.CountryCodes.csv - Excel

    Similarly, I get open the Status table file and see this. Here the first column lets me know this data is only in the first database, the source or left database, that I set in my SQL Data Compare project.

    2023-04-14 13_59_50-dbo.Status.csv - Excel

    The summary also needs the same open process and this shows me all tables, with lots of zeros. However, for my two tables, you can see there is 1 row noted in the Different column for CountryCodes and 3 rows only in the source (SimpleTalk_1_Dev) database.

    2023-04-14 14_00_35-Results Summary.csv - Excel

    I can then save these in Excel format if I like and send them around to colleagues.

    Summary

    You’ve seen how you can review SQL Data Compare results in Excel. I don’t know if your Excel will open the CSV with values in separate columns, or if you need to perform a File | Open as I did.

    This is useful for sending to a business user that might need to make decisions about what data needs to be synched where. The hardest part here is explaining the _s and _t names for source and target.

    SQL Data Compare is very handy for single GB data sets to compare. I wouldn’t recommend this for > 10GB, but under that, with good hardware, you should have success comparing tables or views.

    If you’ve never tried it, download an evaluation today.

  • Flyway Desktop PoC–Adding a Shadow and Baseline Script

    In the last post, I created a baseline marker for Flyway in each database. This set the version in the dev and QA databases to v1. However, I also need a baseline script, at least the tool asks for one, so this is the process if you have objects in your production or other downstream databases.

    I’ll do this for SQL Server and then PostgreSQL.

    Why do this?

    The main reason to create a baseline script is to note which objects already exist in production. For these objects, I don’t want to track these are changes in their current form.

    For example, if I already have a CountryCodes table in production, when I create a project, I want to tell Flyway Desktop that this table exists in production, so if the dev version matches, don’t add this to scripts. If it doesn’t, then I’ve done something in development and need an ALTER script deployed to prod.

    What was the Other Baseline?

    The first baseline in this post, is a version marker. I hate that this is the case, but both Flyway (pre-Redgate) and Flyway Desktop (evolved from SQL Change Automation), had the concept of a baseline, but these were somewhat different things.

    Flyway Baseline – The initial version of the database. Don’t deploy any scripts that are <= to this version.

    Flyway Desktop Baseline – A script that has the structure and code of all objects that exist in the target database(s).

    We can create a baseline script for Flyway, which looks for a B script, but the baseline command expects that you create this script manually. This is used to populate a new database with the baseline migration script prior to running all other scripts.

    Setting up the Baseline Script

    Flyway Desktop makes it easy to create a baseline script, and in fact, prompts you to do so.

    In my project, if I go to the Schema Model (first) tab, I see there is an object in Development. This was the table I created when I set up the database. The goal is to get this table to other environments.

    2023-04-04 16_10_58-Flyway Desktop

    This table doesn’t exist in QA. I do have the flyway_schema_history table, which was the result of the baseline command.

    2023-04-04 16_15_58-SQLQuery4.sql - ARISTOTLE_SQL2022.FWPoc_1_Dev (ARISTOTLE_Steve (77))_ - Microsof

    If I go to the Generate Migrations (second) tab, I see this. The first thing that the tool wants is a Shadow database.

    2023-04-04 16_12_55-Flyway Desktop

    The shadow is essentially a development V-1 (v minus one) version. This is where I test all migrations, compare the state with development, and then determine what’s changed. This is just a regular database, but I create this outside of Flyway Desktop. For me, I created a database (FWPoc_1_Dev_Shadow) and then clicked Set up shadow database to get this dialog. You can name this anything.

    I enter details, and test the connection before saving this. In general, this ought to be saved to my user settings as I’ll have my own shadow different from other developers. I DO NEED to click the “ok to erase data” box.

    2023-04-04 16_14_09-Flyway Desktop

    Once this is done, I now see another prompt on the Generate Migrations tab. Now I need a baseline script. I don’t have anything, but I will click the button.

    2023-04-04 16_14_28-Flyway Desktop

    This gives me a dialog to pick a target database. This target is used to get the initial set of objects to populate in the baseline script. You can use production or a copy (recommended) as the target database.

    2023-04-04 16_14_41-Flyway Desktop

    My QA is the same as prod, so I add that with the proper connection string and then I see the target here for the Baseline. I am ignoring static (or lookup/reference data for now). I’ll click the Baseline button.

    2023-04-04 16_15_25-Flyway Desktop

    This runs and … nothing.

    Which makes sense, as there is nothing in my target database. I actually get an error after this, which tells me that it doesn’t make sense to baseline an empty database.

    2023-04-10 12_38_51-Flyway Desktop

    I wish that were surfaced earlier. In any case, if I close this, I get the same image above, saying I don’t have a baseline. For now, I’ll ignore that.

    Summary

    Not much happened in this post. I added a shadow database, which I’ll use to generate scripts. I tried to baseline, but that errored, as it should. I really don’t need a baseline, so I’ll come back to this later in another format.

    For now, I’ve advanced the SQL Server project. I’ll actually repeat these steps for the PostgreSQL one, but it’s really creating a new database for the shadow and setting a connections string. Everything else looks the same.

    The next post will actually generate a script and deploy this to QA.

  • A Corporate Wellness Day

    Today is the corporate Wellness Day at Redgate. It’s a day off for almost everyone in the company, unless they have something that can’t get moved. A few support people, a few others, like me, that are traveling. We, however, will get another day that we can take off.

    When we revamped benefits for 2023, one of the things that we wanted to encourage was health and wellness. We added a couple days off, like this company holiday and a birthday day off. We also reduced the ability for employees to get out of vacation by not buying back days and emphasizing that managers should ensure they work with people to take their entire allowance. We can carry over 5 days, but no more.

    I like the idea of this day off, as it means people take some time away and won’t come back to a bunch of emails or other things that people sent during their break. Instead, the company is essentially shutting down.

    I don’t mind missing today for travel. I’ve got things to do and I’m looking forward to working with a customer tomorrow. However, I also value my days off and I am looking to try and get a day later this week or next and get up to the mountains.

    Whether you think this is a good idea or not, I would hope you admit that it’s nice that the company is trying to ensure employee wellness. Last week had a number of meetings for Mental Health Awareness Week, and to cap off those efforts, we close today.

    I’ve never had a corporate closure like this as a benefit. I’m looking forward to next year and taking the day off with everyone else.

    I hope.

  • The Baseline for Flyway

    In my previous post, I set up the Flyway Desktop projects for SQL Server and PostgreSQL. I also added a table to each platform for development. In this post, I’ll look at how I let Flyway know what already exists in my system with a baseline.

    This is part of a series of working through Flyway and Flyway desktop to demo database changes. Disclosure: I work for Redgate Software

    Baseline v Flyway Baseline

    I find these terms to be slightly confusing, especially when I look at Flyway vs. Flyway Desktop. In Flyway, there is a “baseline” verb, which you can run at the CLI. This will mark the state of your database at a level and adds the flyway_schema_history table to the schema in which you are working. This baseline causes Flyway to ignore all migrations up to the baseline level.

    In my testing, when I run this on my database, it defaults to V1 for the first migration script. That’s usually fine, but as I wrote, this can cause issues.

    There is also the concept of a baseline migration, which is a Bxx script, and this contains the definitions of all the objects that already exist in your target databases. This ensures that as FW and FWD track and deploy changes, they don’t try to redeploy those migrations that are at a level lower than the baseline numbering (the xx).

    Creating a Baseline

    My development database for SQL Server looks like this:

    2023-01-25 14_27_21-Window

    There is an object in here, but it’s not in any other environment. Both Integration and QA (and the others) have no objects.

    2023-01-25 14_27_43-Window

    In this case, I don’t need a baseline script, because I want this table to deploy to the downstream databases.

    I do, however, need a baseline. I need the baseline marker in my databases to note that we have a base version. This will give me a starting point, but also ensure that FWD creates migrations that are numbered higher than my baseline.

    I’ll add this in two ways. One with Flyway Desktop and one with the Flyway CLI.

    The Flyway Baseline

    For SQL Server, I don’t need to worry about any objects in downstream databases, so I’m just going to run the Flyway CLI. From a command line, I’ll run this code:

    flyway baseline -url="jdbc:sqlserver://localhost;instanceName=SQL2022;databaseName=FWPoC_1_Dev;encrypt=true;integratedSecurity=true;trustServerCertificate=true"

    This is run from my project location, though I’m passing in the connection string from Flyway Desktop as I don’t have a flyway.conf file configured for this project. Things work from the FWD gui, but not the CLI.

    This works, and I see these results. Note the flyway schema history table is created at the bottom, and the version of the database is set to 1.

    2023-02-08 08_55_37-cmd

    Now when I run Flyway info with that URL, I get this. There is an entry in the version tracking for this table:

    2023-02-08 08_56_03-cmd

    I can also see this table in my Object Explorer:

    2023-02-08 08_54_54-SQLQuery9.sql - ARISTOTLE_SQL2022.FWPoc_3_QA (ARISTOTLE_Steve (88)) - Microsoft

    Flyway Desktop and PostgreSQL

    I’m going to use FWD for my PostgreSQL project. This will do some of the work for me and give me the option for a baseline script.

    Note: I set up a shadow database first.

    2023-01-25 14_45_24-Window

    I click “Create baseline” and this asks me for a target. After all, I’m trying to ensure I don’t deploy anything to prod that’s already there.

    2023-01-25 14_45_34-Window

    When I click Add target database, I get a connection dialog. I fill this in with the credentials for prod. This returns me to this screen below, where I see my prod database, which is at this port with this name.

    2023-01-25 14_48_12-Window

    I click Baseline and it goes to work. There’s nothing there, so this returns back to the blank, Generate Migrations tab.

    2023-01-25 14_49_02-Window

    However, there is no baseline or schema tracking table. I didn’t have a poc schema, so perhaps that’s an issue, but that’s OK. We can fix this.

    In the Migrations tab, I see this:

    2023-01-25 15_10_52-Window

    That configures this tab to look at (and work with) this database.

    2023-01-25 15_10_44-Window

    In general, I know we won’t be able to see production, but this is a PoC. However, this is something that I, in general, don’t want to do. I want to work with dev/test environments, so let’s do that.

    I’ll configure my QA environment. I click “configure target database” and I get this screen. These are all the databases for my project. Here I’m going to click “delete” for production and then I’m going to click the Add and configure my QA database. Once I do that, I’ll see this:

    2023-01-25 15_14_52-Window

    Baseline added for PostgreSQL.

    2023-02-08 08_54_02-● SQLQuery_2 - localhost.postgres (postgres) - DBAScripts - Azure Data Studio

    Success.