diff --git a/src/Dev/AT4xMigrationTask.php b/src/Dev/AT4xMigrationTask.php index 5575ae3..874dd80 100644 --- a/src/Dev/AT4xMigrationTask.php +++ b/src/Dev/AT4xMigrationTask.php @@ -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 @@ -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; } @@ -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