-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add version 3.x to version 4.x data migration script
- Loading branch information
1 parent
fa62279
commit 34bae74
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters