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.

2.4 KB
Raw Blame
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';

/**
 * Every column of every table, one `table_name` and `name` per row: the
 * whole schema in one statement, for the boot's check that each column the
 * entities map exists (`db/schema-gaps.ts`). Postgres reads the current
 * schema only, as above.
 */
export function tableColumnsSql(platform: Platform): string {
  if (isSqlite(platform)) {
    return "select m.name as table_name, p.name as name from sqlite_master m join pragma_table_info(m.name) p where m.type = 'table'";
  }
  if (isPostgres(platform)) {
    return 'select table_name, column_name as name from information_schema.columns where table_schema = current_schema()';
  }
  return unsupported(platform);
}