fix(database): keep inbound migration out of live update and restore races

This commit is contained in:
Hamed Sanaei
2026-12-03 03:54:57 +03:30
parent 51f258190f
commit 8cb1e9f1df
9 changed files with 251 additions and 91 deletions
+22 -2
View File
@@ -31,7 +31,11 @@ func scriptFunction(t *testing.T, script, name string) string {
const settingsCLIMock = `#!/bin/bash
set -eu
state=$TEST_SETTINGS_DIR
if [[ "$1" == migrate ]]; then exit 0; fi
if [[ "$1" == migrate ]]; then
printf 'migrate\n' >> "$state/lifecycle"
if [[ "$TEST_READ_MODE" == migration_failure ]]; then exit 1; fi
exit 0
fi
shift
case "$1" in
-show)
@@ -145,7 +149,8 @@ fail2ban_mode=install
body := scriptFunction(t, script, parent)
startCall := function
if script == "update.sh" {
startCall = "setup_fail2ban"
startCall = `echo -e "${green}Migrating database...${plain}"`
driver += scriptFunction(t, script, "_fail")
}
start := strings.Index(body, "\n "+startCall)
if start < 0 {
@@ -277,6 +282,8 @@ func TestScriptConfigReadFailureKeepsServiceSetup(t *testing.T) {
wantLifecycle, wantServices := "fail2ban\nunit\n", "daemon-reload\nenable x-ui\nstart x-ui\n"
if script == "install.sh" {
wantServices = "daemon-reload\nenable x-ui\nrestart x-ui\n"
} else {
wantLifecycle = "migrate\n" + wantLifecycle
}
for name, want := range map[string]string{
"path": "/a/", "username": "admin", "password": "admin", "writes": "",
@@ -289,3 +296,16 @@ func TestScriptConfigReadFailureKeepsServiceSetup(t *testing.T) {
})
}
}
func TestUpdateMigrationFailurePreventsServiceStart(t *testing.T) {
dir, out, err := runScriptConfig(t, "update.sh", "/a/", false, "migration_failure", 1, true)
if err == nil || !strings.Contains(out, "Database migration failed") {
t.Fatalf("migration failure should abort the update: %v\n%s", err, out)
}
if got := readScriptState(t, dir, "restarts"); got != "" {
t.Fatalf("service started after a failed migration: %q", got)
}
if got := readScriptState(t, dir, "lifecycle"); got != "migrate\n" {
t.Fatalf("lifecycle after failed migration: %q", got)
}
}