fix(build): order scaffolded migrations after every existing one (quick-261028-pq8)

- nextMigrationFile starts at one second past the newest updates/ timestamp
  when that is not earlier than now, so registry order is creation order
- frozen-clock unit test and a registry-order check in the scaffold test
- docs: drop the same-second caveat and the manual order check
This commit is contained in:
Jakub Zych
2026-10-28 18:49:17 +01:00
parent 017e0e9a6a
commit a941ab2394
5 changed files with 157 additions and 10 deletions

View File

@@ -8,6 +8,7 @@ import (
"strconv"
"strings"
"testing"
"time"
"git.golem15.com/golem15/summercms/modules/cabana"
"git.golem15.com/golem15/summercms/modules/pact"
@@ -539,6 +540,9 @@ func TestScaffoldPluginSmoke(t *testing.T) {
}
func TestScaffoldCoreArtifacts(t *testing.T) {
// One frozen second for make:model and make:migration: the registry must
// still list CreateAlbums before AddWidgetIndex.
freezeMigrationClock(t, time.Date(2026, 10, 28, 12, 0, 0, 0, time.UTC))
dir := copyHelloApp(t)
ctx := t.Context()
pluginDir, err := MakePlugin(ctx, dir, "golem15.demo")
@@ -655,6 +659,9 @@ func TestScaffoldCoreArtifacts(t *testing.T) {
t.Fatalf("registry.gen.go missing %s:\n%s", want, reg1)
}
}
if create, add := bytes.Index(reg1, []byte("updates.CreateAlbums()")), bytes.Index(reg1, []byte("updates.AddWidgetIndex()")); create > add {
t.Fatalf("registry.gen.go lists AddWidgetIndex before CreateAlbums:\n%s", reg1)
}
pluginAfter, err := os.ReadFile(filepath.Join(pluginDir, "plugin.go"))
if err != nil {
@@ -1325,3 +1332,95 @@ func write(t *testing.T, path, body string) {
t.Fatal(err)
}
}
// freezeMigrationClock pins migrationNow to at for the rest of the test.
func freezeMigrationClock(t *testing.T, at time.Time) {
t.Helper()
prev := migrationNow
migrationNow = func() time.Time { return at }
t.Cleanup(func() { migrationNow = prev })
}
// TestNextMigrationFileSortsAfterExisting: a new migration's timestamp is
// later than every migration already in updates/, so file-name order is
// creation order even within one second.
func TestNextMigrationFileSortsAfterExisting(t *testing.T) {
now := time.Date(2026, 10, 28, 12, 0, 0, 0, time.UTC)
t.Run("same second", func(t *testing.T) {
freezeMigrationClock(t, now)
dir := t.TempDir()
firstID, firstRel, err := nextMigrationFile(dir, "create_widgets")
if err != nil {
t.Fatal(err)
}
if firstID != "20261028120000_create_widgets" {
t.Fatalf("first id = %s", firstID)
}
write(t, filepath.Join(dir, firstRel), "package updates\n")
secondID, secondRel, err := nextMigrationFile(dir, "add_index")
if err != nil {
t.Fatal(err)
}
if secondID != "20261028120001_add_index" {
t.Fatalf("second id = %s", secondID)
}
if !(firstRel < secondRel) {
t.Fatalf("%s does not sort before %s", firstRel, secondRel)
}
})
t.Run("existing later than now", func(t *testing.T) {
freezeMigrationClock(t, now)
dir := t.TempDir()
updates := filepath.Join(dir, "updates")
if err := os.MkdirAll(updates, 0o755); err != nil {
t.Fatal(err)
}
write(t, filepath.Join(updates, "20990101000000_x.go"), "package updates\n")
write(t, filepath.Join(updates, "20200101000000_old.go"), "package updates\n")
id, _, err := nextMigrationFile(dir, "next")
if err != nil {
t.Fatal(err)
}
if id != "20990101000001_next" {
t.Fatalf("id = %s, want 20990101000001_next", id)
}
})
t.Run("non-migration files ignored", func(t *testing.T) {
freezeMigrationClock(t, now)
dir := t.TempDir()
updates := filepath.Join(dir, "updates")
if err := os.MkdirAll(updates, 0o755); err != nil {
t.Fatal(err)
}
for _, name := range []string{"doc.go", "99990101000000x.go", "migrations_test.go", "20990101000000_notes.txt", "2099010100000a_bad.go"} {
write(t, filepath.Join(updates, name), "package updates\n")
}
id, _, err := nextMigrationFile(dir, "plain")
if err != nil {
t.Fatal(err)
}
if id != "20261028120000_plain" {
t.Fatalf("id = %s, want the frozen now", id)
}
})
t.Run("older existing keeps now", func(t *testing.T) {
freezeMigrationClock(t, now)
dir := t.TempDir()
updates := filepath.Join(dir, "updates")
if err := os.MkdirAll(updates, 0o755); err != nil {
t.Fatal(err)
}
write(t, filepath.Join(updates, "20200101000000_old.go"), "package updates\n")
id, _, err := nextMigrationFile(dir, "plain")
if err != nil {
t.Fatal(err)
}
if id != "20261028120000_plain" {
t.Fatalf("id = %s, want the frozen now", id)
}
})
}