mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 11:06:06 +00:00
fix(db): hold SQLite-only functions, LIKE and the sqlite member of a post-cut migration in lint:dialect
lint:dialect missed several classes of SQL that SQLite reads and Postgres does not, or reads differently: - group_concat, ifnull, instr, iif, printf, typeof, total(), changes() and json_object/json_array/json_valid/json_type/json_quote, as calls in SQL text and as the bare name handed to eb.fn. - LIKE in SQL text, Kysely's 'like' operator and MikroORM's $like, which ignore case on SQLite only. A LIKE that is exact on purpose now says so in a like-exact comment; the six existing ones (setting keys, storage paths, the lowercased reference search, sqlite_master) carry it. - The sqlite member of engineDdl was not read at all, so a trigger or a data backfill put there never reached Postgres. It may now hold only the schema change and a rebuild's copy and sqlite_sequence bookkeeping, and SQLite statements with an empty postgres member need a sqlite-only reason. - execBestEffort, bestEffort and execUnlessTableMissing in a post-cut migration: on Postgres a swallowed failure aborts the migration transaction. - The post-cut rules follow POSTGRES_FIRST_CUT as well as the manifest, so regenerating the baseline over a new migration no longer releases it. src/db/engine/sqlite/ is SQLite's own code and is no longer scanned.
This commit is contained in:
@@ -45,6 +45,7 @@ async function databaseFile(connection: Connection): Promise<string | null> {
|
||||
/** A fresh install: nothing but the migrator's own bookkeeping table yet. */
|
||||
async function holdsAnything(connection: Connection): Promise<boolean> {
|
||||
const rows: unknown[] = await connection.execute(
|
||||
// like-exact: SQLite's own `sqlite_` tables, on the SQLite file this snapshot is for.
|
||||
`SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' AND name <> 'mikro_orm_migrations' LIMIT 1`,
|
||||
);
|
||||
return rows.length > 0;
|
||||
|
||||
@@ -81,6 +81,7 @@ export class AppSettingsRepository extends TrekRepository<AppSettings> {
|
||||
* a prefix that itself contains `%`/`_`.
|
||||
*/
|
||||
async findByKeyPrefix(prefix: string): Promise<AppSettingsRow[]> {
|
||||
// like-exact: setting keys are lowercase identifiers, matched by their exact prefix on both engines.
|
||||
const rows = await this.find({ key: { $like: `${prefix}%` } });
|
||||
return rows.map((row) => toRow(row) as AppSettingsRow);
|
||||
}
|
||||
|
||||
@@ -87,6 +87,7 @@ export class SettingsRepository extends TrekRepository<Settings> {
|
||||
* parameter — never interpolated into SQL text.
|
||||
*/
|
||||
async deleteByKeyPrefix(prefix: string): Promise<number> {
|
||||
// like-exact: a plugin's setting keys start with its exact `plugin:<id>:` prefix on both engines.
|
||||
return await this.nativeDelete({ key: { $like: `${prefix}%` } });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -201,12 +201,14 @@ export class TrekPhotosRepository extends TrekRepository<TrekPhotos> {
|
||||
* never match `LIKE`, so every returned value is a real string.
|
||||
*/
|
||||
async listByFilePathPrefix(prefix: string): Promise<string[]> {
|
||||
// like-exact: a storage path prefix, exact on both engines.
|
||||
const rows = await this.find({ file_path: { $like: `${prefix}%` } }, { fields: ['file_path'] });
|
||||
return rows.map((row) => row.file_path).filter((path): path is string => path != null);
|
||||
}
|
||||
|
||||
/** TH2 — `SELECT thumbnail_path FROM trek_photos WHERE thumbnail_path LIKE 'journey/thumbs/%'`, same shape as TH1 on the sibling column. */
|
||||
async listByThumbnailPathPrefix(prefix: string): Promise<string[]> {
|
||||
// like-exact: a storage path prefix, exact on both engines.
|
||||
const rows = await this.find({ thumbnail_path: { $like: `${prefix}%` } }, { fields: ['thumbnail_path'] });
|
||||
return rows.map((row) => row.thumbnail_path).filter((path): path is string => path != null);
|
||||
}
|
||||
|
||||
@@ -129,7 +129,8 @@ export function likeAny<T>(
|
||||
const terms =
|
||||
containing === null
|
||||
? fields.map((field) => ({ [field]: { $ne: null } }))
|
||||
: fields.flatMap((field) => containing.map((run) => ({ [lower(platform, field)]: { $like: `%${run}%` } })));
|
||||
: // like-exact: the column is lowercased and the runs are lowercase, so the match ignores case on both engines.
|
||||
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`);
|
||||
|
||||
Reference in New Issue
Block a user