Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(storage): find a stored reference in any case on Postgres too

Mauricee1a5864406b6
diff --git a/server/src/nest/auth/auth-storage-refs.service.ts b/server/src/nest/auth/auth-storage-refs.service.ts
index 7f7022095..bcdb26584 100644
--- a/server/src/nest/auth/auth-storage-refs.service.ts
+++ b/server/src/nest/auth/auth-storage-refs.service.ts
@@ -17,7 +17,7 @@ export class AuthStorageRefsService extends StorageReclaimSource {
protected async storedValues(containing: readonly string[] | null): Promise<Array<string | null | undefined>> {
const rows = await this.em
.getRepository(Users)
- .find(likeAny<Users>(['avatar'], containing), { fields: ['avatar'] });
+ .find(likeAny<Users>(this.em.getPlatform(), ['avatar'], containing), { fields: ['avatar'] });
return rows.map((row) => row.avatar);
}
}
diff --git a/server/src/nest/collections/collections-storage-refs.service.ts b/server/src/nest/collections/collections-storage-refs.service.ts
index 2384b3a4a..7035d7eec 100644
--- a/server/src/nest/collections/collections-storage-refs.service.ts
+++ b/server/src/nest/collections/collections-storage-refs.service.ts
@@ -19,10 +19,10 @@ export class CollectionsStorageRefsService extends StorageReclaimSource {
protected async storedValues(containing: readonly string[] | null): Promise<Array<string | null | undefined>> {
const covers = await this.em
.getRepository(Collections)
- .find(likeAny<Collections>(['cover_image'], containing), { fields: ['cover_image'] });
+ .find(likeAny<Collections>(this.em.getPlatform(), ['cover_image'], containing), { fields: ['cover_image'] });
const images = await this.em
.getRepository(CollectionPlaces)
- .find(likeAny<CollectionPlaces>(['image_url'], containing), { fields: ['image_url'] });
+ .find(likeAny<CollectionPlaces>(this.em.getPlatform(), ['image_url'], containing), { fields: ['image_url'] });
return [...covers.map((row) => row.cover_image), ...images.map((row) => row.image_url)];
}
}
diff --git a/server/src/nest/files/files-storage-refs.service.ts b/server/src/nest/files/files-storage-refs.service.ts
index 908c4211a..b5795fa8d 100644
--- a/server/src/nest/files/files-storage-refs.service.ts
+++ b/server/src/nest/files/files-storage-refs.service.ts
@@ -22,7 +22,7 @@ export class FilesStorageRefsService extends StorageReclaimSource {
protected async storedValues(containing: readonly string[] | null): Promise<Array<string | null | undefined>> {
const rows = await this.em
.getRepository(TripFiles)
- .find(likeAny<TripFiles>(['filename'], containing), { fields: ['filename'] });
+ .find(likeAny<TripFiles>(this.em.getPlatform(), ['filename'], containing), { fields: ['filename'] });
return rows.map((row) => row.filename);
}
}
diff --git a/server/src/nest/journey/journey-storage-refs.service.ts b/server/src/nest/journey/journey-storage-refs.service.ts
index 71a8bd184..ab51ac0b2 100644
--- a/server/src/nest/journey/journey-storage-refs.service.ts
+++ b/server/src/nest/journey/journey-storage-refs.service.ts
@@ -18,7 +18,7 @@ export class JourneyStorageRefsService extends StorageReclaimSource {
protected async storedValues(containing: readonly string[] | null): Promise<Array<string | null | undefined>> {
const rows = await this.em
.getRepository(Journeys)
- .find(likeAny<Journeys>(['cover_image'], containing), { fields: ['cover_image'] });
+ .find(likeAny<Journeys>(this.em.getPlatform(), ['cover_image'], containing), { fields: ['cover_image'] });
return rows.map((row) => row.cover_image);
}
}
diff --git a/server/src/nest/photos/photos-storage-refs.service.ts b/server/src/nest/photos/photos-storage-refs.service.ts
index 3eec9cbd9..4481bed21 100644
--- a/server/src/nest/photos/photos-storage-refs.service.ts
+++ b/server/src/nest/photos/photos-storage-refs.service.ts
@@ -23,7 +23,7 @@ export class PhotosStorageRefsService extends StorageReclaimSource {
protected async storedValues(containing: readonly string[] | null): Promise<Array<string | null | undefined>> {
const rows = await this.em
.getRepository(TrekPhotos)
- .find(likeAny<TrekPhotos>(['file_path', 'thumbnail_path'], containing), {
+ .find(likeAny<TrekPhotos>(this.em.getPlatform(), ['file_path', 'thumbnail_path'], containing), {
fields: ['file_path', 'thumbnail_path'],
});
return rows.flatMap((row) => [row.file_path, row.thumbnail_path]);
diff --git a/server/src/nest/place-contrib/place-contrib-storage-refs.service.ts b/server/src/nest/place-contrib/place-contrib-storage-refs.service.ts
index 970c6016e..99079d0e9 100644
--- a/server/src/nest/place-contrib/place-contrib-storage-refs.service.ts
+++ b/server/src/nest/place-contrib/place-contrib-storage-refs.service.ts
@@ -18,7 +18,7 @@ export class PlaceContribStorageRefsService extends StorageReclaimSource {
protected async storedValues(containing: readonly string[] | null): Promise<Array<string | null | undefined>> {
const rows = await this.em
.getRepository(PlaceContribOutbox)
- .find(likeAny<PlaceContribOutbox>(['value'], containing), { fields: ['value'] });
+ .find(likeAny<PlaceContribOutbox>(this.em.getPlatform(), ['value'], containing), { fields: ['value'] });
return rows.map((row) => row.value);
}
}
diff --git a/server/src/nest/places/places-storage-refs.service.ts b/server/src/nest/places/places-storage-refs.service.ts
index 6e461e1f8..4c588a290 100644
--- a/server/src/nest/places/places-storage-refs.service.ts
+++ b/server/src/nest/places/places-storage-refs.service.ts
@@ -19,7 +19,7 @@ export class PlacesStorageRefsService extends StorageReclaimSource {
protected async storedValues(containing: readonly string[] | null): Promise<Array<string | null | undefined>> {
const rows = await this.em
.getRepository(Places)
- .find(likeAny<Places>(['image_url'], containing), { fields: ['image_url'] });
+ .find(likeAny<Places>(this.em.getPlatform(), ['image_url'], containing), { fields: ['image_url'] });
return rows.map((row) => row.image_url);
}
}
diff --git a/server/src/nest/storage/storage-reclaim-source.service.ts b/server/src/nest/storage/storage-reclaim-source.service.ts
index 6e0855037..08402f3d5 100644
--- a/server/src/nest/storage/storage-reclaim-source.service.ts
+++ b/server/src/nest/storage/storage-reclaim-source.service.ts
@@ -1,3 +1,4 @@
+import { lower } from '../../db/dialect/sql-functions';
import { logWarn } from '../audit/audit-log.logger';
import { StorageReclaimRegistryService } from './storage-reclaim-registry.service';
import {
@@ -10,7 +11,7 @@ import {
type StorageReferenceSourceId,
} from './storage-reclaim.types';
import type { ServedCategory } from './storage.types';
-import { EntityManager, type FilterQuery } from '@mikro-orm/core';
+import { EntityManager, type FilterQuery, type Platform } from '@mikro-orm/core';
import { Injectable, type OnApplicationBootstrap } from '@nestjs/common';
/**
@@ -29,7 +30,7 @@ import { Injectable, type OnApplicationBootstrap } from '@nestjs/common';
* - `namedKeys` reads every value once for one category, fresh, for the
* question a pass asks right before it deletes;
* - `referenced` reads only the rows whose value holds one of the keys' plain
- * runs (`keySearchCore`, a `LIKE '%run%'` each, at most MAX_LIKE_TERMS per
+ * runs (`keySearchCore`, a `LOWER(col) LIKE '%run%'` each, at most MAX_LIKE_TERMS per
* query, as many queries as the keys need), for the release queue. A key
* with no such run is answered from one whole read; the release queue never
* asks about such a key, it leaves it to the nightly pass.
@@ -113,17 +114,22 @@ const RUNS_PER_QUERY = 25;
* The filter a source narrows its columns with: rows where one of `fields`
* holds one of the runs, or every row where one of them is set when there are
* none to narrow by. The runs are `[a-z0-9-]` only, so they need no LIKE
- * escaping; SQLite's LIKE ignores ASCII case, as the parser does. A filter
- * past MAX_LIKE_TERMS is refused rather than handed to SQLite.
+ * escaping. They are lowercase (`keySearchCore`), and the parser ignores case,
+ * so the column is lowercased before it is matched: `LOWER(col) LIKE '%run%'`
+ * finds a key spelled in any case on both engines, where a bare LIKE ignores
+ * ASCII case on SQLite only and would miss an uppercase spelling on Postgres,
+ * whose object the release would then delete. A filter past MAX_LIKE_TERMS is
+ * refused rather than handed to the database.
*/
export function likeAny<T>(
+ platform: Platform,
fields: ReadonlyArray<keyof T & string>,
containing: readonly string[] | null,
): FilterQuery<T> {
const terms =
containing === null
? fields.map((field) => ({ [field]: { $ne: null } }))
- : fields.flatMap((field) => containing.map((run) => ({ [field]: { $like: `%${run}%` } })));
+ : fields.flatMap((field) => containing.map((run) => ({ [lower(platform, field)]: { $like: `%${run}%` } })));
if (terms.length > MAX_LIKE_TERMS) {
logWarn(`[storage] a reference lookup asked for ${terms.length} LIKE terms, more than ${MAX_LIKE_TERMS}`);
throw new Error(`a reference lookup may hold at most ${MAX_LIKE_TERMS} LIKE terms`);
diff --git a/server/src/nest/trips/trips-storage-refs.service.ts b/server/src/nest/trips/trips-storage-refs.service.ts
index b5049a95c..12cb9abe0 100644
--- a/server/src/nest/trips/trips-storage-refs.service.ts
+++ b/server/src/nest/trips/trips-storage-refs.service.ts
@@ -18,7 +18,7 @@ export class TripsStorageRefsService extends StorageReclaimSource {
protected async storedValues(containing: readonly string[] | null): Promise<Array<string | null | undefined>> {
const rows = await this.em
.getRepository(Trips)
- .find(likeAny<Trips>(['cover_image'], containing), { fields: ['cover_image'] });
+ .find(likeAny<Trips>(this.em.getPlatform(), ['cover_image'], containing), { fields: ['cover_image'] });
return rows.map((row) => row.cover_image);
}
}
diff --git a/server/tests/integration/storage-release.test.ts b/server/tests/integration/storage-release.test.ts
index 0ef9d9def..d4b2848c8 100644
--- a/server/tests/integration/storage-release.test.ts
+++ b/server/tests/integration/storage-release.test.ts
@@ -6,7 +6,7 @@
* journey, a saved place, a value an undo wrote back) stays. The objects are
* written straight into the test run's uploads folder under random names and
* taken back afterwards, the owner markers the job wrote with them.
- * RELI-001 to RELI-009.
+ * RELI-001 to RELI-010.
*/
import { buildApp } from '../../src/bootstrap';
import { Addons } from '../../src/db/entities/Addons.entity';
@@ -115,6 +115,25 @@ describe('the deferred release of what a change left unnamed', () => {
expect(exists('covers', next)).toBe(true);
});
+ it('RELI-010: a row that names the object in uppercase keeps it on every engine', async () => {
+ const { user } = await makeUser(orm);
+ const old = stored('covers');
+ const trip = await makeTrip(orm, user.id, { cover_image: `/uploads/covers/${old}` });
+ // A client may write the key in another case; the parser reads it case-insensitively,
+ // and so must the narrowed lookup, which a bare LIKE is only on SQLite.
+ await makeTrip(orm, user.id, { cover_image: `/uploads/covers/${old.toUpperCase()}` });
+
+ const res = await request(app)
+ .put(`/api/trips/${trip.id}`)
+ .set('Cookie', authCookie(user.id))
+ .send({ cover_image: `/uploads/covers/${stored('covers')}` });
+ expect(res.status).toBe(200);
+ expect(await queued('covers', old)).toBe(1);
+ await tick();
+ expect(exists('covers', old)).toBe(true);
+ expect(await queued('covers', old)).toBe(0);
+ });
+
it('RELI-002: a deleted trip takes its cover along a day later, unless a journey inherited it', async () => {
const { user } = await makeUser(orm);
const alone = stored('covers');
diff --git a/server/tests/unit/nest/storage-reference-sources.test.ts b/server/tests/unit/nest/storage-reference-sources.test.ts
index 1411536aa..974920420 100644
--- a/server/tests/unit/nest/storage-reference-sources.test.ts
+++ b/server/tests/unit/nest/storage-reference-sources.test.ts
@@ -38,6 +38,7 @@ import { makeTrip } from '../../helpers/factories/trips';
import { makeUser } from '../../helpers/factories/users';
import { createTestModule, type TestModule } from '../../helpers/test-module';
import { createTestOrm, type TestOrm } from '../../helpers/test-orm';
+import { SqlitePlatform } from '@mikro-orm/sql';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
@@ -246,9 +247,11 @@ describe('reference sources', () => {
it('RSRC-016: a filter past MAX_LIKE_TERMS terms is refused before it reaches SQLite', () => {
const runs = Array.from({ length: MAX_LIKE_TERMS + 1 }, (_unused, i) => `run-${String(i).padStart(8, '0')}`);
- expect(() => likeAny<{ a: string }>(['a'], runs)).toThrow(/at most/);
- expect(() => likeAny<{ a: string; b: string }>(['a', 'b'], runs.slice(0, 25))).not.toThrow();
- expect(() => likeAny<{ a: string; b: string }>(['a', 'b'], runs.slice(0, 26))).toThrow(/at most/);
+ expect(() => likeAny<{ a: string }>(new SqlitePlatform(), ['a'], runs)).toThrow(/at most/);
+ expect(() => likeAny<{ a: string; b: string }>(new SqlitePlatform(), ['a', 'b'], runs.slice(0, 25))).not.toThrow();
+ expect(() => likeAny<{ a: string; b: string }>(new SqlitePlatform(), ['a', 'b'], runs.slice(0, 26))).toThrow(
+ /at most/,
+ );
});
it('RSRC-017: namedKeysByCategory reads once and answers every category the source claims, none it does not', async () => {