Skip to content

Commit

Permalink
Fix migration task to update all Image tables
Browse files Browse the repository at this point in the history
  • Loading branch information
jonom committed Jun 12, 2020
1 parent 97f25c6 commit 0b80b67
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/Dev/FocusPointMigrationTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\Queries\SQLUpdate;
use SilverStripe\Versioned\Versioned;

class FocusPointMigrationTask extends MigrationTask
{
Expand Down Expand Up @@ -51,20 +52,29 @@ protected function changeDbFields($from, $to, $message)
throw new \Exception("$imageTable table does not have \"{$to}X\" and \"{$to}Y\" fields. Did you run dev/build?");
}

DB::get_conn()->withTransaction(function() use ($imageTable, $from, $to, $message) {
// Update all Image tables
$imageTables = [
$imageTable,
$imageTable . "_" . Versioned::LIVE,
$imageTable . "_Versions",
];

DB::get_conn()->withTransaction(function() use ($imageTables, $from, $to, $message) {
$oldColumnX = "\"{$from}X\"";
$oldColumnY = "\"{$from}Y\"";
$newColumnX = "\"{$to}X\"";
$newColumnY = "\"{$to}Y\"";

$query = SQLUpdate::create("\"$imageTable\"")
->assignSQL($newColumnX, $oldColumnX)
->assignSQL($newColumnY, "$oldColumnY * -1");
foreach ($imageTables as $imageTable) {
$query = SQLUpdate::create("\"$imageTable\"")
->assignSQL($newColumnX, $oldColumnX)
->assignSQL($newColumnY, "$oldColumnY * -1");

$query->execute();
$query->execute();

DB::query("ALTER TABLE \"$imageTable\" DROP COLUMN $oldColumnX");
DB::query("ALTER TABLE \"$imageTable\" DROP COLUMN $oldColumnY");
DB::query("ALTER TABLE \"$imageTable\" DROP COLUMN $oldColumnX");
DB::query("ALTER TABLE \"$imageTable\" DROP COLUMN $oldColumnY");
}

DB::get_schema()->alterationMessage($message, 'changed');
} , function () {
Expand Down

0 comments on commit 0b80b67

Please sign in to comment.