A newly set-up instance runs without its MongoDB indexes until the API restarts, because setup drops the database after boot built them

オープン
#1,882 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
74/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
mongodb, nodejs, typescript
領域
backend, databases

調査の方向性

Read apps/api/src/setup/setup.service.ts and the ensureDatabaseIndexes implementation in apps/api/src/core/prisma.ts; trace the database-drop path used by initApp and delete. Start with apps/api/src/setup/__tests__/setup.service.test.ts, then use the integration harness in apps/api/test/suites/ to verify that indexes exist after setup. Done means indexes are restored after a drop and before admin creation, with the relevant tests passing.

索引モデルが issue の本文から書いたものです。

説明

Area: API Bug Difficulty: Low Performance Priority: Medium

A newly set-up instance runs with no MongoDB indexes until the API restarts. ensureDatabaseIndexes builds every index in DATABASE_INDEXES once, when the Prisma client is created at boot. SetupService.initApp, which serves POST /v1/setup, then drops the whole database before it creates the admin. Dropping a database also drops its indexes. Every collection written after that is recreated on first insert with only _id_.

Until the next restart, the instance enforces none of the schema's unique constraints: UserModel.username, GroupModel.name, InstrumentRepoModel.url and PermissionSetModel.name, plus the InstrumentRecordModel.assignmentId index. Every query those indexes exist for scans the whole collection. The services check for duplicates before they insert (for example GroupsService.create calls exists({ name })), so a duplicate needs two concurrent requests. The index is the only thing that stops that race. A Docker deployment that is set up once and never restarted stays in this state indefinitely. The same thing happens on every re-initialisation in development, which initApp allows when NODE_ENV=development.

The Playwright stack runs this way too: testing/src/global/setup.spec.ts posts /v1/setup after the API has booted, so no e2e test ever runs against the indexes.

Where

apps/api/src/setup/setup.service.ts:99 (initApp) and :49 (delete, test-only) both call:

private async dropDatabase(): Promise<void> {
  const result = await this.prismaClient.$runCommandRaw({ dropDatabase: 1 });
  ...
}

apps/api/src/core/prisma.ts:190: the only call to ensureDatabaseIndexes, in the client factory, right after $connect():

await client.$connect();
await ensureDatabaseIndexes(client);

Introduced with startup index creation in 672eaab19 (v2.2.3). Every instance set up since then started without indexes and got them only at its first restart.

Reproduce

  1. Start the API on an empty database (pnpm dev:test in apps/api will do).
  2. Before setup, list the indexes on UserModel: the unique UserModel_username_key is there.
  3. POST /v1/setup with valid $InitAppOptions.
  4. List the indexes on UserModel again.

Actual: only _id_. The same holds for every other collection in DATABASE_INDEXES.
Expected: setup leaves the database with every index ensureDatabaseIndexes builds at boot.

Tests

  • apps/api/src/setup/__tests__/setup.service.test.ts: it('should rebuild the database indexes after dropping the database, so a newly set-up instance enforces its unique constraints'). Assert the order: drop, then indexes, then the admin is created.
  • apps/api/test/suites/: boot the app, POST /v1/setup, then run listIndexes on UserModel and GroupModel through the Prisma client and expect the unique indexes. Unit tests mock Prisma, so only this harness shows what a real dropDatabase does.
  • E2E: no route exposes indexes. One option is a spec that sends two concurrent POST /v1/groups requests with the same name after the global setup and expects exactly one group to be created. Today both can succeed. Whether that is reliable enough, or whether the integration suite counts as the e2e coverage here, is the implementer's call to raise in the PR.

Suggested fix

Rebuild the indexes inside dropDatabase, right after the drop succeeds, so every path that drops the database, delete included, restores them. src/core/prisma.ts already exports ensureDatabaseIndexes, and it takes a RawCommandRunner, which the injected client satisfies. Running it before createAdmin also means the admin and demo inserts happen with the indexes in place.

Found while working on #1571 (PR #1878).

主要言語
TypeScript
スター
119
フォーク
19
平均マージ
1日 18時間
マージ済み PR(30日)
61

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

DouglasNeuroInformatics/OpenDataCapture のほかの issue

DouglasNeuroInformatics/OpenDataCapture の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。