feat(migrate): add lifecycle commands and standalone authoring #18

Merged
end merged 8 commits from feat/migrate-lifecycle into main 2026-10-28 16:10:40 -07:00
Showing only changes of commit 213d325363 - Show all commits
@@ -69,22 +69,22 @@ corestack-goose create --dir ./migrations <name> sql|go
corestack-goose fix --dir ./migrations
```
The directory must already exist. Use package migrations for generated Go files, names restricted to lowercase ASCII words separated by underscores, and reject duplicate generated identifiers before writes. No package option, alternate template option or database connection is needed. SQL uses Goose's supported CreateWithTemplate with its stock SQL template; Go supplies a custom template. Creation runs in a temporary staging directory before publishing validated output. Do not call SetSequential, SetLogger or any global registration helper.
The directory must already exist. Use package migrations for generated Go files, names restricted to lowercase ASCII words separated by underscores, and reject duplicate generated identifiers before writes. No package option, alternate template option or database connection is needed. SQL uses Goose's supported CreateWithTemplate with its stock SQL template; Go supplies a custom template. Creation runs in a temporary staging directory before publishing validated output. House Go filenames are `<version>_<name>_migration.go`: append the fixed `_migration` component to Goose's staged filename, leaving SQL filenames unchanged. This keeps every allowed name, including `test`, `create_test`, `linux` and `windows`, in ordinary builds without test-file or platform suffix filtering. Factory identifiers derive from the original logical name, excluding the fixed filename component; create and fix use this same convention. No build-tag support is introduced. Do not call SetSequential, SetLogger or any global registration helper.
A generated Go file declares an unexported factory `migrationExample(version int64) *goose.Migration` returning `goose.NewGoMigration(version, &goose.GoFunc{RunTx: upExample}, &goose.GoFunc{RunTx: downExample})`, plus editable transactional up/down bodies. The tool-owned `register.go` exports `GoMigrations() []*goose.Migration` returning a new slice of calls such as `migrationExample(20261028220000)`. Create maintains this aggregate even for a SQL-only set, returning an empty slice when there are no Go migrations. That aggregate is the sole explicit registration-version surface: each literal equals the corresponding filename's numeric prefix. Calling GoMigrations twice produces separate Migration and GoFunc values for provider isolation. Generated consumer files import only Goose and the standard library; applications pass the result to migrate.New. The existing embedded-directory requirement still applies, including for Go-only sets.
Fix scans SQL and supported Go migration filenames together, preserves sequential IDs, sorts timestamp IDs numerically, and allocates sequential IDs after the highest existing sequential ID, with at least five-digit padding. Follow Goose's timestamp classification (a valid UTC timestamp after the Unix epoch), reject invalid/duplicate/overflowed numeric IDs and any resulting numeric/lexical ordering disagreement. Update filenames and regenerate aggregate literals from the same mapping; keep Go factory names independent of versions. Never replace numbers in migration bodies or parse arbitrary Go registration styles. Use go/parser to validate the owned aggregate's narrow function/call/literal shape and its agreement with the current files, and go/format for new Go output. Require the owned marker and expected structure before replacing an existing register.go; refuse handwritten or drifted registration layouts with an actionable diagnostic. Editable migration bodies remain untouched. Validate generated factory identity/signature and package agreement, without attempting to understand user migration logic.
Fix scans SQL and house `<version>_<name>_migration.go` filenames together, preserves sequential IDs, sorts timestamp IDs numerically, and allocates sequential IDs after the highest existing sequential ID, with at least five-digit padding. Follow Goose's timestamp classification (a valid UTC timestamp after the Unix epoch), reject invalid/duplicate/overflowed numeric IDs and any resulting numeric/lexical ordering disagreement. Update filenames and regenerate aggregate literals from the same mapping; keep Go factory names independent of versions. Never replace numbers in migration bodies or parse arbitrary Go registration styles. Require the owned marker and agreement with the current filenames and factories before replacing an existing register.go; refuse handwritten or drifted registration layouts with an actionable diagnostic. Canonical reconstruction from those files and factories, compared after go/format, is sufficient; no separate aggregate AST grammar recognizer is required. Use go/format for new Go output and go/parser where needed to validate editable files' factory identity/signature and package agreement, without attempting to understand user migration logic. Editable migration bodies remain untouched.
Both create and fix preflight the complete planned set before mutation: regular files/directories rather than symlink paths, names, version mappings, generated identifiers, aggregate agreement, destination collisions and output syntax. New destinations are opened exclusively, including renamed migration copies; no unknown destination is overwritten. Existing register.go is intentionally replaced only when recognized as tool-owned and unchanged since preflight. Use one authoring process and no concurrent editor mutations during the operation; these checks do not establish filesystem isolation against racing writers. Fix preserves original migration bytes while changing their paths and the owned aggregate.
There is no atomic multi-file guarantee. Stage output first, publish a destination completely before removing its old source, and stop at the first write/remove failure with completed and outstanding paths in the diagnostic. Interruption can leave duplicate files, mismatched aggregate versions or temporary files; do not automatically rerun or promise rollback. Work on a clean/recoverable source checkout, inspect the reported paths and restore the affected source set from version control before retrying. Preflight must reject an inconsistent set. This bounded manual recovery avoids a persistent journal or transaction framework. Source renumbering never changes database history: run fix before those IDs reach any shared database; after deployment preserve IDs and add a new migration instead. The tool has no database access with which to prove deployment history.
There is no atomic multi-file guarantee. Stage output first, publish a destination completely before removing its old source, and stop at the first write/remove failure with completed and outstanding paths in the diagnostic. Interruption can leave duplicate files, mismatched aggregate versions or temporary files; do not automatically rerun or promise rollback. Before fix, commit or separately save/back up the complete source set, including newly created/untracked migrations and edits to tracked files; ordinary version-control restore cannot recover untracked files or unsaved changes. After failure, inspect the reported paths and restore the affected source set from that commit or backup before retrying. Preflight must reject an inconsistent set. This bounded manual recovery avoids a persistent journal or transaction framework. Source renumbering never changes database history: run fix before those IDs reach any shared database; after deployment preserve IDs and add a new migration instead. The tool has no database access with which to prove deployment history.
### Consequences
* Runtime persistence remains existing Goose schema/version history writes; new writers are development-only source creation, copies/removals and the owned aggregate replacement. Importing unused runtime code starts nothing. There is no new auth boundary, configuration service, worker, timer or dependency.
* Expected implementation is 100–200 runtime lines and 250–400 authoring lines, plus focused tests, examples and consumer docs. Touch new adapter/tool files, migrate's authoring guidance/doc comment and test fixtures; no current callers, release publication configuration or dependency changes are required. Escalate material growth, especially unsupported-source compatibility or automated crash recovery, rather than silently enlarging this PR.
* The runtime adapter should remain thin; authoring also requires staging, preflight, collision checks and bounded failure diagnostics, so its size must be assessed during implementation rather than promised as a small line count. Include focused tests, examples and consumer docs. Touch new adapter/tool files, migrate's authoring guidance/doc comment and test fixtures; no current callers, release publication configuration or dependency changes are required. Escalate material growth, especially unsupported-source compatibility or automated crash recovery, rather than silently enlarging this PR.
* Before delivery, test both startup policies and require blocking later startup; all three native commands against SQLite; setup/teardown and context/error paths; usage/help with no database setup; status output errors. Build a production fixture importing only the runtime adapter and the standalone tool separately; verify the production dependency list and binary symbols exclude this project's authoring package and file-writing entry points. Goose's own upstream authoring functions may exist in module source; prove reachable application code excludes authoring rather than claiming the dependency's source is absent.
* The decisive real flow must create SQL and Go migrations, fix timestamp IDs, compile the unedited generated registrations, apply against SQLite and verify versions, pending state and provider isolation. Also cover collisions, unsupported/drifted aggregates, duplicate IDs, mixed IDs, repeated fix and an injected partial-write failure followed by documented source restoration. Build/test both supported artifacts. These are future delivery gates, not checks performed by authoring this ADR.
* The decisive real flow must create SQL and Go migrations, fix timestamp IDs, compile the unedited generated registrations (including names `test`, `create_test` and a host-incompatible platform name such as `windows` on Linux/macOS or `linux` on Windows), apply against SQLite and verify versions, pending state and provider isolation. Also cover collisions, unsupported/drifted aggregates, duplicate IDs, mixed IDs, repeated fix and an injected partial-write failure followed by documented source restoration. Build/test both supported artifacts. These are future delivery gates, not checks performed by authoring this ADR.
* Consumer docs should recommend this standalone workflow and link Goose's concepts instead of reproducing its manual. Accepting this proposal also accepts the narrow generated layout, single-writer operation and version-control recovery boundary. Independent ADR review and explicit owner acceptance remain required; this proposed record authorizes no production implementation.
## More Information