Last active
August 29, 2015 14:07
-
-
Save mfn/f722b86d9f8e31fc2d27 to your computer and use it in GitHub Desktop.
Filters a mysql dump to remove all FULLTEXT indices and does its best to keep the SQL syntax error-free
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
#!/usr/bin/env php | |
<?php | |
/** | |
* Filters MySQL dumps to remove FULLTEXT index creation from stdin to stdout. | |
* | |
* Has logic to adjust the line *before* the FULLTEXT to not create invalid SQL. | |
* | |
* @author Markus Fischer <markus@fischer.name> | |
* @license Do what you want, credit but don't blame me | |
*/ | |
$lineBefore = NULL; | |
$fulltextInLastLine = false; | |
$reComma = ';,(\s+)$;'; | |
while (false !== $line = fgets(STDIN)) { | |
# match these lines: | |
# FULLTEXT KEY `IDX_FULLTEXT_COMMENTS_MESSAGE` (`message`), | |
# ^- column 2 | |
if (2 === strpos($line, 'FULLTEXT KEY ')) { | |
# if there's a comma at the end, we can safely remove the whole line | |
if (preg_match($reComma, $line)) { | |
continue; | |
} | |
# otherwise: it's the last statement of the table creation. But since | |
# we're going to remove this line, we need to make the line BEFORE the | |
# last statement and remove the trailing ',' there. | |
if (NULL !== $lineBefore) { | |
$lineBefore = preg_replace($reComma, '$1', $lineBefore); | |
} | |
continue; | |
} | |
if (NULL !== $lineBefore) { | |
echo $lineBefore; | |
} | |
$lineBefore = $line; | |
} | |
if (NULL !== $lineBefore) { | |
echo $lineBefore; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment