Skip to content

fix(rivetkit-napi): run onMigrate before createState/onCreate/createVars#4785

Merged
NathanFlurry merged 1 commit intomainfrom
cleanup/on-migrate-ordering
Apr 27, 2026
Merged

fix(rivetkit-napi): run onMigrate before createState/onCreate/createVars#4785
NathanFlurry merged 1 commit intomainfrom
cleanup/on-migrate-ordering

Conversation

@NathanFlurry
Copy link
Copy Markdown
Member

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copy link
Copy Markdown
Member Author

NathanFlurry commented Apr 26, 2026

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 26, 2026

Preview packages published to npm

Install with:

npm install rivetkit@pr-4785

All packages published as 0.0.0-pr.4785.3960118 with tag pr-4785.

Engine binary is shipped via @rivetkit/engine-cli on linux-x64-musl, linux-arm64-musl, darwin-x64, and darwin-arm64. Windows users should use the release installer or set RIVET_ENGINE_BINARY.

Docker images:

docker pull rivetdev/engine:slim-3960118
docker pull rivetdev/engine:full-3960118
Individual packages
npm install rivetkit@pr-4785
npm install @rivetkit/react@pr-4785
npm install @rivetkit/rivetkit-napi@pr-4785
npm install @rivetkit/workflow-engine@pr-4785

@NathanFlurry NathanFlurry force-pushed the cleanup/lifecycle-flag-merge branch from 8def5c9 to 8de539b Compare April 27, 2026 01:06
@NathanFlurry NathanFlurry force-pushed the cleanup/on-migrate-ordering branch from d5ebf66 to 2652aa5 Compare April 27, 2026 01:06
@NathanFlurry NathanFlurry force-pushed the cleanup/lifecycle-flag-merge branch from 8de539b to c7773a5 Compare April 27, 2026 01:17
@NathanFlurry NathanFlurry force-pushed the cleanup/on-migrate-ordering branch from 2652aa5 to 33fb1d1 Compare April 27, 2026 01:17
@NathanFlurry NathanFlurry marked this pull request as ready for review April 27, 2026 01:32
@NathanFlurry NathanFlurry force-pushed the cleanup/lifecycle-flag-merge branch from c7773a5 to 8f60327 Compare April 27, 2026 01:47
@NathanFlurry NathanFlurry force-pushed the cleanup/on-migrate-ordering branch from 33fb1d1 to 547636c Compare April 27, 2026 01:47
@claude
Copy link
Copy Markdown

claude Bot commented Apr 27, 2026

PR Review: fix(rivetkit-napi): run onMigrate before createState/onCreate/createVars

Summary

This PR fixes a lifecycle ordering bug in the NAPI receive-loop preamble (run_preamble). Previously, onMigrate ran after createState, onCreate, and createVars, meaning any DB schema set up by onMigrate was unavailable in those earlier hooks. The fix moves the onMigrate call to run first, aligning the NAPI preamble with the TypeScript actor instance in mod.ts where #setupDatabase (including migration) runs before all user-facing lifecycle hooks.

Corrected preamble order:

  1. onMigrate
  2. createState / onCreate (new actors) OR state + connection restore (existing actors)
  3. createVars
  4. onWake
  5. onBeforeActorStart

Code Quality

The fix is minimal and surgical — a pure code movement with no logic changes. The comment added in the Rust file (// Run database migrations before any user lifecycle hook so c.db is usable from createState, onCreate, and createVars) correctly explains the why and follows CLAUDE.md style (complete sentence, lowercase unless mentioning code symbols).

All CLAUDE.md conventions are followed:

  • Conventional commit format ✓
  • No _ => fall-through arms ✓
  • No vi.mock or module-level mocking ✓
  • Comment style: complete sentences ✓

Potential Issues

Minor gap — wake path not tested. onMigrate now correctly runs before onWake and createVars\ on actor restart after sleep, but the test suite only exercises the new-actor path. The fix does apply to the wake path (since run_preamble is called on every startup, new or woken), but a sleep/wake test would give stronger confidence. Not a blocker.

Type assertions in fixtures. The fixture actors use as { ... } casts for c.state and c.vars. This is consistent with the existing fixture pattern and acceptable for test code.


Performance

No new per-request work. The migration callback runs once during actor startup (preamble), as before. The with_timeout("onMigrate", config.on_migrate_timeout, ...) wrapping is preserved unchanged.


Security

No trust boundary changes. This is a pure internal lifecycle ordering fix.


Test Coverage

The three new driver tests are well-structured:

  • One test per affected hook (createState, onCreate, createVars)
  • crypto.randomUUID() keys prevent cross-test contamination
  • CREATE TABLE IF NOT EXISTS makes the migration idempotent
  • Positive assertion: querying the table returns 0 (exists, no rows) vs -1 (table missing / query failed)
  • dbTestTimeout: 180_000 is consistent with other DB fixtures

Minor gap: no test covers the wake path (verifying onMigrate runs before createVars/onWake after sleep). The fix does apply there, but a follow-up test would strengthen confidence.


Verdict

Correct, well-tested, minimal fix. Approve with the optional suggestion to add a wake-path test as a follow-up.

Base automatically changed from cleanup/lifecycle-flag-merge to main April 27, 2026 07:13
@NathanFlurry NathanFlurry merged commit fa83702 into main Apr 27, 2026
8 of 27 checks passed
@NathanFlurry NathanFlurry deleted the cleanup/on-migrate-ordering branch April 27, 2026 07:13
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.

1 participant