Last active
March 5, 2019 22:59
-
-
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
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 | |
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; | |
} | |
?> |
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
How do you use this file? where does findAllDatabaseXmlFiles function come?