import { psql } from "./names";
import type { Sql } from "./quote";
import { runShell } from "./runShell";

/**
 * SLOW: runs a psql with the provided SQL query (or queries) passed as stdin.
 * - Does NOT run in a single-transaction mode.
 * - Suitable for slow queries, like indexes and FKs creation.
 * - Prints progress as it goes (suitable for slow queries).
 * - Logs the progress unique lines to the log file if it's attached.
 */
export async function runSqlProgressNonTransactional(
  dsn: string,
  query: Sql,
  comment: string,
): Promise<void> {
  await runShell(`${psql(dsn)} -a`, query.toString(), comment, "progress");
}

/**
 * FAST: runs a psql with the provided SQL query (or queries) passed as stdin.
 * - Runs in a single-transaction mode.
 * - Suitable for fast queries, like pre-data dump restoration.
 * - Prints progress as it goes (suitable for slow queries).
 * - Does NOT log the progress unique lines to the log file (assuming that the
 *   SQL queries are so bulky that it makes no sense to log them).
 */
export async function runSqlProgressTransactional(
  dsn: string,
  query: Sql,
  comment: string,
): Promise<void> {
  await runShell(
    `${psql(dsn)} -a -1`, // -1 means "single-transaction"
    query.toString(),
    comment,
    "progress.unlogged",
  );
}
