Skip to content

feat: add db migrations reset command#8177

Merged
eduardoboucas merged 3 commits intomainfrom
feat/db-migrations-reset
Apr 21, 2026
Merged

feat: add db migrations reset command#8177
eduardoboucas merged 3 commits intomainfrom
feat/db-migrations-reset

Conversation

@eduardoboucas
Copy link
Copy Markdown
Member

No description provided.

@eduardoboucas eduardoboucas requested a review from a team as a code owner April 21, 2026 09:00
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 21, 2026

📊 Benchmark results

Comparing with 794c2e0

  • Dependency count: 1,061 (no change)
  • Package size: 355 MB ⬆️ 0.00% increase vs. 794c2e0
  • Number of ts-expect-error directives: 356 (no change)

@eduardoboucas eduardoboucas requested a review from pieh April 21, 2026 09:03
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 21, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: eefb27a9-c72f-4973-954e-29637bb3d6cf

📥 Commits

Reviewing files that changed from the base of the PR and between 3f759b1 and 19f5846.

📒 Files selected for processing (2)
  • tests/unit/commands/database/db-migrate.test.ts
  • tests/unit/commands/database/db-migrations-reset.test.ts
✅ Files skipped from review due to trivial changes (1)
  • tests/unit/commands/database/db-migrations-reset.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/unit/commands/database/db-migrate.test.ts

📝 Walkthrough

Summary by CodeRabbit

  • New Features

    • Added netlify db migrations reset with --branch for remote branches and --json for structured output
    • Migrations directory can be configured and will use a sensible default if present
    • Branch selection can fall back to an environment variable when not provided
  • Tests

    • Added and updated unit tests covering migrate fallback, branch resolution, and reset (local and remote) behaviors

Walkthrough

Adds a new netlify db migrations reset subcommand that deletes local migration files not recorded as applied, operating in local DB mode or remote branch mode (via --branch or NETLIFY_DB_BRANCH). Wires the command into the CLI and implements reset logic in a new module that discovers local migrations, fetches applied records (local DB or remote API), computes pending migrations, and removes pending files. Also adds PRODUCTION_BRANCH, makes db-migrate fall back to a default migrations directory when present, and changes db-migration-pull to prefer process.env.NETLIFY_DB_BRANCH when --branch is not provided. Tests added/updated for these behaviors.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~30 minutes

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive No pull request description was provided by the author, making it impossible to assess whether it is related to the changeset. Add a description explaining the purpose, benefits, and scope of the db migrations reset command feature.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat: add db migrations reset command' accurately summarizes the main change - adding a new database migrations reset subcommand with supporting logic.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/db-migrations-reset

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
src/commands/database/db-migrations-reset.ts (1)

19-23: Remove explanatory comments from LocalMigration.

Line 19–Line 23 narrate the type fields; please rely on field naming/type shape instead.

As per coding guidelines, "Never write comments on what the code does; make the code clean and self-explanatory instead".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/commands/database/db-migrations-reset.ts` around lines 19 - 23, Remove
the explanatory inline comments above the LocalMigration type's fields so the
interface is self-descriptive; specifically delete the two comment lines
describing the "name" and "path" fields and leave the declarations (name:
string, path: string) intact in the LocalMigration definition.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@tests/unit/commands/database/db-migrate.test.ts`:
- Around line 103-114: The assertions use literal POSIX strings which fail on
Windows; update the test to build expected paths OS-agnostically by using
path.join or path.normalize when asserting calls to mockExistsSync and
mockApplyMigrations so they match the value produced by migrate(); specifically
replace '/project/netlify/database/migrations' with path.join('/project',
'netlify', 'database', 'migrations') (or normalize the actual and expected) in
the test that invokes migrate() and checks mockExistsSync and
mockApplyMigrations.

In `@tests/unit/commands/database/db-migrations-reset.test.ts`:
- Around line 109-112: The test currently compares rmPaths to hardcoded POSIX
strings which fail on Windows; update the expectations in
db-migrations-reset.test.ts to construct platform-normalized paths using
path.resolve(...) (e.g. replace the literal
'/project/netlify/database/migrations/0003_c' and similar entries with
path.resolve('project', 'netlify', 'database', 'migrations', '0003_c')) so the
deep-equality checks for rmPaths succeed cross-platform; apply the same
normalization for the other two assertion locations referenced in the comment
(the arrays around lines 110–111, 130–131, and 220–221) and ensure path is
imported from 'path' if not already.

