mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 19:40:34 +00:00
refactor(db): type the database settings and inject one engine
The settings for the database were one flat record whose url and pool size
were meaningful only for Postgres, so every reader had to re-check the URL.
They are now a discriminated union per engine (DbEngineSettings): the
Postgres arm carries its URL and a pool size already capped at the ceiling,
and the SQLite arm carries nothing else. The env schema uses the same ceiling
constant, so the boot check and the derivation cannot drift apart.
The engine was resolved from the environment by every caller that needed it
(the lifecycle, the unit of work, the backup adapters), each building its own
instance. The engine is now provided once per container by
DatabaseEngineModule and injected everywhere, so the state an engine keeps
(Postgres's reopen hook) is the same wherever it is used. resolveEngine stays
for the MikroORM CLI config and the DI factory, and nothing else calls it.
The engine's reopen no longer carries a `reopenSwapsConnection` flag. The
engine itself decides what a reopen needs: SQLite closes and reconnects the
ORM's connection before the schema bootstrap (its handle is swapped under
Kysely's cache), and Postgres only migrates, because a restore replaces the
contents of the database the pool already reaches. The Postgres pool is never
closed on a reopen, since ending it waits for connections lent out during the
restore.
The backup ports answer every method the same way. verify() now throws a
DomainError for a refused archive on both engines, and the restore path maps
it back to the same { success: false, error, status } result, so the HTTP
response of a refused restore (status and body) is unchanged. The restore
step that swaps the database and then always reopens it is shared in
nest/backup/database-swap.ts, with the error-description helper in
nest/common, instead of two copies that could drift.
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
* bundle), via a real MikroORM over the full migrated schema (`createSnapshotTestDb`
|
||||
* + `createTestOrm`) rather than a hand-rolled `:memory:` table set.
|
||||
*/
|
||||
import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { PluginActions } from '../../../src/db/entities/PluginActions.entity';
|
||||
import { PluginErrorLog } from '../../../src/db/entities/PluginErrorLog.entity';
|
||||
import { PluginSettingsFields } from '../../../src/db/entities/PluginSettingsFields.entity';
|
||||
@@ -47,7 +48,7 @@ beforeAll(async () => {
|
||||
actions: t.repo(PluginActions),
|
||||
settingsFields: t.repo(PluginSettingsFields),
|
||||
errorLog: t.repo(PluginErrorLog),
|
||||
uow: new UnitOfWork(t.em),
|
||||
uow: new UnitOfWork(t.em, new SqliteEngine()),
|
||||
};
|
||||
});
|
||||
beforeEach(() => {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* https/SSRF guard on the endpoints, the code + refresh token exchanges (mocked fetch),
|
||||
* tokens encrypted at rest, and a stored refresh token that the plugin never sees.
|
||||
*/
|
||||
import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { PluginOauthState } from '../../../src/db/entities/PluginOauthState.entity';
|
||||
import { PluginOauthTokens } from '../../../src/db/entities/PluginOauthTokens.entity';
|
||||
import { PluginSettingsFields } from '../../../src/db/entities/PluginSettingsFields.entity';
|
||||
@@ -90,7 +91,7 @@ async function makeOauthService(): Promise<PluginOAuthService> {
|
||||
orm.repo(PluginOauthTokens),
|
||||
orm.repo(PluginOauthState),
|
||||
orm.repo(PluginSettingsFields),
|
||||
new UnitOfWork(orm.em),
|
||||
new UnitOfWork(orm.em, new SqliteEngine()),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
* defensible: dropping the `logError` call, or falling through to `activate`
|
||||
* anyway, would ship green with no test failing. This file pins it directly.
|
||||
*/
|
||||
import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { AuditLog } from '../../../src/db/entities/AuditLog.entity';
|
||||
import { NotificationChannelPreferences } from '../../../src/db/entities/NotificationChannelPreferences.entity';
|
||||
import { PluginActions } from '../../../src/db/entities/PluginActions.entity';
|
||||
@@ -99,7 +100,7 @@ async function buildRuntime(withOrm: boolean): Promise<PluginRuntimeService> {
|
||||
t.repo(NotificationChannelPreferences),
|
||||
// Plan 4 Task 4: `uow` is no longer `@Optional()` — ordered ahead of
|
||||
// `registry?`/`hostFactory?`, matching the real constructor's own order.
|
||||
new UnitOfWork(t.em),
|
||||
new UnitOfWork(t.em, new SqliteEngine()),
|
||||
undefined,
|
||||
undefined,
|
||||
withOrm ? t.orm : undefined,
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
* shared helpers use to let ordinary unit tests call a repository with no
|
||||
* request wrapper at all: that default would hide this exact bug.
|
||||
*/
|
||||
import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { AppSettings } from '../../../src/db/entities/AppSettings.entity';
|
||||
import { Trips } from '../../../src/db/entities/Trips.entity';
|
||||
import { Users } from '../../../src/db/entities/Users.entity';
|
||||
@@ -70,7 +71,7 @@ let userId: number;
|
||||
|
||||
beforeAll(async () => {
|
||||
t = await createTestOrm(testDb, { allowGlobalContext: false });
|
||||
permissions = new PermissionsService(t.repo(AppSettings), new UnitOfWork(t.em));
|
||||
permissions = new PermissionsService(t.repo(AppSettings), new UnitOfWork(t.em, new SqliteEngine()));
|
||||
// Plan 3j Task 1: PluginGuards' own role lookup (canCreateAs, PG4) now goes
|
||||
// through UsersRepository.getRole — a REAL repository bound to `t.em`
|
||||
// (allowGlobalContext: false, same as `permissions`' AppSettingsRepository
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
* which for a notification channel meant being dispatched to everyone with no credentials.
|
||||
*/
|
||||
import { db as testDb } from '../../../src/db/database';
|
||||
import { SqliteEngine } from '../../../src/db/engine/sqlite/sqlite-engine';
|
||||
import { AuditLog } from '../../../src/db/entities/AuditLog.entity';
|
||||
import { NotificationChannelPreferences } from '../../../src/db/entities/NotificationChannelPreferences.entity';
|
||||
import { PluginActions } from '../../../src/db/entities/PluginActions.entity';
|
||||
@@ -204,7 +205,7 @@ describe('a plugin channel label is bounded by the host', () => {
|
||||
t.repo(Settings),
|
||||
t.repo(NotificationChannelPreferences),
|
||||
// Plan 4 Task 4: `uow` is no longer `@Optional()`.
|
||||
new UnitOfWork(t.em),
|
||||
new UnitOfWork(t.em, new SqliteEngine()),
|
||||
);
|
||||
// Stand the plugin up as a granted, active notificationChannel provider.
|
||||
(rt as unknown as { supervisor: { running: Map<string, unknown> } }).supervisor.running.set('loud', {
|
||||
|
||||
Reference in New Issue
Block a user