Skip to content

Commit

Permalink
Add version 3.x to version 4.x data migration script
Browse files Browse the repository at this point in the history
  • Loading branch information
michalkleiner committed Mar 15, 2021
1 parent fa62279 commit 34bae74
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
3 changes: 3 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ SilverStripe\Control\Director:
rules:
at-taxonomy-overview: Chrometoaster\AdvancedTaxonomies\Controllers\TaxonomyOverviewController

Chrometoaster\AdvancedTaxonomies\Dev\AT4xMigrationTask:
enable_v4_migration: true

---
Only:
moduleexists: 'silverstripe/cms'
Expand Down
111 changes: 111 additions & 0 deletions src/Dev/AT4xMigrationTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Chrometoaster\AdvancedTaxonomies\Dev;

use Chrometoaster\AdvancedTaxonomies\Models\BaseObject;
use Chrometoaster\AdvancedTaxonomies\Models\BaseTerm;
use Chrometoaster\AdvancedTaxonomies\Models\TaxonomyTerm;
use Exception;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\BuildTask;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
use SilverStripe\Versioned\Versioned;

class AT4xMigrationTask extends BuildTask
{
private static $segment = 'AT4x-migration-task';

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

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


/**
* Static run wrapper with a dummy request
*
* @throws Exception
*/
public static function migrate()
{
return self::create()->run(new HTTPRequest('GET', '/'));
}


/**
* @param HTTPRequest $request
* @throws Exception
*/
public function run($request)
{
if (!Config::forClass(self::class)->get('enable_v4_migration')) {
DB::get_schema()->alterationMessage('Data migration to Advanced Taxonomies 4.x format is disabled.', 'notice');

return;
}

$schema = DataObject::getSchema();
$termTable = $schema->tableName(TaxonomyTerm::class);
$baseObjectTable = $schema->tableName(BaseObject::class);
$baseTermTable = $schema->tableName(BaseTerm::class);

// Safety net
if (!($baseObjectTable && $baseTermTable && $termTable)) {
throw new Exception(sprintf("One of the required db tables (%s, %s, %s) doesn't exist, did you run dev/build with a flush param?", $baseObjectTable, $baseTermTable, $termTable));
}

$versionedFields = array_keys(Config::inst()->get(Versioned::class, 'db_for_versions_table'));

DB::get_conn()->withTransaction(function () use ($schema, $termTable, $versionedFields) {

// cater for standard versioning db table suffiixes
$dbTableSuffixes = [
'',
'_' . Versioned::LIVE,
'_Versions', // add RecordID and Version fields
];

foreach ($dbTableSuffixes as $dbTableSuffix) {
foreach ([BaseObject::class, BaseTerm::class] as $model) {

// get db table with suffix and all uninherited db fields
$dbTable = $schema->tableName($model) . $dbTableSuffix;
$dbFields = array_keys($schema->databaseFields($model, false));

// add special versioning fields
if ($dbTableSuffix === '_Versions') {
if (get_parent_class($model) === DataObject::class) {
array_push($dbFields, ...$versionedFields);
} else {
array_push($dbFields, 'RecordID', 'Version');
}
}

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

// make sure the table is empty to avoid foreign key conflicts
DB::query(sprintf('DELETE FROM "%s"', $dbTable));

// create a list of unique db table fields
$dbFieldsList = implode('","', array_unique($dbFields));

// prepare the insert query
$sql = sprintf(
'INSERT INTO "%s" ("%s") SELECT "%s" FROM "%s"',
$dbTable,
$dbFieldsList,
$dbFieldsList,
$termTable . $dbTableSuffix
);

DB::query($sql);
}
}

DB::get_schema()->alterationMessage('Taxonomy terms data migrated successfully into Advanced Taxonomies 4.x format.', 'changed');
}, function () {
DB::get_schema()->alterationMessage('Failed to migrate taxonomy terms data to Advanced Taxonomies 4.x format.', 'error');
}, false, true);
}
}
13 changes: 13 additions & 0 deletions src/Models/BaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Chrometoaster\AdvancedTaxonomies\Models;

use Chrometoaster\AdvancedTaxonomies\Dev\AT4MigrationTask;
use Chrometoaster\AdvancedTaxonomies\Generators\URLSegmentGenerator;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\RequiredFields;
use SilverStripe\i18n\i18n;
Expand Down Expand Up @@ -285,4 +287,15 @@ public function providePermissions()
],
];
}


/**
* Trigger the 3.x to 4.x data migration (when enabled)
*/
public function requireDefaultRecords()
{
parent::requireDefaultRecords();

AT4MigrationTask::migrate();
}
}

0 comments on commit 34bae74

Please sign in to comment.