Skip to content

Instantly share code, notes, and snippets.

@juzna
Last active December 17, 2015 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juzna/5579634 to your computer and use it in GitHub Desktop.
Save juzna/5579634 to your computer and use it in GitHub Desktop.
automatic db schema dump via Adminer, possibly on commit
<?php
/**
* Dump database schema
*
* Better than mysqldump, because of the nice output :)
* You can run this from CLI, it will output the db schema.
* Consider adding it as pre-commit hook.
*
* Example:
* php tools/db-schema-dump.php > db/schema.sql
*/
require_once __DIR__ . '/app/bootstrap.php';
Nette\Diagnostics\Debugger::$bar = NULL;
// get connection settings
if ($conn = $container->getByType('Kdyby\Doctrine\Connection', FALSE)) { // from doctrine
/** @var \Kdyby\Doctrine\Connection $conn */
$params = $conn->getParams();
$adminerConfig = array(
'host' => $params['host'],
'user' => $params['user'],
'password' => $params['password'],
'database' => $params['dbname'],
);
} elseif ( ! empty($container->parameters['database'])) { // or from parameters
$params = $container->parameters['database'];
$adminerConfig = array(
'host' => $params['host'],
'user' => $params['user'],
'password' => $params['password'],
'database' => $params['database'],
);
} else {
echo 'Database connection parameters not found';
die(1);
}
// forge the request for Adminer
session_name('adminer_sessid');
session_start();
$_GET = array(
"username" => "",
"dump" => "",
);
$_POST = array(
"output" => "text",
"format" => "sql",
"routines" => 1,
"events" => 1,
"triggers" => 1,
"db_style" => "",
"table_style" => "CREATE",
"data_style" => "",
"databases" => array($adminerConfig["database"]),
"token" => "dummy",
);
$_SESSION = array(
"token" => "dummy",
);
function adminer_object() {
class AdminerLogin extends Adminer {
function credentials() {
global $adminerConfig;
return array($adminerConfig['host'], $adminerConfig['user'], $adminerConfig['password']);
}
}
return new AdminerLogin;
}
// run Adminer (it calls exit inside)
chdir(WWW_DIR . '/developer/adminer'); // put here path to downloaded adminer
include "./index.php";
#!/bin/bash
# Check whether database schema was changed, and ask to commit it
# Consider adding this as pre-commit hook ;)
# dump database schema
PD_ENV=local php tools/db-schema-dump.php > .db/schema.sql || exit 1
sed -i '$ d' .db/schema.sql
# is it changed?
changed=$(git diff --name-only -- .db/schema.sql | wc -l)
if [ $changed -eq "1" ]; then
echo "Database schema was changed, please commit it as well"
echo " -- see .db/schema.sql"
echo
echo "or force commit without checks: git commit -n"
exit 1
fi
#!/bin/sh
./tools/git-hook-check-schema.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment