Skip to content

Instantly share code, notes, and snippets.

@chris03
Last active March 5, 2019 22:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chris03/af05e345bedf254fded5 to your computer and use it in GitHub Desktop.
Save chris03/af05e345bedf254fded5 to your computer and use it in GitHub Desktop.
SQL Migration Script for Concrete5 lowercase tables names to original CamelCase table names
<?php
header("Content-Type:text/plain");
print '-- SQL Migration Script for Concrete5 lowercase tables names to original table names' . PHP_EOL;
print '-- By Chris03' . PHP_EOL;
print '-- Note: When running with phpMyAdmin, you should temporary enable: Settings -> SQL Queries -> Ignore multiple statement errors ' . PHP_EOL;
$fileNames = findAllDatabaseXmlFiles('concrete');
$tableFiles = getTablesFromFiles($fileNames);
printStats($tableFiles);
foreach($tableFiles as $tables)
{
printSqlQueries($tables);
}
/// Functions ///
function getTablesFromFiles($files)
{
$tables = array();
foreach ($files as $fileName)
{
$tables[$fileName] = getTableNamesFromFile($fileName);
}
return $tables;
}
function printStats($tables)
{
$totalFiles = count($tables);
$totalTables = count($tables, COUNT_RECURSIVE) - $totalFiles;
print '----------------------------------------------' . PHP_EOL;
echo "-- Total files: $totalFiles" . PHP_EOL;
echo "-- Total tables: $totalTables" . PHP_EOL;
print '----------------------------------------------' . PHP_EOL;
}
function printSqlQueries($tables)
{
foreach ($tables as $t)
{
$tLower = strtolower($t);
print "ALTER TABLE $tLower RENAME $t;" . PHP_EOL;
}
}
function getTableNamesFromFile($fileName) {
$tables = array();
if($xml = simplexml_load_file($fileName))
{
$i = 0;
foreach ($xml->children() as $t)
{
$tables[$i] = $t['name'];
$i++;
}
}
else
{
print 'Failed to open xml document.';
}
return $tables;
}
function findAllDatabaseXmlFiles($basePath)
{
$directory = new RecursiveDirectoryIterator($basePath);
$files = new RecursiveIteratorIterator($directory);
$dbFiles = array();
$i = 0;
foreach($files as $file)
{
if ($file->getFilename() == 'db.xml')
{
$dbFiles[$i] = $file->getPathname();
$i++;
}
}
return $dbFiles;
}
?>
@madalinignisca
Copy link

How do you use this file? where does findAllDatabaseXmlFiles function come?

@matteason
Copy link

Thanks for sharing this - you just saved me hours of frustration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment