mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 08:53:13 +00:00
fix(storage): find a stored reference in any case on Postgres too
The release queue narrows each owner's rows with LIKE '%run%' before it parses them, and relied on SQLite's LIKE ignoring ASCII case. On Postgres LIKE is exact, so a row naming an object in uppercase was not found and the release deleted an object that row still names. likeAny now matches LOWER(column) against the lowercase run, which reads alike on both engines. RELI-010 fails on Postgres without it.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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]);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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`);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
Reference in New Issue
Block a user