Created
August 14, 2013 15:40
-
-
Save andrerom/6232275 to your computer and use it in GitHub Desktop.
For dumping a database to a "fixture" php file, as used by integration tests in eZ Publish 5.x
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 | |
require 'ezc/Base/base.php'; | |
spl_autoload_register( 'ezcBase::autoload' ); | |
$db = ezcDbFactory::create( 'mysql://user:password@server/databse_name' ); | |
// Get all tables | |
$tables = array(); | |
$result = $db->query( 'SHOW TABLES' ); | |
while ( $row = $result->fetch( PDO::FETCH_COLUMN ) ) | |
{ | |
// Filtering out some tables | |
if ( strpos( $row, 'ezx_' ) !== 0 ) | |
$tables[] = $row; | |
} | |
// get data | |
$fixture = array(); | |
foreach ( $tables as $table ) | |
{ | |
$result = $db->query( 'SELECT * FROM ' . $table ); | |
$fixture[$table] = array(); | |
while ( $row = $result->fetch( PDO::FETCH_ASSOC ) ) | |
{ | |
$fixture[$table][] = $row; | |
} | |
} | |
echo "<?php\n\nreturn ", str_replace( array( "\n " ,"\n ", "\n ", " => \n" ) , array( "\n", "\n ", "\n ", " =>\n" ), var_export( $fixture, true ) ), ";\n\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment