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:
@@ -562,12 +562,31 @@ func identKind(pkg string) identScanKind {
|
||||
}
|
||||
}
|
||||
|
||||
// migrationStampLen is the length of the timestamp that prefixes every
|
||||
// scaffolded migration file name.
|
||||
const migrationStampLen = 14
|
||||
|
||||
const migrationStampLayout = "20060102150405"
|
||||
|
||||
// nextMigrationFile picks the updates/ file for a new migration. Its
|
||||
// timestamp is now, or one second after the newest migration already in
|
||||
// updates/ when that one is not earlier than now, so the registry, which sorts
|
||||
// by file name, lists migrations in creation order even when several are made
|
||||
// within one second.
|
||||
func nextMigrationFile(pluginDir, slug string) (id, rel string, err error) {
|
||||
if err := validateTableName(slug); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
updates := filepath.Join(pluginDir, "updates")
|
||||
ts := migrationNow().Format("20060102150405")
|
||||
next := migrationNow().Truncate(time.Second)
|
||||
newest, ok, err := newestMigrationStamp(updates)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
if ok && !next.After(newest) {
|
||||
next = newest.Add(time.Second)
|
||||
}
|
||||
ts := next.Format(migrationStampLayout)
|
||||
for i := 0; i < 60; i++ {
|
||||
id = fmt.Sprintf("%s_%s", ts, slug)
|
||||
rel = filepath.Join("updates", id+".go")
|
||||
@@ -585,15 +604,45 @@ func nextMigrationFile(pluginDir, slug string) (id, rel string, err error) {
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("build: stat %s: %w", path, err)
|
||||
}
|
||||
next, parseErr := time.Parse("20060102150405", ts)
|
||||
if parseErr != nil {
|
||||
return "", "", fmt.Errorf("build: migration timestamp: %w", parseErr)
|
||||
}
|
||||
ts = next.Add(time.Second).Format("20060102150405")
|
||||
next = next.Add(time.Second)
|
||||
ts = next.Format(migrationStampLayout)
|
||||
}
|
||||
return "", "", fmt.Errorf("build: could not allocate a unique migration id for %s", slug)
|
||||
}
|
||||
|
||||
// newestMigrationStamp returns the latest timestamp among the .go files in
|
||||
// updates whose name starts with 14 digits and an underscore. Other files
|
||||
// (doc.go, tests) are ignored; a missing directory has none.
|
||||
func newestMigrationStamp(updates string) (time.Time, bool, error) {
|
||||
entries, err := os.ReadDir(updates)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return time.Time{}, false, nil
|
||||
}
|
||||
return time.Time{}, false, fmt.Errorf("build: read %s: %w", updates, err)
|
||||
}
|
||||
var newest time.Time
|
||||
found := false
|
||||
for _, entry := range entries {
|
||||
name := entry.Name()
|
||||
if entry.IsDir() || !strings.HasSuffix(name, ".go") || len(name) <= migrationStampLen || name[migrationStampLen] != '_' {
|
||||
continue
|
||||
}
|
||||
stamp := name[:migrationStampLen]
|
||||
if strings.IndexFunc(stamp, func(r rune) bool { return r < '0' || r > '9' }) >= 0 {
|
||||
continue
|
||||
}
|
||||
ts, err := time.Parse(migrationStampLayout, stamp)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if !found || ts.After(newest) {
|
||||
newest, found = ts, true
|
||||
}
|
||||
}
|
||||
return newest, found, nil
|
||||
}
|
||||
|
||||
func modelTableName(pluginID, typeName string) (string, error) {
|
||||
table := strings.ReplaceAll(pluginID, ".", "_") + "_" + pluralSnake(typeName)
|
||||
if err := validateTableName(table); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user