Skip to content

Commit 099f97d

Browse files
authored
fix(rivetkit): setup database before lifecycle hooks for c.db availability (#4494)
# 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
1 parent e89e5eb commit 099f97d

6 files changed

Lines changed: 133 additions & 83 deletions

File tree

.github/workflows/pkg-pr-new.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@ jobs:
1616
- run: pnpm install
1717
- run: pnpm build -F rivetkit -F '@rivetkit/*' -F '!@rivetkit/shared-data' -F '!@rivetkit/engine-frontend' -F '!@rivetkit/mcp-hub'
1818
- run: npx turbo build:pack-inspector -F rivetkit
19+
# Strip sourcemaps from inspector tarball to stay under pkg-pr-new 20MB limit
20+
- run: |
21+
cd rivetkit-typescript/packages/rivetkit/dist
22+
mkdir -p /tmp/inspector-repack
23+
tar xzf inspector.tar.gz -C /tmp/inspector-repack
24+
find /tmp/inspector-repack -name '*.map' -delete
25+
tar czf inspector.tar.gz -C /tmp/inspector-repack .
26+
rm -rf /tmp/inspector-repack
1927
- run: pnpm dlx pkg-pr-new publish 'shared/typescript/*' 'engine/sdks/typescript/runner/' 'engine/sdks/typescript/runner-protocol/' 'rivetkit-typescript/packages/*' --packageManager pnpm --template './examples/*'

pnpm-lock.yaml

Lines changed: 29 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rivetkit-typescript/packages/rivetkit/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@
351351
"cli-table3": "^0.6.5",
352352
"commander": "^12.1.0",
353353
"dockerode": "^4.0.9",
354+
"drizzle-orm": "^0.44.2",
354355
"eventsource": "^4.0.0",
355356
"local-pkg": "^0.5.1",
356357
"tsup": "^8.4.0",

rivetkit-typescript/packages/rivetkit/src/actor/driver.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import type { ManagerDriver } from "@/manager/driver";
44
import { type AnyConn } from "./conn/mod";
55
import type { AnyActorInstance } from "./instance/mod";
66
import type { RegistryConfig } from "@/registry/config";
7-
import type { RawDatabaseClient } from "@/db/config";
7+
import type { RawDatabaseClient, DrizzleDatabaseClient } from "@/db/config";
88
import type { ISqliteVfs } from "@rivetkit/sqlite-vfs";
9-
import { BaseSQLiteDatabase } from "drizzle-orm/sqlite-core";
109

1110
export type ActorDriverBuilder = (
1211
config: RegistryConfig,
@@ -86,7 +85,7 @@ export interface ActorDriver {
8685
*/
8786
overrideDrizzleDatabaseClient?(
8887
actorId: string,
89-
): Promise<BaseSQLiteDatabase<any, any, any, any> | undefined>;
88+
): Promise<DrizzleDatabaseClient | undefined>;
9089

9190
/**
9291
* Creates a SQLite VFS instance for creating KV-backed databases.

rivetkit-typescript/packages/rivetkit/src/actor/instance/mod.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ export class ActorInstance<
451451

452452
get db(): InferDatabaseClient<DB> {
453453
if (!this.#db) {
454+
console.trace("[DEBUG] database not loaded");
454455
throw new errors.DatabaseNotEnabled();
455456
}
456457
return this.#db;
@@ -511,6 +512,10 @@ export class ActorInstance<
511512
}
512513
}
513514

515+
// Setup database before lifecycle hooks so c.db is available in
516+
// createState, onCreate, createVars, and onWake.
517+
await this.#setupDatabase(preload);
518+
514519
// Create a write collector to batch new-actor init writes into a
515520
// single kvBatchPut.
516521
const writeCollector = new WriteCollector(actorDriver, actorId);
@@ -545,10 +550,6 @@ export class ActorInstance<
545550
await this.#measureStartup("onWakeMs", () =>
546551
this.#callOnStart(),
547552
{ pauseKvGuard: true });
548-
549-
// Setup database.
550-
await this.#setupDatabase(preload);
551-
552553
// Initialize alarms
553554
await this.#measureStartup("initAlarmsMs", () =>
554555
this.#scheduleManager.initializeAlarms(),

0 commit comments

Comments
 (0)