diff --git a/Migrations/AnnotationsMigration.php b/Migrations/AnnotationsMigration.php index 728bc6f..dcff4c8 100644 --- a/Migrations/AnnotationsMigration.php +++ b/Migrations/AnnotationsMigration.php @@ -9,25 +9,51 @@ namespace Piwik\Plugins\Migration\Migrations; +use Piwik\Common; use Piwik\Option; use Piwik\Plugins\Annotations\AnnotationList; use Piwik\Plugins\Migration\TargetDb; class AnnotationsMigration extends BaseMigration { + /** + * @param TargetDb $targetDb + * @return array + */ public function validateStructure(TargetDb $targetDb) { - return array(); + // since Matomo 5.5.0, annotations are stored in their own table + // and the AnnotationList class had been removed + if (!class_exists(AnnotationList::class)) { + return $this->checkTablesHaveSameStructure($targetDb, 'annotations'); + } + + return []; } + /** + * @param Request $request + * @param TargetDb $targetDb + * @return void + */ public function migrate(Request $request, TargetDb $targetDb) { + // since Matomo 5.5.0, annotations are stored in their own table + // and the AnnotationList class had been removed + if (!class_exists(AnnotationList::class)) { + $this->migrateEntities($request, $targetDb, 'annotations', 'annotations', 'id'); + + return; + } + + // before Matomo 5.5.0, annotations were stored in the options table $sourceName = AnnotationList::getAnnotationCollectionOptionName($request->sourceIdSite); $targetName = AnnotationList::getAnnotationCollectionOptionName($request->targetIdSite); $annotations = Option::get($sourceName); if ($annotations) { - $this->log('Found annotations'); + $annotationItems = Common::safe_unserialize($annotations) ?: []; + $this->log(sprintf('Found %d annotations', count($annotationItems))); $targetDb->insert('option', array( 'option_name' => $targetName, diff --git a/tests/System/Commands/MigrateTest.php b/tests/System/Commands/MigrateTest.php index 5a04eff..cb8e5f6 100644 --- a/tests/System/Commands/MigrateTest.php +++ b/tests/System/Commands/MigrateTest.php @@ -56,7 +56,7 @@ public function test_runMigration() Found 2 segments at 2019-01-10 02:48:01 Processed SegmentsMigration at 2019-01-10 02:48:01 Processing AnnotationsMigration at 2019-01-10 02:48:01 -Found annotations at 2019-01-10 02:48:01 +Found 1 annotations at 2019-01-10 02:48:01 Processed AnnotationsMigration at 2019-01-10 02:48:01 Processing CustomDimensionMigration at 2019-01-10 02:48:01 Found 3 custom dimensions at 2019-01-10 02:48:01 @@ -138,6 +138,14 @@ public function test_runMigration_actuallyCopiesValues() $archives = $targetDb->fetchAll('SELECT * FROM ' . self::$fixture->targetDb->prefixTable('archive_blob_2013_01')); $this->assertGreaterThanOrEqual(195, count($archives)); $this->assertLessThanOrEqual(600, count($archives)); + + if (!class_exists('Piwik\Plugins\Annotations\AnnotationList')) { + $annotationsTable = self::$fixture->targetDb->prefixTable('annotations'); + $this->assertContains($annotationsTable, $targetDb->listTables(), 'annotations table does not exist'); + + $annotationItems = $targetDb->fetchAll('SELECT * FROM ' . $annotationsTable); + $this->assertCount(1, $annotationItems); + } } /**