45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
package verdict
|
|
|
|
import (
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
// Liquibase prints one UPDATE SUMMARY per changelog it ran; "Run:" is the
|
|
// number of changesets that summary applied.
|
|
liquibaseRun = regexp.MustCompile(`(?m)^Run:\s+(\d+)\s*$`)
|
|
// Flyway prints one line for the whole run.
|
|
flywayApplied = regexp.MustCompile(`Successfully applied (\d+) migrations?`)
|
|
)
|
|
|
|
// AppliedCount reads how many migrations an apply ran from the tool's own
|
|
// summary. Each tool reports it differently, and a marker that fits one tool
|
|
// counts nothing (or the wrong thing) in another: until 2026-09-27 this module
|
|
// reported 0 for every Liquibase run and 1 for every Flyway run of any size.
|
|
// An output without the tool's summary counts 0.
|
|
func AppliedCount(tool, out string) int {
|
|
count := 0
|
|
switch tool {
|
|
case "liquibase":
|
|
for _, m := range liquibaseRun.FindAllStringSubmatch(out, -1) {
|
|
n, _ := strconv.Atoi(m[1])
|
|
count += n
|
|
}
|
|
case "flyway":
|
|
for _, m := range flywayApplied.FindAllStringSubmatch(out, -1) {
|
|
n, _ := strconv.Atoi(m[1])
|
|
count += n
|
|
}
|
|
case "atlas":
|
|
// One "-- migrating version <v>" line per file it applied.
|
|
for _, line := range strings.Split(out, "\n") {
|
|
if strings.HasPrefix(strings.TrimSpace(line), "-- migrating version ") {
|
|
count++
|
|
}
|
|
}
|
|
}
|
|
return count
|
|
}
|