---

Nitpick comments:
In `@src/commands/database/db-migrations-reset.ts`:
- Around line 19-23: Remove the explanatory inline comments above the
LocalMigration type's fields so the interface is self-descriptive; specifically
delete the two comment lines describing the "name" and "path" fields and leave
the declarations (name: string, path: string) intact in the LocalMigration
definition.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3a549c32-d20a-42d2-a8b6-2eae5b00a5c9

📥 Commits

Reviewing files that changed from the base of the PR and between 794c2e0 and 4913adc.

📒 Files selected for processing (8)
  • src/commands/database/database.ts
  • src/commands/database/db-migrate.ts
  • src/commands/database/db-migration-pull.ts
  • src/commands/database/db-migrations-reset.ts
  • src/commands/database/util/constants.ts
  • tests/unit/commands/database/db-migrate.test.ts
  • tests/unit/commands/database/db-migration-pull.test.ts
  • tests/unit/commands/database/db-migrations-reset.test.ts

Comment thread tests/unit/commands/database/db-migrate.test.ts
Comment thread tests/unit/commands/database/db-migrations-reset.test.ts
Comment thread src/commands/database/db-migrations-reset.ts Outdated
Comment on lines +180 to +189
migrationsCommand
.command('reset')
.description('Delete local migration files that have not been applied yet')
.option('-b, --branch <branch>', 'Target a remote preview branch instead of the local development database')
.option('--json', 'Output result as JSON')
.action(async (options: MigrationsResetOptions, command: BaseCommand) => {
const { migrationsReset } = await import('./db-migrations-reset.js')
await migrationsReset(options, command)
})
.addExamples(['netlify db migrations reset', 'netlify db migrations reset --branch my-feature-branch'])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
migrationsCommand
.command('reset')
.description('Delete local migration files that have not been applied yet')
.option('-b, --branch <branch>', 'Target a remote preview branch instead of the local development database')
.option('--json', 'Output result as JSON')
.action(async (options: MigrationsResetOptions, command: BaseCommand) => {
const { migrationsReset } = await import('./db-migrations-reset.js')
await migrationsReset(options, command)
})
.addExamples(['netlify db migrations reset', 'netlify db migrations reset --branch my-feature-branch'])
migrationsCommand
.command('reset')
.description('Delete local migration files that have not been applied yet')
.option('-b, --branch <branch>', 'Target a remote branch instead of the local development database')
.option('--json', 'Output result as JSON')
.action(async (options: MigrationsResetOptions, command: BaseCommand) => {
const { migrationsReset } = await import('./db-migrations-reset.js')
await migrationsReset(options, command)
})
.addExamples(['netlify db migrations reset', 'netlify db migrations reset --branch my-feature-branch', 'netlify db migrations reset --branch production'])

I don't exactly suggest this (I'm struggling on exact wording) - but just to sync with the removal of the production branch check removal.

Can be done in follow up

Copy link
Copy Markdown
Contributor

@pieh pieh Apr 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking at command like migrations pull - it also seem like you can do just --branch there (and it would use production) and maybe some wording for handling those cases can be borrowed here

@eduardoboucas eduardoboucas enabled auto-merge (squash) April 21, 2026 09:52
@eduardoboucas eduardoboucas merged commit 3dd0f38 into main Apr 21, 2026
103 of 105 checks passed
@eduardoboucas eduardoboucas deleted the feat/db-migrations-reset branch April 21, 2026 10:00
eduardoboucas pushed a commit that referenced this pull request Apr 21, 2026
🤖 I have created a release *beep* *boop*
---


## [25.1.0](v25.0.1...v25.1.0)
(2026-04-21)


### Features

* add `db migrations reset` command
([#8177](#8177))
([3dd0f38](3dd0f38))
* add `db status` command
([#8173](#8173))
([9bccaf9](9bccaf9))
* Add deploy_source to CLI deploy requests (EX-2032)
([#8155](#8155))
([289933d](289933d))
* restructure db command files
([#8175](#8175))
([794c2e0](794c2e0))
* support `NETLIFY_DB_BRANCH` env var in `db status` command
([#8174](#8174))
([5647420](5647420))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: token-generator-app[bot] <82042599+token-generator-app[bot]@users.noreply.github.com>
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