mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 21:11:14 +00:00
feat(db): run TREK on PostgreSQL
TREK runs on PostgreSQL when TREK_DB_URL is set, and on SQLite as before otherwise: an engine port under src/db/engine, the dialect helpers under src/db/dialect, a Postgres baseline for the migrations up to the cut, pg_dump/pg_restore backups, a probe of every repository method against a real server and a Postgres test lane in CI.
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import { isPostgres, isSqlite, unsupported } from './platform';
|
||||
import type { Platform } from '@mikro-orm/core';
|
||||
|
||||
/**
|
||||
* The schema catalog, for the migrations' helpers (`db/migration-utils.ts`):
|
||||
* which tables and columns exist, each spelled the way its engine keeps them.
|
||||
* Both statements take the table name as their one bound parameter and answer
|
||||
* one row per hit with the name in `name`. Postgres reads the current schema
|
||||
* only, the one TREK's own tables live in.
|
||||
*/
|
||||
|
||||
/** Lists the columns of the table bound as `?`, one `name` per row; none for a missing table. */
|
||||
export function columnNamesSql(platform: Platform): string {
|
||||
if (isSqlite(platform)) return 'select name from pragma_table_info(?)';
|
||||
if (isPostgres(platform)) {
|
||||
return 'select column_name as name from information_schema.columns where table_schema = current_schema() and table_name = ?';
|
||||
}
|
||||
return unsupported(platform);
|
||||
}
|
||||
|
||||
/** One row when the table bound as `?` exists, none otherwise. */
|
||||
export function tableExistsSql(platform: Platform): string {
|
||||
if (isSqlite(platform)) return "select name from sqlite_master where type = 'table' and name = ?";
|
||||
if (isPostgres(platform)) {
|
||||
return 'select table_name as name from information_schema.tables where table_schema = current_schema() and table_name = ?';
|
||||
}
|
||||
return unsupported(platform);
|
||||
}
|
||||
|
||||
/**
|
||||
* The tables in the current schema, one `table_name` per row in name order:
|
||||
* the schema TREK's own tables live in. Postgres only, and the one definition
|
||||
* of that question: the bootstrap reads it through the connection, the client
|
||||
* tools' table listing hands it to psql.
|
||||
*/
|
||||
export const CURRENT_SCHEMA_TABLES_SQL =
|
||||
'select table_name from information_schema.tables where table_schema = current_schema() order by table_name';
|
||||
@@ -620,6 +620,17 @@ export function collateNoCase(platform: Platform, ref: string): RawQueryFragment
|
||||
return unsupported(platform);
|
||||
}
|
||||
|
||||
/**
|
||||
* A search that ignores case over `refs`: `(a LIKE ? ESCAPE '\' OR b LIKE ?
|
||||
* ...)`, one bound pattern per column, the caller escaping `%`, `_` and `\`.
|
||||
* SQLite's `LIKE` already ignores case (ASCII letters only); Postgres's is
|
||||
* exact, so it gets `ILIKE`, which folds every letter.
|
||||
*/
|
||||
export function ilike(platform: Platform, refs: readonly string[]): string {
|
||||
const operator = isSqlite(platform) ? 'LIKE' : isPostgres(platform) ? 'ILIKE' : unsupported(platform);
|
||||
return `(${refs.map((ref) => `${column(ref)} ${operator} ? ESCAPE '\\'`).join(' OR ')})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* `datetime('now', '-N hours')` — the hour-granularity sibling of
|
||||
* `nowMinusDays()` above, for `reminder-jobs.service.ts#todoTick`'s
|
||||
|
||||
Reference in New Issue
Block a user