Created
January 7, 2018 05:07
-
-
Save MasonM/3efa929513f5a12989958d71af55bbae to your computer and use it in GitHub Desktop.
pg_dump multi-row insert cleanup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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