Tag: Flyway

  • Friday Flyway Tips: State-based deployment with Flyway

    I was asked about state-based deployments in Flyway, so I decided to show how this can work with a quick demo. This post walks through the process.

    I’ve been working with Flyway and 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.

    A Steady State

    I have a Flyway project setup already. I’ve refreshed the screen and as you can see here, there aren’t any differences between my database and my project in version control.

    2025-02_0349

    In SSMS, I’ll create a new table in my FWState_1_Dev database. This is where my project is pointed in Flyway Desktop (FWD).

    2025-02_0350

    I’ve created a new table and it exists. If I  check my QA database, FWSTate_3_QA, I don’t see this object.

    2025-02_0351

    If I refresh my project in Flyway Desktop (FWD), I’ll see my change. I can pick it and see the code, and then I’ll click “Save to project” in the upper right.

    2025-03_0106

    Once the code is saved, I see the confirmation and I have the code ready for commit to a VCS. I won’t do that here, though you should.

    2025-03_0107

    What I want to do now is go to the left bar and select “deploy”, which is the rocket icon.

    2025-03_0108

    When I do this, I see a blank screen, as I don’t have a target. I can select that on the right if I’ve configured one.

    2025-03_0109

    If I pick the drop down, you will see my dev and QA environments. I made changes in the dev environment, and want these changes to go to QA. I can also click the “Manage environments” button. Let’s do that.

    2025-03_0110

    When I do that, I see a list of environments. I use this a lot to double check that I’m deploying to the right place for this project and where to look for changes. If you work across multiple projects as I do, this is handy.

    You can see below I’ve selected QA with the green checkmack in the radio button. Once you pick your environment, you can click “Confirm”. If you need a new target, click Configure.

    2025-03_0111

    I return to the deploy screen, and this time I see the changes that have not been deployed. In this case, that’s one change, but if there were more, I would see them. I can click any object to see the changes, and once I’ve selected what to deploy, I can click deploy.

    2025-03_0113

    When I click Deploy the script is generated and I see a preview. I also can see that this is from my schema model on disk being deployed to the Qa database. That’s handy as this might take a few minutes and who knows what distractions might appear in my life.

    I also can execute this as a transaction or not, and copy the script if I want to examine it in another tool (or format it like SQL Prompt).

    2025-03_0114

    When I click Deploy, I need to confirm my deployment. Always a good step, though I think if you do this often, your muscle memory will be ready to just click confirm.

    2025-03_0115

    Once the deployment runs, we see a message this was successful.

    2025-03_0116

    If I now check the QA database, I see the object.

    2025-03_0117

    Summary

    This post has shown how you can perform manual deployments of changes in a state-based method using Flyway. This is a quick way to move your changes from one machine to another.

    Flyway can do much more, and for smoother automation, check out Flyway Enterprise.

    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

    Here is a video of the process below.

  • 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.

  • Friday Flyway Tips: Chaining Commands for State-based Deployments

    One of the cool things I’ve seen with the new Flyway CLI is that I can combine multiple actions together in one call, which can make the process of writing automations streamlined.

    I’m not sure I love this, but I’ll show how this works.

    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.

    Making a State-Based Deployment from the CLI

    In a previous post, I showed how to use the new prepare and deploy verbs to create a deployment script and then apply it to a database. This is a great way to handle simpler deployments if you want to work in a state-based fashion with Flyway.

    That post showed two different CLI calls, which are shown below:

    flyway prepare -prepare.source=schemaModel -prepare.target="env:qa" -prepare.scriptFilename="deployments\FWState__deployment.sql"

    flyway deploy -scriptFilename="deployments\FWState__deployment.sql" -environment=qa -executeInTransaction=true

    Easy enough, and I like this structure as I’d likely build a pipeline that looks like this, where I prepare the script, then have a manual approval step to check this before deployment:

    2024-12_0228

    However, you might argue that someone would check the script in a QA or other deployment, so maybe you streamline things later, or you use this as a QA/test deployment and want everything to run smoothly after a PR.

    In that case …

    Chaining Commands

    I can run this code instead.

    flyway prepare deploy …

    Let’s see this work. If you look, I have a couple of changes that I can see in Flyway Desktop. There is a table alter and a new stored procedure.

    2024-12_0232

    I can streamline the automation of these two changes in the CLI with the code style above with this code:

    flyway prepare deploy -prepare.source=schemaModel -prepare.target=”env:qa” -environment=qa -executeInTransaction=true

    Here is the pre-execution look at my database:

    2024-12_0233

    The execution, which didn’t use a transaction. I’ll have to figure that one out:

    2024-12_0234

    And the post database view with the table and proc changes deployed.

    2024-12_0235

    I built and deployed the script in one command, and it also appeared to have added my script to the deployments folder. No idea why that happened either.

    2024-12_0236

    If you want commands chained, you can do it.

    Summary

    This post showed how you can chain your prepare and deploy commands together in one command, which might be preferable in some situations.

    I don’t know if I like this, or would do it, but it’s not a bad idea. Ultimately, I’d really like this deployment script to be saved back to the repo so I could track it, but that’s going to be some work, and likely that means not chaining commands.

    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.

  • Using Flyway Prepare for State-Based Deployments

    One of the neat enhancements made to Flyway was the addition of state-based workflows and tooling. A lot of people have loved SQL Compare or SQL Source Control for deployments. As I’ve worked with customers, they’ve asked for this, and the v11 version of Flyway includes this for the Enterprise edition.

    This post will look at using these two commands to deploy a change to a database from the command line instead of Flyway Desktop.

    If you want to know more about Flyway, all my posts are together in one feed.

    My Current Environment

    I have Flway v11.0 installed and I have two databases: FWState_1_Dev and FWState_3_QA. These two databases are in my flyway.toml file, where I see them configured as development and qa.

    2024-12_0208

    I also created a “deployments” folder under my project, which is where I want deployment scripts. I’ll do this manually, but in a pipeline, I’d do this in an automated fashion. As you can see, this is an empty folder.

    2024-12_0209

    In Flyway Desktop (FWD), you can see that I have a change made in my dev db that’s ready to deploy. I could click “deploy” and run this from FWD, but I want to experiment with automation here, so I’m practicing at the command line.

    2024-12_0203

    I can verify this change isn’t deployed by checking the database.

    2024-12_0210

    Let’s get started.

    Flyway Prepare

    The prepare command is designed to create the deployment script. It can use a variety of sources, but in this case, I’ll use the schema model that I’ve saved in my git repo. This is where I (in general) want to pull from, using PRs and branches to manage work. In this case, I’ll pull from main, where I’ve got that change.

    To get my changes, I’ll use a few parameters with Flyway. One thing to note, each of these parameters includes a namespace. I think this will go away at some point, but it’s good to be explicit in any case as we have lots of “source” parameters in different namespaces.

    Second, use a single hyphen. That somewhat offends my unix/linux’y self, as the parameter names are more than one character. I’m used to 2 hyphens, which generate an error in Flyway.

    2024-12_0211

    The basic parameters I’m using are:

    • prepare.source – the location of my source. In this case, the schemaModel defined in my flyway.toml file as “schema-model” in the file system.
    • prepare-target – the target to look at to build the script. For me this is an environment preconfigured (see above) as qa, so “env:qa”
    • prepare.scriptFilename – the default is D__deployment.sql, but I want it in a subfolder, so I’ll explicitly put this in.

    My CLI call is this:

    flyway prepare -prepare.source=schemaModel -prepare.target=”env:qa” -prepare.scriptFilename=”deployments\FWState__deployment.sql”

    When I run this in my project folder, I see these results:

    2024-12_0212

    If I look in my deployments folder, I see the file:

    2024-12_0213

    The contents of the file are here:

    2024-12_0214

    This looks like what I’d get in FWD if I clicked “Deploy”.

    2024-12_0204

    This step just created the file. I need another call to deploy this.

    Flyway Deploy

    The Flyway Deploy command will execute a deployment script against a target. The idea here is that one script is created from a state-based project and deployed with this command.

    As with prepare, I’ll use a few parameters here. The ones I need are:

    • scriptFilename – the file to deploy. I’ll use the same one as above.
    • environment – the name of the environment where I am deploying, in this case, no need for the prefix.
    • executeInTransaction – this should default to true, but I’ll be explicit as I might change this in different platforms (postgreSQL, Oracle, etc.) and so if someone copies this pipeline, I want this to be clear.

    This gives me this command:

    flyway deploy -scriptFilename=”deployments\FWState__deployment.sql” -environment=qa -executeInTransaction=true

    When I run this, it works.

    2024-12_0215

    I can verify this in SSMS.

    2024-12_0216

    I’ve deployed changes, and if I look at the FWD deploy tab, I see no changes.

    2024-12_0217

    I could put these two commands in a pipeline and have them work in an automated fashion.

    If you do that, use variables in your pipeline, or ensure people commit (and merge) changes to the flyway.toml file that is used for the various options.

    Summary

    This post showed a quick way to start automated state-based deployments in Flyway, using the new prepare and deploy commands. A few options were used to control these deployments from a command line.

    There are more options that you can include to control deployments and I’ll look at some other ways of doing this in the future. For now, this gets my deployments working easily.

    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.