Skip to content

Commit 5090ba5

Browse files
committed
Merge branch '4'
2 parents fab40b0 + 9607025 commit 5090ba5

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

src/Dev/FocusPointMigrationTask.php

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,36 +49,61 @@ protected function changeDbFields($from, $to, $message)
4949

5050
// Safety net
5151
if (!isset($fields["{$to}X"]) || !isset($fields["{$to}Y"])) {
52-
throw new \Exception("$imageTable table does not have \"{$to}X\" and \"{$to}Y\" fields. Did you run dev/build?");
52+
throw new \Exception("{$imageTable} table does not have \"{$to}X\" and \"{$to}Y\" fields. Did you run dev/build?");
5353
}
5454

5555
// Update all Image tables
5656
$imageTables = [
5757
$imageTable,
58-
$imageTable . "_" . Versioned::LIVE,
59-
$imageTable . "_Versions",
58+
$imageTable . '_' . Versioned::LIVE,
59+
$imageTable . '_Versions',
6060
];
6161

62-
DB::get_conn()->withTransaction(function() use ($imageTables, $from, $to, $message) {
62+
$success = false;
63+
DB::get_conn()->withTransaction(function () use ($imageTables, $from, $to, &$success) {
6364
$oldColumnX = "\"{$from}X\"";
6465
$oldColumnY = "\"{$from}Y\"";
6566
$newColumnX = "\"{$to}X\"";
6667
$newColumnY = "\"{$to}Y\"";
6768

68-
foreach ($imageTables as $imageTable) {
69-
$query = SQLUpdate::create("\"$imageTable\"")
70-
->assignSQL($newColumnX, $oldColumnX)
71-
->assignSQL($newColumnY, "$oldColumnY * -1");
69+
foreach ($imageTables as $table) {
70+
$fields = DB::field_list($table);
71+
$hasOldColumns = isset($fields["{$from}X"]) && isset($fields["{$from}Y"]);
7272

73-
$query->execute();
73+
if ($hasOldColumns) {
74+
$query = SQLUpdate::create("\"{$table}\"")
75+
->assignSQL($newColumnX, $oldColumnX)
76+
->assignSQL($newColumnY, "{$oldColumnY} * -1");
7477

75-
DB::query("ALTER TABLE \"$imageTable\" DROP COLUMN $oldColumnX");
76-
DB::query("ALTER TABLE \"$imageTable\" DROP COLUMN $oldColumnY");
78+
$query->execute();
79+
80+
continue;
81+
}
82+
83+
DB::get_schema()->alterationMessage("{$table} does not have {$oldColumnX} and {$oldColumnY} fields. Skipping...", 'notice');
7784
}
7885

79-
DB::get_schema()->alterationMessage($message, 'changed');
80-
} , function () {
86+
$success = true;
87+
}, function () {
8188
DB::get_schema()->alterationMessage('Failed to alter FocusPoint fields', 'error');
8289
}, false, true);
90+
91+
if ($success) {
92+
foreach ($imageTables as $table) {
93+
$fields = DB::field_list($table);
94+
$hasOldColumns = isset($fields["{$from}X"]) && isset($fields["{$from}Y"]);
95+
96+
if ($hasOldColumns) {
97+
$oldColumnX = "\"{$from}X\"";
98+
$oldColumnY = "\"{$from}Y\"";
99+
100+
DB::query("ALTER TABLE \"{$table}\" DROP COLUMN {$oldColumnX}");
101+
DB::query("ALTER TABLE \"{$table}\" DROP COLUMN {$oldColumnY}");
102+
103+
DB::get_schema()->alterationMessage("Dropped {$oldColumnX} and {$oldColumnY} from {$table}", 'changed');
104+
}
105+
}
106+
DB::get_schema()->alterationMessage($message, 'changed');
107+
}
83108
}
84109
}

0 commit comments

Comments
 (0)