Export your database to XML schema files., (*4)
sh src/Export/exportMySQL.sh <hostname> <username> <database> <output_dir>
Your output should look like:, (*5)
Creating schema: series
Creating schema: services
Creating schema: sessions
Creating schema: slides
Creating schema: speakers
Creating schema: states
Creating schema: steps
Creating schema: subscription
The XML files outputted will be how you manage what your database looks like from now on., (*6)
When you make a change to your schema files you then use CloudDueling\AutoMigrate\MySQL
and to loop through each schema file and to alter your database., (*7)
Example of available methods:, (*8)
$params = array(
'dbuser' => 'root',
'dbpass' => 'root',
'dbname' => 'database',
'dbhost' => 'localhost'
);
try {
$diff = new MySQLDiff($params);
} catch(Exception $e) {
echo $e->getMessage(); exit;
}
// This returns an array of what's missing in the database
try {
$diff_lines = $diff->getDiffs();
var_dump($diff_lines);
catch(Exception $e) {
echo $e->getMessage(); exit;
}
// This returns SQL queries which can be run to fix the database
try {
$diff_lines = $diff->getSQLDiffs();
var_dump($diff_lines);
} catch(Exception $e) {
echo $e->getMessage(); exit;
}
// This generates the SQL and actually runs all of them
try {
$diff_lines = $diff->runSQLDiff();
var_dump($diff_lines);
} catch(Exception $e) {
echo $e->getMessage(); exit;
}
Example looping through a directory of schemas and update your database with them., (*9)
Laravel 4 Task, (*10)
// Coming soon, feel free to PR this
Laravel 3 Task, (*11)
<?php
class Schema_Task {
public function update($arguments)
{
$params = array(
'dbuser' => Config::get('database.connections.mysql.username'),
'dbpass' => Config::get('database.connections.mysql.password'),
'dbname' => Config::get('database.connections.mysql.database'),
'dbhost' => Config::get('database.connections.mysql.host')
);
$files = scandir(Config::get('schemas.path'));
$differences = 0;
foreach ($files as $file) {
if (in_array($file, ['.','..','.DS_Store'])) {
continue;
}
$params['dumpxml'] = Config::get('schemas.path') . '/' . $file;
try {
$diff = new CloudDueling\AutoMigrate\MySQL($params);
$diff_lines = $diff->getSQLDiffs();
if (count($diff_lines) == 0) {
continue;
}
++$differences;
echo "Difference found: {$params['dumpxml']}\n" .
" - " . implode("\n - ", $diff_lines) . "\n\n";
$diff->runSQLDiff();
} catch(Exception $e) {
echo $e->getMessage() . "\n";
exit;
}
}
echo "Differences found: {$differences}\n";
}
}