Skip to content

Commit

Permalink
Merge pull request #27 from chrometoasters/pulls/26-fresh-install-mig…
Browse files Browse the repository at this point in the history
…ration

Update the migration script to better handle fresh and empty installs
  • Loading branch information
michalkleiner authored Apr 29, 2021
2 parents 0039e6d + b41057d commit 0f0624b
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/Dev/AT4xMigrationTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class AT4xMigrationTask extends BuildTask

protected $description = 'Migrate AT db data from version 3.x to version 4.x';

protected $basicFields = ['ID', 'RecordID', 'Version'];

/**
* Static run wrapper with a dummy request
Expand Down Expand Up @@ -63,7 +64,15 @@ public function run($request)
// Skip migration when BaseObject or BaseTerm have any data in them
if ($baseObjectsCount || $baseTermsCount) {
DB::get_schema()->alterationMessage("BaseObject or BaseTerm table already contains data, skipping the migration.", 'notice');
DB::get_schema()->alterationMessage('If you want disable the migration completely and hide this message, set AT4xMigrationTask::enable_v4_migration to false.', 'notice');
DB::get_schema()->alterationMessage('If you want to disable the migration completely and hide this message, set AT4xMigrationTask::enable_v4_migration to false.', 'notice');

return;
}

// Skip migration if there's no data at all
if (($baseObjectsCount == $baseTermsCount) && ($baseObjectsCount == $termsCount) && ($baseObjectsCount == 0)) {
DB::get_schema()->alterationMessage("There's no data to migrate, skipping the migration.", 'notice');
DB::get_schema()->alterationMessage('If you want to disable the migration completely, e.g. for fresh installs, set AT4xMigrationTask::enable_v4_migration to false.', 'notice');

return;
}
Expand Down Expand Up @@ -93,10 +102,23 @@ public function run($request)
if (get_parent_class($model) === DataObject::class) {
array_push($dbFields, ...$versionedFields);
} else {
array_push($dbFields, 'RecordID', 'Version');
array_push($dbFields, ...$this->basicFields);
}
}


$currentTermTableFields = DB::query(sprintf('SHOW COLUMNS FROM "%s"', $termTable . $dbTableSuffix))->column();
$dbFieldsDiff = array_intersect(array_unique($dbFields), $currentTermTableFields);

// if there are no columns to migrate, i.e. if the only fields are the basic versioning related field
// and an ID, skip the table for the current model
if (empty($dbFieldsDiff) || empty(array_diff($dbFieldsDiff, $this->basicFields)) || ($dbFieldsDiff == ['ID'])) {
DB::get_schema()->alterationMessage(sprintf('No column data to migrate to %s table.', $dbTable), 'notice');

continue;
}


DB::get_schema()->alterationMessage(sprintf('Migrating data to %s table.', $dbTable), 'changed');

// make sure the table is empty to avoid foreign key conflicts
Expand Down

0 comments on commit 0f0624b

Please sign in to comment.