Tag: Flyway

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

  • Friday Flyway Tips: Searching a Migration

    This was actually a cool tip I saw internally from one of the product managers, when trying to find specific text in a migration.

    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.

    A Large Project

    I’ve got a project in Flyway Desktop that I add to regularly. It doesn’t do anything amazing, but I keep slowly adding objects, with the idea that I want to build up a large project for showcasing different things to customers. You can look at the project here if you are interested.

    In my list of migrations, I’ve got a number of migrations, 21 to this point. It is a little hard to read names here, but I’ve named each script to make searching easier. I’ve included a create or alter, a type, and an object name. This helps me use the search box at the top of FWD, which searches migration names.

    2024-10_0002

    Most of these are relatively small scripts, one or two objects. A few are a little larger. If I select a particular one (21 as shown below), the code scrolls off the screen. Even if I expand the code listing to take up the entire screen, I can’t see all the code.

    More importantly, it can be hard to visually find something in the code. If you’ve ever edited a stored procedure of 100 or more lines, you know what I mean.

    2024-10_0003

    One of our PMs pointed out recently that you can use the standard browser CTRL+F if you’ve clicked into the script. I’ve done this below and the search widget pops up at the top of the code. I’ve entered part of an object name here, prod, and you can see this shows 6 matches.

    The first one is highlighted in a pale yellow color in the code.

    2024-10_0004

    If I click the down (or up) arrows in the search widget, the code jumps around, as it would in a browser. You can see below I’ve jumped down to the 4th occurrence, with the 2nd and 3rd highlighted in an orange(ish) color, while the 4th also has the yellow background bar.

    2024-10_0005

    Many of us would perform work on this code in an IDE (SSMS, ADS, etc.), but while managing a project or perhaps evaluating something else, we might want to quickly find something in code in FWD. Perhaps reviewing a change I’m about to commit. USing CTRL+F might be a handy feature.

    I do wish the search was more comprehensive at the top, and could dig through all the files in the project, but at least I can search within a file here. Having a good naming convention for migrations also helps me to find the high level purpose for a migration in the list.

    Flyway Enterprise

    Try Flyway Enterprise out today. If you haven’t worked with Flyway Desktop, download it today. There is a free version that organizes migrations and paid versions with many more features.

    If you use Flyway Community, download Flyway Desktop and get a GUI for your migration scripts.

    Video Walkthrough

    I made a quick video showing this as well. You can watch it below, or check out all the Flyway videos I’ve added:

  • Parsing EXE Output in PowerShell

    I saw a post internally that asked this question: Anyone have a handy powershell script testing if the installed flyway version matches a specific string?

    That seemed simple, but getting program output from PoSh wasn’t something I’ve tried. So I tackled the challenge and this is what happened.

    Getting the Output

    The first thing I wanted to do was actually figure out what the output of checking the version was from the CLI. I looked at the help and noticed a version verb. When I run that, I see a bunch of lines of output.

    2024-09_0002

    A lot of output. I need to parse a bunch of strings, and then find a line.

    My first experiment was to run this to get a file with this output.

    flyway version > fwversion.txt

    Now, let’s parse this.

    Parsing Content

    It’s been awhile since I read stuff from a file, but I know Get-Content works to read the file. What about finding a line. I saw this post with an answer that noted Select-String can be used, so I decided to try that.

    Here’s a first cut of code:

    2024-09_0003

    That didn’t work. However, with some experiments, I tried this code:

    Get-content fwversion.txt | select-string 'Edition'

    That worked.

    2024-09_0004

    Now, I’ll assign that to a variable with this code:

    $a=Get-content fwversion.txt | select-string ‘Edition’

    Next, I’ll split this string by spaces into a new variable with this:

    $b = $a -split(‘ ‘)

    Then I can evaluate the various element of $b. You can see below the first and third elements are what I’m interested in. Really the third. Remember, PoSh is zero-based.

    2024-09_0005

    That let’s me parse the output, but I don’t want to save a file. Now on to the next step.

    Capturing the Output from a Program

    One of the things I know you can do in a PoSh ptompt is run a program. The redirection operator allows you to move output. When I tried it, I couldn’t quite get the output I wanted, but I did find this post that helped. With that, I ran this code:

    $a = & "flyway" --version  2>&1 | select-string 'Edition'

    This runs Flyway, captures the output in a stream and then uses the code above to find the right line. I assign this to a variable.

    Almost there.

    Adding a Parameter and a Test

    Since I want to call this from the CLI and pass i a parameter, I added a param() clause to my script and then a test that compares the version output from the flyway.exe to the parameter. That gest me this code:

    param(
        [string]$versionToCheck=""
    )
    $a = & "flyway" --version  2>&1 | select-string 'Edition'
    $b = $a -split(' ')
    if ($b[3] -eq $versionToCheck)
    { Write-Output("$($b) installed")}
    else {

    Write-Output(“wrong version – $($b) installed”)
    }

    
    

    Now I can call this from the CLI and check things. It works well. At least for now.

    2024-09_0001

    I am certainly not a PoSh expert, but this short script took me about 15 minutes to write with a little research. Then a little testing and I sent it off to the requester. Haven’t heard any complaints, so I’m hoping this actually works for them.