From e7026d4aed263c290cad240c8ae108651034fda7 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 19 Jun 2023 11:16:27 +0100 Subject: [PATCH] Get schema function --- packages/server/src/integrations/postgres.ts | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/packages/server/src/integrations/postgres.ts b/packages/server/src/integrations/postgres.ts index b1f20f97ec..49c2c147a6 100644 --- a/packages/server/src/integrations/postgres.ts +++ b/packages/server/src/integrations/postgres.ts @@ -21,6 +21,7 @@ import { PostgresColumn } from "./base/types" import { escapeDangerousCharacters } from "../utilities" import { Client, ClientConfig, types } from "pg" +import { exec } from "child_process" // Return "date" and "timestamp" types as plain strings. // This lets us reference the original stored timezone. @@ -381,6 +382,34 @@ class PostgresIntegration extends Sql implements DatasourcePlus { return response.rows.length ? response.rows : [{ [operation]: true }] } } + + async getSchema() { + const dumpCommandParts = [ + `PGPASSWORD="${this.config.password}"`, + `pg_dump -U ${this.config.user} -h ${this.config.host} -p ${this.config.port} -d ${this.config.database} --schema-only`, + ] + + const dumpCommand = dumpCommandParts.join(" ") + + return new Promise((res, rej) => { + exec(dumpCommand, (error, stdout, stderr) => { + if (error) { + console.error(`Error generating dump: ${error.message}`) + rej(error.message) + return + } + + if (stderr) { + console.error(`pg_dump error: ${stderr}`) + rej(stderr) + return + } + + res(stdout) + console.log("SQL dump generated successfully!") + }) + }) + } } export default {