Skip to content

Add migration directory validation utility and CLI command#2

Merged
jalexw merged 8 commits into
mainfrom
claude/dreamy-bohr-i46XA
Jun 1, 2026
Merged

Add migration directory validation utility and CLI command#2
jalexw merged 8 commits into
mainfrom
claude/dreamy-bohr-i46XA

Conversation

@jalexw

@jalexw jalexw commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a new validateMigrationDirectory utility function and corresponding CLI command to validate the structure and integrity of Kysely migrations directories. This helps catch common issues like missing 5-digit prefixes, duplicate migration numbers (branch collisions), and missing up()/down() exports.

Key Changes

  • New utility module (src/utils/validateMigrationDirectory.ts):

    • Validates that migration files are prefixed with exactly 5 digits (e.g., 00000-my-migration.ts)
    • Detects duplicate migration numbers across files (branch collision detection)
    • Verifies each migration module exports both up() and down() functions
    • Ignores .d.ts declaration files and dotfiles
    • Returns structured validation results with error/warning levels
    • Supports optional duplicatesAsWarnings flag to downgrade duplicate number issues from errors to warnings
  • New CLI command (validate-migration-directory / validate):

    • Integrates the validation utility into the dbh CLI
    • Accepts a folder path argument and optional --duplicates-as-warnings flag
    • Outputs formatted error/warning messages and exits with appropriate status codes (0 for valid, 1 for invalid)
    • Provides user-friendly success/failure messages with file counts and issue summaries
  • Comprehensive test suite (src/tests/ValidateMigrationDirectory.test.ts):

    • Tests against bundled example migrations
    • Validates well-formed directories pass
    • Verifies detection of missing directories, empty directories, and malformed file names
    • Tests duplicate detection and the duplicatesAsWarnings option
    • Confirms validation of up()/down() function exports
    • Validates that declaration files and dotfiles are properly ignored
  • Documentation (README.md):

    • Added usage examples for the new validate-migration-directory command
    • Documented the validation checks performed and available options

Implementation Details

  • Uses dynamic import() with pathToFileURL to load and validate migration modules at runtime
  • Regex-based validation (/^(\d{5})(?:\D|$)/) ensures exactly 5-digit prefixes
  • Distinguishes between error-level issues (validation failures) and warning-level issues (configurable via options)
  • Returns comprehensive result object including validated directory path, discovered migration files, and all issues found

https://claude.ai/code/session_01V7f97zUsEZoVrpfNj1S5KQ

Adds a CLI command that validates the opinionated shape of a Kysely
migrations directory:
 - directory is non-empty
 - every file is prefixed with a 5-digit migration number
 - every module exports an up() and down() function
 - no duplicate migration numbers (branch collisions)

Exits 0 when valid, non-zero otherwise. Duplicate numbers are errors by
default but can be downgraded to warnings via --duplicates-as-warnings.

https://claude.ai/code/session_01V7f97zUsEZoVrpfNj1S5KQ
@jalexw jalexw self-assigned this Jun 1, 2026
claude added 7 commits June 1, 2026 18:58
Add a pull_request trigger so lint, unit, E2E, and CLI test jobs run on
PRs. Guard the three publish jobs (GitHub Packages, NPMJS, GHCR Docker
image) with 'if: github.event_name == "push"' so publishing only
happens on push to main.

https://claude.ai/code/session_01V7f97zUsEZoVrpfNj1S5KQ
Document the opinionated migration file format (5-digit prefix, up()/down()
exports, unique numbers) with examples using both the Kysely<any> query
builder and the sql template tag from @/sql, plus how to validate, build,
and run migrations via the dbh CLI.

https://claude.ai/code/session_01V7f97zUsEZoVrpfNj1S5KQ
Migration files must always import sql from @/sql; the build-db-migrations
step rewrites that literal specifier to the built sql.js, so external
consumers must configure the @/sql alias locally rather than importing
from @schemavaults/dbh/sql.

https://claude.ai/code/session_01V7f97zUsEZoVrpfNj1S5KQ
Reframe the database-migrations skill for consumers of the @schemavaults/dbh
package: use bunx/npx @schemavaults/dbh for all CLI commands, add a one-time
consumer setup section (local sql.ts module + @/sql tsconfig path alias), and
remove in-repo references (bun run cli, ./src/tests/example-migrations).

https://claude.ai/code/session_01V7f97zUsEZoVrpfNj1S5KQ
Most PostgreSQL drivers target Node.js rather than Bun, so apply/run
migrations with npx; validating and building (Bun's bundler) use bunx.

https://claude.ai/code/session_01V7f97zUsEZoVrpfNj1S5KQ
Minor bump for the new validate-migration-directory CLI command.

https://claude.ai/code/session_01V7f97zUsEZoVrpfNj1S5KQ
@jalexw jalexw merged commit 4397e4e into main Jun 1, 2026
9 checks passed
@jalexw jalexw deleted the claude/dreamy-bohr-i46XA branch June 1, 2026 22:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants