From f2e6cceaa8a8dd4b2cc2e2777980c45a42ef9624 Mon Sep 17 00:00:00 2001 From: Michal Kleiner Date: Fri, 30 Apr 2021 00:24:41 +1200 Subject: [PATCH 1/2] Update the migration script to better handle fresh and empty installs --- src/Dev/AT4xMigrationTask.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Dev/AT4xMigrationTask.php b/src/Dev/AT4xMigrationTask.php index 5575ae3..2857e33 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 @@ -68,6 +69,14 @@ public function run($request) 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 disable the migration completely and hide this message, set AT4xMigrationTask::enable_v4_migration to false.', 'notice'); + + return; + } + $versionedFields = array_keys(Config::inst()->get(Versioned::class, 'db_for_versions_table')); $termTableFields = DB::query(sprintf('SHOW COLUMNS FROM "%s"', $termTable))->column(); @@ -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 From b41057d4e269de8e22ab4369500b280d3e5a4c0f Mon Sep 17 00:00:00 2001 From: Michal Kleiner Date: Fri, 30 Apr 2021 00:37:10 +1200 Subject: [PATCH 2/2] Tweak wording of the DB alteration messages --- src/Dev/AT4xMigrationTask.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Dev/AT4xMigrationTask.php b/src/Dev/AT4xMigrationTask.php index 2857e33..874dd80 100644 --- a/src/Dev/AT4xMigrationTask.php +++ b/src/Dev/AT4xMigrationTask.php @@ -64,7 +64,7 @@ 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; } @@ -72,7 +72,7 @@ public function run($request) // 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 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, e.g. for fresh installs, set AT4xMigrationTask::enable_v4_migration to false.', 'notice'); return; }