test: add db seeding script#350
Conversation
📝 WalkthroughWalkthroughThis PR adds a complete database seeding system for SQLite, enabling deterministic, reproducible population of story data. It introduces a ChangesDatabase Seeding System
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install timed out. The project may have too many dependencies for the sandbox. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
This pull request introduces a database seeding script and a comprehensive mock dataset to populate the application's database. It adds a new db:seed script to package.json, defines structured seed data for stories, branches, chapters, and entities in scripts/seed/dataset.ts, and implements the execution logic in scripts/seed/index.ts using node:sqlite and Drizzle ORM. Feedback on the changes highlights a critical bug in scripts/seed/index.ts where the DatabaseSync constructor is passed an invalid option allowExtension instead of enableLoadExtension, which will cause a runtime TypeError when loading the sqlite-vec extension.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| mkdirSync(dirname(dbPath), { recursive: true }) | ||
| console.log(`[seed] target DB: ${dbPath}`) | ||
|
|
||
| const sqlite = new DatabaseSync(dbPath, { allowExtension: true }) |
There was a problem hiding this comment.
In Node.js node:sqlite, the option to allow loading extensions in the DatabaseSync constructor is enableLoadExtension, not allowExtension. Using allowExtension will cause the subsequent call to sqlite.enableLoadExtension(true) to throw a TypeError, silently failing to load the sqlite-vec extension.
| const sqlite = new DatabaseSync(dbPath, { allowExtension: true }) | |
| const sqlite = new DatabaseSync(dbPath, { enableLoadExtension: true }) |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@scripts/seed/dataset.ts`:
- Around line 1209-1219: The filler rows currently assign content from
REPLY_LINES for all non-opening entries, so entries where kind === 'user_action'
get AI-reply prose; update the entryRows.push call in the dataset generator to
choose content based on kind: if kind === 'user_action' pick from
ACTION_LINES[(fi + i) % ACTION_LINES.length], otherwise (for 'ai_reply') pick
from REPLY_LINES[(fi + i) % REPLY_LINES.length]; keep the existing opening
branch that uses f.title/f.description and preserve id generation via
entryId(f.key, i), branchId, and position fields.
In `@scripts/seed/index.ts`:
- Around line 46-52: The catch block leaves extension loading enabled if
sqlite.loadExtension(...) throws; change the try/catch to a try/finally (or add
a finally) so that sqlite.enableLoadExtension(false) is always called regardless
of success or error, keeping the current warning behavior in the catch (or
rethrow after finally) and ensuring sqlite.enableLoadExtension(false) runs;
update the block around sqlite.enableLoadExtension(true) /
sqlite.loadExtension(getLoadablePath()) / sqlite.enableLoadExtension(false)
accordingly.
🪄 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 Plus
Run ID: f27fd23f-a18b-401c-9977-cada63470fd8
📒 Files selected for processing (3)
package.jsonscripts/seed/dataset.tsscripts/seed/index.ts
| const kind: NewStoryEntry['kind'] = | ||
| i === 1 ? 'opening' : i % 2 === 0 ? 'user_action' : 'ai_reply' | ||
| entryRows.push({ | ||
| id: entryId(f.key, i), | ||
| branchId, | ||
| position: i, | ||
| kind, | ||
| content: | ||
| i === 1 | ||
| ? `[${f.title}] ${f.description ?? 'An opening scene.'}` | ||
| : `${REPLY_LINES[(fi + i) % REPLY_LINES.length]}`, |
There was a problem hiding this comment.
Populate filler user_action rows with action text.
Line 1216 currently feeds every non-opening filler entry from REPLY_LINES, so rows marked kind === 'user_action' are seeded with AI-reply prose instead of user-action text. That makes the dataset less representative for any UI or tests that branch on entry kind.
Suggested fix
entryRows.push({
id: entryId(f.key, i),
branchId,
position: i,
kind,
content:
i === 1
? `[${f.title}] ${f.description ?? 'An opening scene.'}`
- : `${REPLY_LINES[(fi + i) % REPLY_LINES.length]}`,
+ : kind === 'user_action'
+ ? ACTION_LINES[(fi + i) % ACTION_LINES.length]
+ : `${REPLY_LINES[(fi + i) % REPLY_LINES.length]} ${REPLY_BEATS[(fi + i) % REPLY_BEATS.length]}`,
chapterId: null,
metadata: entryMetadataSchema.parse({🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@scripts/seed/dataset.ts` around lines 1209 - 1219, The filler rows currently
assign content from REPLY_LINES for all non-opening entries, so entries where
kind === 'user_action' get AI-reply prose; update the entryRows.push call in the
dataset generator to choose content based on kind: if kind === 'user_action'
pick from ACTION_LINES[(fi + i) % ACTION_LINES.length], otherwise (for
'ai_reply') pick from REPLY_LINES[(fi + i) % REPLY_LINES.length]; keep the
existing opening branch that uses f.title/f.description and preserve id
generation via entryId(f.key, i), branchId, and position fields.
| sqlite.enableLoadExtension(true) | ||
| sqlite.loadExtension(getLoadablePath()) | ||
| sqlite.enableLoadExtension(false) | ||
| } catch (err) { | ||
| // Non-fatal: no current migration needs sqlite-vec (test-db migrates without it). | ||
| console.warn('[seed] sqlite-vec load skipped:', (err as Error).message) | ||
| } |
There was a problem hiding this comment.
Disable extension loading in the failure path as well.
If sqlite.loadExtension(...) throws, enableLoadExtension(false) is never reached, so extension loading stays enabled for the rest of the run. Keep it disabled in a finally block to avoid widening attack surface.
Suggested fix
try {
sqlite.enableLoadExtension(true)
sqlite.loadExtension(getLoadablePath())
- sqlite.enableLoadExtension(false)
} catch (err) {
// Non-fatal: no current migration needs sqlite-vec (test-db migrates without it).
console.warn('[seed] sqlite-vec load skipped:', (err as Error).message)
+ } finally {
+ sqlite.enableLoadExtension(false)
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| sqlite.enableLoadExtension(true) | |
| sqlite.loadExtension(getLoadablePath()) | |
| sqlite.enableLoadExtension(false) | |
| } catch (err) { | |
| // Non-fatal: no current migration needs sqlite-vec (test-db migrates without it). | |
| console.warn('[seed] sqlite-vec load skipped:', (err as Error).message) | |
| } | |
| try { | |
| sqlite.enableLoadExtension(true) | |
| sqlite.loadExtension(getLoadablePath()) | |
| } catch (err) { | |
| // Non-fatal: no current migration needs sqlite-vec (test-db migrates without it). | |
| console.warn('[seed] sqlite-vec load skipped:', (err as Error).message) | |
| } finally { | |
| sqlite.enableLoadExtension(false) | |
| } |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@scripts/seed/index.ts` around lines 46 - 52, The catch block leaves extension
loading enabled if sqlite.loadExtension(...) throws; change the try/catch to a
try/finally (or add a finally) so that sqlite.enableLoadExtension(false) is
always called regardless of success or error, keeping the current warning
behavior in the catch (or rethrow after finally) and ensuring
sqlite.enableLoadExtension(false) runs; update the block around
sqlite.enableLoadExtension(true) / sqlite.loadExtension(getLoadablePath()) /
sqlite.enableLoadExtension(false) accordingly.
Summary by CodeRabbit
db:seedcommand. This enables consistent database initialization with sample data for development and testing purposes.