import type { TableInfo } from "./getTablesInSchema";
import { ident, join, sql } from "./quote";
import { runSqlProgressNonTransactional } from "./runSqlProgress";

export async function analyzeTables({
  toDsn,
  tables,
}: {
  toDsn: string;
  tables: TableInfo[];
}): Promise<void> {
  if (tables.length === 0) {
    return;
  }

  await runSqlProgressNonTransactional(
    toDsn,
    join(
      tables.map(
        ({ schema, name }) => sql`ANALYZE ${ident(schema)}.${ident(name)};`,
      ),
      "\n",
    ),
    "Running ANALYZE for the copied tables",
  );
}
