Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify restoreDbFromSource to drop all target tables before restore #5642

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 57 additions & 4 deletions scripts/postgres/restoreDbFromSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,61 @@ async function restoreFromBackup({
});
const targetPassword = await targetSigner.getAuthToken();

// pg_restore only drops tables that exist in the source dump, so we drop all target tables before calling pg_restore.
// We could drop the whole target db or the schema, but then we would have to deal with stricter permissions.
await new Promise(resolve => {
const result = spawn(
// For each table in the target db public schema, we will create a SQL DROP command and then execute it.
const dropTableQuery = spawn(
'psql',
[
`--host=${host}`,
`--username=${username}`,
`--dbname=${dbName}`,
`--port=${port}`,
'--no-password',
`--command=DO $$ DECLARE
stmt text;
BEGIN
FOR stmt IN
SELECT 'DROP TABLE IF EXISTS "' || tablename || '" CASCADE;'
FROM pg_tables
WHERE schemaname = 'public'
LOOP
EXECUTE stmt;
END LOOP;
END
$$;`,
],
{
env: {
...process.env,
PGPASSWORD: targetPassword,
},
},
);

dropTableQuery.stdout.on('data', data => {
console.log(data.toString('utf-8'));
});

dropTableQuery.stderr.on('data', data => {
console.error(data.toString('utf-8'));
});

dropTableQuery.on('close', code => {
if (code) {
console.log(
`Attempted to drop all tables from DB ${dbName}. Check output for errors. Exit code: ${code}`,
);
} else {
console.log(`Successfully dropped all tables from DB ${dbName}.`);
}
resolve(undefined);
});
});

await new Promise(resolve => {
const restoreDbResult = spawn(
'pg_restore',
[
`--host=${host}`,
Expand All @@ -240,15 +293,15 @@ async function restoreFromBackup({
},
);

result.stdout.on('data', data => {
restoreDbResult.stdout.on('data', data => {
console.log(data.toString('utf-8'));
});

result.stderr.on('data', data => {
restoreDbResult.stderr.on('data', data => {
console.error(data.toString('utf-8'));
});

result.on('close', code => {
restoreDbResult.on('close', code => {
if (code) {
console.log(
`DB ${dbName} may have been restored with errors. Check output for errors. Exit code: ${code}`,
Expand Down
Loading