Running Flyway from PowerShell–A Gotcha

I ran across an interesting gotcha while trying to run a Flyway command from PowerShell. Specifically, the snapshot command and providing a parameter that is the snapshot.filename parameter.

Someone reported an issue with Snapshot, so I tried it from a command line. It worked fine for me, but a sharp Redgate developer noted that the person was running something like this from PowerShell:

flyway snapshot -snapshot.filename="deployed1.snapshot" -url="jdbc:sqlserver://localhost;databaseName=FWSimpleTalk_1_Dev;encrypt=true;integratedSecurity=true;trustServerCertificate=true"

As you can see below, this caused an error.

2023-08-28 11_52_27-Window

I checked my version. Surely “flyway snapshot” was valid, and it was.

However, there is an issue here with PowerShell and the parameter. In this case, the PoSh will try to interpret this as

The issue is lightly explained in this SO question, though I can’t quite figure out where this is documented. However, using quotes around the parameter name fixes this. Note, I switched the parameter value to single quotes.

flyway snapshot "-snapshot.filename='deployed1.snapshot'" -url="jdbc:sqlserver://localhost;databaseName=FWSimpleTalk_1_Dev;encrypt=true;integratedSecurity=true;trustServerCertificate=true"

As you can see, this works.

2023-08-28 08_25_29-Window

PowerShell is closed to command shell, but not the same. Keep that in mind as you build automation. Be wary of how your parameter values might be interpreted and test lots of values.

Unknown's avatar

About way0utwest

Editor, SQLServerCentral
This entry was posted in Blog and tagged , , . Bookmark the permalink.

3 Responses to Running Flyway from PowerShell–A Gotcha

  1. nanoDBA's avatar nanoDBA says:

    Prepending / Using the call operator & may also “force PowerShell to treat the string as a command to be executed”. I think it disables parsing of the command similar to Invoke-Expression.

    https://ss64.com/ps/call.html

    Like

  2. brianary's avatar brianary says:

    The other way to do this is to use the [“stop-parsing token”](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_parsing#the-stop-parsing-token) to prevent any PowerShell parsing of the command line before the application gets it:

    “`powershell
    flyway –% snapshot -snapshot.filename=”deployed1.snapshot” -url=”jdbc:sqlserver://localhost;databaseName=FWSimpleTalk_1_Dev;encrypt=true;integratedSecurity=true;trustServerCertificate=true”
    “`

    Like

    • brianary's avatar brianary says:

      The other way to do this is to use the “stop-parsing token” to prevent any PowerShell parsing of the command line before the application gets it:

      flyway --% snapshot -snapshot.filename="deployed1.snapshot" -url="jdbc:sqlserver://localhost;databaseName=FWSimpleTalk_1_Dev;encrypt=true;integratedSecurity=true;trustServerCertificate=true"

      Like

Comments are closed.