Created
June 4, 2024 22:53
-
-
Save akirk/658df243c2901b755615782337d45564 to your computer and use it in GitHub Desktop.
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 | |
$json = json_decode('[{ | |
"step": "runSql", | |
"sql": { | |
"resource": "literal", | |
"name": "schema.sql", | |
"contents": "" | |
} | |
}]'); | |
$files = glob( './friends-demo*.sql' ); | |
sort($files); | |
$file = end( $files ); | |
$sql = file_get_contents($file); | |
$sql = str_replace( "),\n\t(", '),(', $sql ); | |
$sql = str_replace( "\nVALUES\n\t(", ' VALUES (', $sql ); | |
$new_sql = ''; | |
foreach ( explode( PHP_EOL, $sql ) as $line ) { | |
if ( 'INSERT INTO `' === substr( $line, 0, 13 ) ) { | |
$table_name = substr( $line, 13, strpos( $line, '`', 13 ) - 13 ); | |
$new_sql .= 'DELETE FROM `' . $table_name . '`;' . PHP_EOL; | |
$new_sql .= $line . PHP_EOL; | |
} | |
} | |
$json[0]->sql->contents = trim( $new_sql ); | |
$j = trim( json_encode( $json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ), '[]' . PHP_EOL); | |
echo $j, PHP_EOL, 'Bytes: ', strlen( $j ), PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment