3
votes

I would like to get the latest schema version value of a particular database using Flyway. Is there a function in Flyway to get the current schema version number in command line?

I can run the following command:

flyway info

This gives me the entire schema content for my database (shortened) as follows:

+----------------+-------------------------------------+---------------------+---------+
| Version        | Description                         | Installed on        | State   |
+----------------+-------------------------------------+---------------------+---------+
| 1.0.1          | Create Table TRACKPATH              | 2015-11-10 08:39:36 | Success |
| 1.0.2          | Create Table TRACKGAUGE             | 2015-11-10 08:39:36 | Success |
| ...            | ...                                 | ...                 | ...     |
| 1.5.7          | Create Table FUNCTIONAL SITE        | 2015-11-10 08:40:10 | Success |
| 1.5.8          | Create Table TOPOGRAPHY AREA        | 2015-11-10 08:40:10 | Success |
| 1.5.9          | Create Table FS DETAILDEFD          | 2015-11-10 08:40:11 | Success |
+----------------+-------------------------------------+---------------------+---------+

I am only interested in the last schema entry version '1.5.9' value.

My environment is as follows:

  • Windows 7
  • Flyway 3.0
2
Why is it that you need this? It's possible to scrape this from the command line output. I can share some code to do this in Powershell if this helps?David Atkinson
Hi @DavidAtkinson, I need to use this to compare the version with another value to make sure they match within a shell script. I had thought of scraping the output but wanted to know if Flyway had a hidden feature to do it within its application. However, I would be very grateful if you could share the Powershell code, thanks.Leroy
you could also read the flyway_schema_history table where Flyway stores the version info.Dharmendar Kumar 'DK'

2 Answers

5
votes

I recently had to solve exactly this problem while building a Flyway plug-in for Octopus Deploy. (Currently waiting to be merged):

https://github.com/OctopusDeploy/Library/pull/244

If there is a way to return just the version number I couldn't find it. Instead I did as David Atkinson suggested and scraped "migrate info" to find the version number.

The following PowerShell works for me. Let me know if it solves your problem. (And yeah, there are probably nicer ways to do it than the uber-line that holds the important logic!)

# Saving target DB info
$flywayCmd = "C:\path\to\flyway.cmd"
$arguments = @(
    "info", 
    "-url=$targetUrl",
    "-user=$targetUser",
    "-password=$targetPassword"
)
Write-Host "Determining version of target database:"
Write-Host "Executing the following: & $flywayCmd $arguments"
$targetDbInfo = & $flywayCmd $arguments
Write-Host "Target DB info:"
Write-Host $targetDbInfo

# Finding intended version number of target database
$targetDbVersion = ($targetDbInfo | ? {$_.StartsWith("|") } | ? { $_ -notcontains "No migrations found" } | % { $parts = $_.Split('|'); New-Object PSObject -Property @{Version = $parts[1].Trim(); State = $parts[4].Trim()}} | ? { $_.State -eq "Success" } | Select-Object -Last 1).Version
Write-Host "Target database is at version $targetDbVersion"
0
votes

In addition to the answer provided by @Alex Yates, there might be another way through thanks to the callback concept offered by Flyway.

Callbacks allow you to hook into Flyway's lifecycle as per the official doc, using either SQL or Java callbacks.

In your case, it is possible to define a callback to the 'afterInfo' command step that catches the version and maintains it inside a variable or prints it elsewhere, to be used later on by your script.