Tag: DevOps

  • Friday Flyway Tips: Autopilot in 10 minutes

    The Solutions Engineers at Redgate recently released an Introduction to Redgate Flyway Autopilot course on our Redgate University. They’ve been working on this for quite some time to help people get started with Flyway in their own environment. It’s gotten smooth and slick, so I’m going to set this up in 10 minutes in this post and video, but with a twist. I’m using my schema to show you how easy this is.

    I’ve been working with Flyway Desktop for work more and more as we transition from older SSMS plugins to the standalone tool. This series looks at some tips I’ve gotten along the way.

    Getting Started

    The course walks you through a few things. These include:

    • Getting Git
    • Installing Flyway Desktop
    • Having an Azure DevOps or GitHub account
    • Having a SQL Server or PostgreSQL server (I’ll use SQL Server)

    You will also get a Redgate Token and set up a local runner. I won’t detail those steps here, but I will have them in another post. The video will also skip those steps.

    Creating a Repository

    I’m working in GitHub, but you can do this in Azure DevOps. Others work, but those aren’t in the course. The main thing to do is go to the official Redgate repo at: https://github.com/red-gate/Flyway-AutoPilot-FastTrack

    This brings you to this site:

    2025-01_0099

    From there, don’t clone or fork, but use as a template. This is in the upper right corner.

    2025-01_0100

    When you click this, you get the Create a new repository page, that looks like this. If you’re familiar with GitHub, this looks like any other repo. Give it a name, which must be unique in your org. I added FWAutopilot as I already have an “Autopilot” repo that is public.

    2025-01_0102

    You can make this public or private, but just be aware of this from the standpoint of your org, especially if you add internal schemas. You can also set a description.

    Once this is created, you’ll see the repo in your org. Here’s my Autopilot repo:

    2025-01_0103

    This is a copy of the template all set up. Now, on to Flyway Desktop.

    Creating a Project

    In Flyway Desktop, I’ll click the drop down by Open Project and select Open from Version Control.

    2025-01_0110

    Here I’ll paste in the URL of my repo. I also check that the folder for the local clone is valid. In my case, I tend to put things in Documents/Git, but you might have your own standard.

    2025-01_0136

    Once this clones down, you can see a repo in the path above that looks like the online repo. Flyway Desktop will also refresh the schema, which should give you this error.

    2025-01_0210

    This is because the databases don’t exist. As you can see, I’ve filtered to databases with “auto” in the name and I have nothing.

    2025-01_0207

    If I use the file | open in SSMS, I can go into the repo and into the Scripts folder, where I see this:

    2025-01_0219

    I want to open the CreateAutoPilotDatabases.sql script, which looks like what you see below. This creates 5 databases and adds schema objects to one of them. The goal is for Autopilot to use Database DevOps and Flyway to migrate these changes to other databases.

    2025-01_0220

    Run this, and I see different databases. I’ve refreshed things, and you can see Prod has nothing but Dev has objects.

    2025-01_0221

    Now that I have a db, let’s refresh Flyway Desktop. Now I see no changes.

    2025-01_0222

    Note: If you aren’t doing this on your localhost instance, then you can edit the connections to the dev database (and other databases).

    Adding Our Own Schema

    Don’t start deleting schema objects yet, but you can add your own. I’ll do that. I have a script that contains a schema for baseball data. The beginning is shown below, but I’ll run this in my AutopilotDev database.

    2025-01_0223

    After I do this, I’ll refresh Flyway Desktop again and I see my tables. This is a partial list as the full list scrolls off the screen.

    2025-01_0224

    I’ll select all these from the checkbox at the top next to Object Name and then click “Save to project” in the upper right. This writes the CREATE scripts for all objects to the schema model, as you can see below in the update message.

    2025-01_0225

    The next step (shown at the bottom) is to generate a migration script. I’ll click that.

    I get the screen below, which shows me all the changes that have been made to objects. I can select one or more of these to put into a migration script (deployment script). If I don’t select them all, then I will see those I haven’t selected re-appear here and I can add them to a different script. To keep this simple, I’ve selected them all.

    2025-01_0226

    When I click “Generate script” in the upper right, Flyway will create a script containing all the objects I’ve selected with the appropriate create/alter/drop code inside. Here is  my one large script. You can see the start of the script below. If we scrolled, we’d see the CREATE for all the tables in here.

    2025-01_0227

    This project is configured to automatically generate an undo script, so below the above part, there is the undo script. Again, this is just the beginning of the script. However, you can see before we drop tables, we need to remove constraints.

    2025-01_0228

    Once I’m happy, I can click save and this is written as a migration script (and an undo script).

    2025-01_0229

    I can click Verify, which essentially runs these scripts against my shadow database, but I don’t do that if I haven’t altered the scripts. You can if you want.

    Now that we’ve made some changes, let’s commit those. On the right side of Flyway Desktop is the VCS blade. You can see I have 28 changes in my repo.

    2025-01_0230

    If I click the “28”, this opens to the commit tab. I can also click the arrow at the top and select the commit tab. In here, I see my changes and I can include all of them or some of them and write a commit message.

    2025-01_0231

    I’ve selected them all and written a message, so I’ll click the drop down by commit and select the combined Commit and Push.

    2025-01_0232

    If I check my repo, I see this commit included. You can see this altered the schema-model and migrations folders.

    2025-01_0233

    Now we need to keep this Database DevOps flow going and deploy our code.

    Setting Up Runners

    If I check the Actions tab in my repo, I see there are two workflows configured. They are the same, but one works for Windows and one for Linux. I don’t have any runs yet and I haven’t configured a runner.

    2025-01_0234

    If I go to Settings in my repo and the Actions | Runners area, I’ll see this. The runners are the agents that execute your code. In this case, I need to setup a new one. I’ll detail that in another post.

    2025-01_0235

    Once I have a runner set up, I should see something like this:

    2025-01_0251

    Adding Secrets

    Flyway is a licensed product, so I need to tell the runner that it is licensed to use the product. If you don’t have a license, this system can get you a 28-day trial, but if you have one, you can just use that.

    If you go to the token section of the Redgate portal, you should see something like this:

    2025-01_0242_thumb1

    If you click New Token, you get a new token.

    Note: I’ve deleted this token, so this code doesn’t work.

    2025-01_0243_thumb1

    Don’t close this, but open a new browser tab for your repo. Go to the Secrets | Actions section under Settings. You should see this. Click New repository secret.

    2025-01_0244_thumb

    The documentation notes you need to add two secrets: FLYWAY_TOKEN and FLYWAY_EMAIL. These are essentially secret variables picked up by the automation. When I click new, I add the email like this.

    2025-01_0245_thumb1

    I added the token in the same way, pasting in the token from the portal. When I finished, I see two secrets.

    2025-01_0246_thumb1

    Run the Automation

    Check your production database (and the test one). There should be no objects, which is what we saw above.

    Now, go to the Actions section of your repo. Click the Windows workflow on the left (or Linux if you used that).

    2025-01_0247_thumb1

    Now on the right, click Run workflow, and then Run again in the pop up.

    2025-01_0248_thumb1

    In a minute, your web page should show this running with a yellow circle before the name.

    2025-01_0249_thumb1

    Your CLI window should look like this as well, with a job running.

    2025-01_0250_thumb1

    If you click then name of the run on the web page, you should then see the three tasks. Here my build completed before I could get the screenshot, but yours likely has the yellow on the build database.

    2025-01_0251_thumb1

    If I click any of the tasks, I’ll see the logging as they run. In this shot below, I’ve clicked the running prod deploy, as that was running when I was ready for the screen shot.

    2025-01_0252_thumb1

    The output scrolls along and can be hart to follow, but after any of these are complete, you can click on them and see the task outline. Each of these items below can be expanded by clicking on the angle bracket. You can see I’ve expanded the Migrate Test DB task.

    2025-01_0254_thumb1

    However, most of the time we assume we have a repeatable, reliable execution of our migration, so we don’t care. The proof is in checking the databases.

    Here is my refreshed AutoPilotTest database.

    2025-01_0255_thumb1

    and here is the AutopilotProd database.

    2025-01_0256_thumb1

    I moved code from dev –> VCS –> test –> prod without executing it anywhere past Dev. This is the way changes should be made to test them before they hit prod.

    We should also have feature branches, PRs, and more, but that’s beyond the 10 minutes to get started. From here, I could easily make other changes in dev and get them deployed by clicking a button in GitHub.

    Summary

    This process took me ten minutes. To be fair, I’d tested it a few times, but in knowing what things are needed in the docs, it took me ten minutes, which I show in a video below.

    Flyway is an incredible way of deploying changes from one database to another, and now includes both migration-based and state-based deployments. You get the flexibility you need to control database changes in your environment. If you’ve never used it, give it a try today. It works for SQL Server, Oracle, PostgreSQL and nearly 50 other platforms.

    Video Walkthrough

    I’ve got a video of me doing this in 10 minutes.

  • Denver Dev Day Oct 2024 Slides

    Here are the slides from my talk today: CI in Azure DevOps

    If you have questions, please feel free to contact me (top menu above).

  • Creating a Test Pipeline in Azure DevOps using a Windows Agent

    A customer was having some trouble getting started with Azure DevOps (AzDO) and building their database, so we took a step back and decided to create a simple test pipeline, so they could get a feel for how things work and then move on to more complex builds.

    This post looks at a basic pipeline on a Windows agent. I assume you have an Azure DevOps account and have created a project.

    This post is part of a series on Azure DevOps. You can click the link to see other posts.

    Setup

    We decided to start with a very simple test to start, using a task to get a directory listing. That’s a nice simple task, and one you can re-use a lot as you try to configure your pipeline.

    We created a new pipeline in our Azure DevOps project. To do this, we went to the Pipelines section of AzDO and selected “New”.

    2024-08_0004

    We next had a screen that showed choices. If you pick a repo, this wants to build a code-first, YAML pipeline. Great for coding and automation, bad for beginners. Select “Use the classic editor” at the bottom. Don’t worry, you can always move to YAML.

    2024-08_0005

    This takes us to a repository screen. We were working in Azure DevOps, so we can use Azure Repos, but if you used GitHub or some other Git repo on the Internet, you can choose that.

    You can choose to build from other branches, but I tend to start with main, though we often protect main to prevent direct commits. Once you select the repo and branch, click Continue.

    2024-08_0007

    We now get to choose a template. For teaching someone new, I Start with an empty one. Click Empty job to move on.

    2024-08_0008

    This gets us a basic pipeline. To orient you, there are multiple tabs, Tasks, Variables, Triggers, Options, History. We don’t need any of these to get started. The Tasks item is highlighted, so we see our tasks.

    The pipeline is the top level element, and most listed ot the left. The “Get Sources, is next, and is part of the pipeline as it downloads the repo. The Agent Job 1 is the container for a series of steps in our task, like a SQL Agent Job that contains steps. We’ll add different steps below here.

    2024-08_0009

    If you look to the right, you see some edit boxes. These are the top level pipeline settings. The Name is shown at the very top of the image, near the breadcrumb. If I edit this to say “Basic Directory” then this appears at the top.

    2024-08_0010

    The Agent pool is the list of possible agents. This defaults to using the Azure hosted agents from Microsoft, but you can create local agent pools if you want things to run inside your infrastructure on VMs instead. I’ll leave this, and I’ll also leave the Agent spec to Windows 2019. There are other options, which you can see.

    2024-08_0011

    Note, I am not a fan of using Latest agents. Pick a version that reduce debugging issues. Change this to a later version periodically when you are ready.

    If you click on the Agent job 1, you see some different values on the right. You can rename this agent, or specify a different agent from the pipeline. We’ll leave this alone.

    2024-08_0012

    On the line with Agent job 1, there is a + to the right. This is how we add tasks (steps) under our agent. Click this.

    2024-08_0013

    This brings up a list of tasks to the right. There are a lot of ones listed, and you can add more to your account. We’ll live with this list.

    2024-08_0014

    We could scroll or click the various categories (build, utility, test, etc.), but let’s search. Type “cmd” in the search box. We see a limited list of tasks. We’ll click Add next to the Command Line Script.

    2024-08_0015

    Once this chosen, it is added to the left under Agent job 1. Click this line to see the various settings on the right.

    2024-08_0017

    Let’s configure this. First, we’ll change the name to directory listing. Next, let’s delete everything in the script box and type “dir”. We should see this.

    2024-08_0018

    Now click “Save and queue” at the top. We get to enter a save comment and can configure the run. Just leave the defaults.

    Note: I grabbed this shot, and realized I’d accidentally set the agent to MacOS. I reset that before I continued.

    2024-08_0019

    Once we click Save and run, we should go to the screen that has meta data about this pipeline run. It looks like this. Click the line with the clue clock and Agent job 1.

    2024-08_0020

    That should bring up a log listing of the various steps. The listing on the left matches our pipeline steps. Whichever item is selected on the left filters the logs on the right to that info.

    2024-08_0021

    If we click the directory listing step, we see some info logged and then the command being run.

    2024-08_0022

    This directory listing matches what’s in our repo. We can see this easily by clicking on the repo. 2024-08_0024

    When the agent runs, the software runs inside the folder of the repo that is downloaded as part of the pipeline. If you want to get another folder listing, useful sometimes as you configure things, just change the dir command.

    Summary

    This is just a basic look at a pipeline and how to set up a simple task. It’s a useful task, and until you have your pipeline working, I’d leave this task in there. Being able to get visibility into the agent machine is often very helpful.

    I’ll look at a few other options in future posts.

  • SQL Saturday Denver 2024 Resources

    Here are the resources from my talks today.

    Best Practices for Seamless Database Deployments

    Architecting Zero Downtime Deployments

    Some good questions today, which I’ll write a few posts on:

    • What are feature flags
    • How do you handle adding a clustered index
    • How do you communicate with downstream groups on something like a rename

    Probably a few more, but it’s been a long day and I’m tired.