Skip to content

Instantly share code, notes, and snippets.

@MasonM
Last active January 7, 2018 06:03
Show Gist options
  • Save MasonM/1023ebc74cc5b0b0cd9d5cce8e6ae11e to your computer and use it in GitHub Desktop.
Save MasonM/1023ebc74cc5b0b0cd9d5cce8e6ae11e to your computer and use it in GitHub Desktop.
pg_dump_filter_and_separate.sh
#!/bin/bash
pg_dump --column-inserts | php -B '$dir = "sql"; $cur_table = null;' -R '
if (preg_match("/^(INSERT INTO (\w+) \([^)]*\) VALUES )(.*);$/", $argn, $matches)) {
$str = ($cur_table !== $matches[2]) ? $matches[1] : ",";
$cur_table = $matches[2];
file_put_contents("$dir/$cur_table.sql", "{$str}\n\t{$matches[3]}", FILE_APPEND);
}' -E 'foreach(glob("$dir/*.sql") as $f) file_put_contents($f, ";", FILE_APPEND);'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment