Flyway迁移架构版本
Flyway migration schema version
我想使用 Flyway 获取特定数据库的最新模式版本值。 Flyway 有在命令行中获取当前模式版本号的功能吗?
我可以运行以下命令:
flyway info
这为我的数据库提供了完整的模式内容(已缩短),如下所示:
+----------------+-------------------------------------+---------------------+---------+
| 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 |
+----------------+-------------------------------------+---------------------+---------+
我只对最后一个架构条目版本“1.5.9”值感兴趣。
我的环境如下:
- Windows 7
- 飞路 3.0
我最近在为 Octopus Deploy 构建 Flyway 插件时不得不解决这个问题。 (目前等待合并):
https://github.com/OctopusDeploy/Library/pull/244
如果有办法 return 只有版本号,我找不到。相反,我按照 David Atkinson 的建议做了,并抓取了 "migrate info" 以查找版本号。
以下 PowerShell 适合我。如果它能解决您的问题,请告诉我。 (是的,可能有比拥有重要逻辑的超级线路更好的方法来做到这一点!)
# 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"
除了@Alex Yates 提供的答案之外,由于 Flyway 提供的回调概念,可能还有另一种方法。
回调允许您根据 official doc 使用 SQL 或 Java 回调挂钩 Flyway 的生命周期。
在您的情况下,可以定义对 'afterInfo' 命令步骤的回调,以捕获版本并将其保存在变量中或将其打印到其他地方,以供您的脚本稍后使用。
我想使用 Flyway 获取特定数据库的最新模式版本值。 Flyway 有在命令行中获取当前模式版本号的功能吗?
我可以运行以下命令:
flyway info
这为我的数据库提供了完整的模式内容(已缩短),如下所示:
+----------------+-------------------------------------+---------------------+---------+
| 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 |
+----------------+-------------------------------------+---------------------+---------+
我只对最后一个架构条目版本“1.5.9”值感兴趣。
我的环境如下:
- Windows 7
- 飞路 3.0
我最近在为 Octopus Deploy 构建 Flyway 插件时不得不解决这个问题。 (目前等待合并):
https://github.com/OctopusDeploy/Library/pull/244
如果有办法 return 只有版本号,我找不到。相反,我按照 David Atkinson 的建议做了,并抓取了 "migrate info" 以查找版本号。
以下 PowerShell 适合我。如果它能解决您的问题,请告诉我。 (是的,可能有比拥有重要逻辑的超级线路更好的方法来做到这一点!)
# 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"
除了@Alex Yates 提供的答案之外,由于 Flyway 提供的回调概念,可能还有另一种方法。
回调允许您根据 official doc 使用 SQL 或 Java 回调挂钩 Flyway 的生命周期。
在您的情况下,可以定义对 'afterInfo' 命令步骤的回调,以捕获版本并将其保存在变量中或将其打印到其他地方,以供您的脚本稍后使用。