Skip to content

Instantly share code, notes, and snippets.

@MasonM
Created January 7, 2018 05:07
Show Gist options
  • Save MasonM/3efa929513f5a12989958d71af55bbae to your computer and use it in GitHub Desktop.
Save MasonM/3efa929513f5a12989958d71af55bbae to your computer and use it in GitHub Desktop.
pg_dump multi-row insert cleanup
<?php
$current_table_insert = null;
while ($line = fgets(STDIN)) {
if (strpos($line, 'SET ') === 0) {
echo $line;
} elseif (strpos($line, 'INSERT INTO') === 0) {
preg_match('/^(INSERT INTO (\w+) \([^)]*\) VALUES )(.*);$/', $line, $matches);
if ($current_table_insert !== null) {
echo ($current_table_insert === $matches[1]) ? ',' : ';';
}
if ($current_table_insert !== $matches[1]) {
echo "\n\n{$matches[1]}";
}
echo "\n\t{$matches[3]}";
$current_table_insert = $matches[1];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment