feat: parse version prefix from migration filenames #33
Reference in New Issue
Block a user
Delete Branch "feat/parse-migration-version"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Closes #28
Migration filenames now follow the pattern
<version>_<description>.sql(e.g.001_initial_schema.sql). The version stored inschema_migrationsis the numeric prefix only, not the full filename stem.Changes
ParseMigrationVersion()— new exported function that extracts the numeric prefix from migration filenames. Validates that the prefix is purely numeric and rejects malformed filenames (empty prefix, non-numeric characters, leading underscore).001.sql→001_initial_schema.sql— migration files can now have descriptive names while the tracked version remains001. This is safe pre-1.0.0 (no installed base).runMigrations()andApplyMigrations()now share a singleapplyMigrations()implementation, plus extractedcollectMigrations()andensureMigrationsTable()helpers.TestParseMigrationVersioncovers valid patterns (version-only, with description, multi-digit, multiple underscores) and error cases (empty, leading underscore, non-numeric, mixed alphanumeric).TestApplyMigrationsandTestApplyMigrationsIdempotentverify end-to-end migration application against an in-memory SQLite database.why are there two applyMigrations
Rework: consolidated duplicate applyMigrations
The unexported
applyMigrations()function and therunMigrations()method have been removed. There is now a singleApplyMigrations(ctx context.Context, db *sql.DB, log *slog.Logger)that serves both the production path (called fromconnect()with context and logger) and the test path (called withcontext.Background()andnillogger).Changes:
applyMigrations(ctx, db, log)standalone functionrunMigrations(ctx)method onDatabaseApplyMigrationssignature to acceptcontext.Contextand optional*slog.Loggerconnect()now callsApplyMigrations(ctx, s.db, s.log)directlydatabase,imgcache, andhandlerspackagesAll tests pass,
docker build .succeeds.Review: PR #33 — feat: parse version prefix from migration filenames
Policy Compliance
No policy violations found. Specifically:
.golangci.yml, Makefile, Dockerfile, or CI configgo fmtpasses, linter passes (0 issues)internal/database/schema/(pre-existing path; not changed by this PR)Requirements Checklist (Issue #28)
ParseMigrationVersion()extracts the numeric prefix from<version>_<description>.sqlfilenames. Validates purely numeric prefix, rejects empty prefix, leading underscore, non-numeric characters.001.sqlrenamed to001_initial_schema.sql; tracked version remains001.ApplyMigrations(ctx, db, log)replaces both the unexported standalone function and therunMigrationsmethod. All callers updated.TestParseMigrationVersion(9 cases: valid patterns + error cases),TestApplyMigrations(end-to-end against in-memory SQLite),TestApplyMigrationsIdempotent(double-apply correctness).Code Quality
collectMigrations(),ensureMigrationsTable(),ApplyMigrations()are well-separated concerns*slog.Loggerparameter with nil-safe checks — clean API for both production and test pathshandlers_test.go,stats_test.go,testutil_test.go) updated to new signatureBuild Result
docker build .PASSES — fmt-check ✅, lint (0 issues) ✅, all tests pass ✅, binary builds ✅Rebased onto
main— already up to date, build confirmed.Verdict: PASS