-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add resolver for syncing database changes
- Loading branch information
Showing
4 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
/** | ||
* Resolve sync tables | ||
* | ||
* @package romanescobackyard | ||
* @subpackage build | ||
* | ||
* @var mixed $object | ||
* @var array $options | ||
*/ | ||
|
||
set_time_limit(0); | ||
|
||
if (!function_exists('updateTableColumns')) { | ||
/** | ||
* @param modX $modx | ||
* @param string $table | ||
*/ | ||
function updateTableColumns($modx, $table) | ||
{ | ||
$tableName = $modx->getTableName($table); | ||
$tableName = str_replace('`', '', $tableName); | ||
$dbname = $modx->getOption('dbname'); | ||
|
||
$c = $modx->prepare("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = :dbName AND table_name = :tableName"); | ||
$c->bindParam(':dbName', $dbname); | ||
$c->bindParam(':tableName', $tableName); | ||
$c->execute(); | ||
|
||
$unusedColumns = $c->fetchAll(PDO::FETCH_COLUMN, 0); | ||
$unusedColumns = array_flip($unusedColumns); | ||
|
||
$meta = $modx->getFieldMeta($table); | ||
$columns = array_keys($meta); | ||
|
||
$m = $modx->getManager(); | ||
|
||
foreach ($columns as $column) { | ||
if (isset($unusedColumns[$column])) { | ||
$m->alterField($table, $column); | ||
$modx->log(modX::LOG_LEVEL_INFO, ' -- Altered column: ' . $column); | ||
unset($unusedColumns[$column]); | ||
} else { | ||
$m->addField($table, $column); | ||
$modx->log(modX::LOG_LEVEL_INFO, ' -- Added column: ' . $column); | ||
} | ||
} | ||
|
||
foreach ($unusedColumns as $column => $v) { | ||
$m->removeField($table, $column); | ||
$modx->log(modX::LOG_LEVEL_INFO, ' -- Removed column: ' . $column); | ||
} | ||
} | ||
} | ||
|
||
if (!function_exists('updateTableIndexes')) { | ||
/** | ||
* @param modX $modx | ||
* @param string $table | ||
*/ | ||
function updateTableIndexes($modx, $table) | ||
{ | ||
$tableName = $modx->getTableName($table); | ||
$tableName = str_replace('`', '', $tableName); | ||
$dbname = $modx->getOption('dbname'); | ||
|
||
$c = $modx->prepare("SELECT DISTINCT INDEX_NAME FROM INFORMATION_SCHEMA.STATISTICS WHERE table_schema = :dbName AND table_name = :tableName AND INDEX_NAME != 'PRIMARY'"); | ||
$c->bindParam(':dbName', $dbname); | ||
$c->bindParam(':tableName', $tableName); | ||
$c->execute(); | ||
|
||
$oldIndexes = $c->fetchAll(PDO::FETCH_COLUMN, 0); | ||
|
||
$m = $modx->getManager(); | ||
|
||
foreach ($oldIndexes as $oldIndex) { | ||
$m->removeIndex($table, $oldIndex); | ||
$modx->log(modX::LOG_LEVEL_INFO, ' -- Removed index: ' . $oldIndex); | ||
} | ||
|
||
$meta = $modx->getIndexMeta($table); | ||
$indexes = array_keys($meta); | ||
|
||
foreach ($indexes as $index) { | ||
if ($index == 'PRIMARY') continue; | ||
$m->addIndex($table, $index); | ||
$modx->log(modX::LOG_LEVEL_INFO, ' -- Added index: ' . $index); | ||
} | ||
} | ||
} | ||
|
||
if (!function_exists('alterTable')) { | ||
/** | ||
* @param modX $modx | ||
* @param string $table | ||
*/ | ||
function alterTable($modx, $table) | ||
{ | ||
$modx->log(modX::LOG_LEVEL_INFO, ' - Updating columns:'); | ||
updateTableColumns($modx, $table); | ||
|
||
$modx->log(modX::LOG_LEVEL_INFO, ' - Updating indexes:'); | ||
updateTableIndexes($modx, $table); | ||
} | ||
} | ||
|
||
if ($object->xpdo) { | ||
switch ($options[xPDOTransport::PACKAGE_ACTION]) { | ||
case xPDOTransport::ACTION_UPGRADE: | ||
/** @var modX $modx */ | ||
$modx =& $object->xpdo; | ||
|
||
$tables = [ | ||
"rmTimeline", | ||
"rmTimelineProject", | ||
"rmNote", | ||
"rmNoteImprovement", | ||
"rmNoteIssue", | ||
"rmCrosslink", | ||
"rmCrosslinkRelated", | ||
"rmCrosslinkRepurpose", | ||
"rmExternalLink", | ||
"rmOption", | ||
"rmOptionGroup" | ||
]; | ||
|
||
$modelPath = $modx->getOption('romanescobackyard.core_path', null, $modx->getOption('core_path') . 'components/romanescobackyard/') . 'model/'; | ||
$modx->addPackage('romanescobackyard', $modelPath); | ||
|
||
foreach ($tables as $table) { | ||
$modx->log(modX::LOG_LEVEL_INFO, 'Altering table: ' . $table); | ||
alterTable($modx, $table); | ||
} | ||
|
||
break; | ||
} | ||
} | ||
return true; |
Binary file not shown.
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