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 {