Skip to content

Commit

Permalink
Add resolver for syncing database changes
Browse files Browse the repository at this point in the history
  • Loading branch information
hugopeek committed Nov 19, 2021
1 parent b5b10b8 commit 8ab3cce
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 1 deletion.
3 changes: 2 additions & 1 deletion _build/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@
"after": [
"setresourceids.php",
"setinputoptions.php",
"corrections.php"
"corrections.php",
"synctables.php"
]
}
}
Expand Down
138 changes: 138 additions & 0 deletions _build/resolvers/synctables.php
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 modified _packages/romanescobackyard-1.0.0-beta8.transport.zip
Binary file not shown.
1 change: 1 addition & 0 deletions core/components/romanescobackyard/docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Backyard 1.0.0-beta8
Released on November 19, 2021

- Add resolver for syncing database changes
- Remove unnecessary migx_id and pos values in MIGX configs
- Fix aggregate keys for Author in schema (don't use alias field!)
- Improve data formatting and descriptions in Related Content grids
Expand Down

0 comments on commit 8ab3cce

Please sign in to comment.