summaryrefslogtreecommitdiffstats
path: root/install.php
diff options
context:
space:
mode:
authorSimon Rettberg2019-03-06 17:29:38 +0100
committerSimon Rettberg2019-03-06 17:29:38 +0100
commit5826d219772ffaccd62a9177af044edb81ac41b6 (patch)
treea66ef5fb5508f89aeddca016b4c5404d808d490e /install.php
parent[statistics] Actually convert all old UUID fallback formats (diff)
downloadslx-admin-5826d219772ffaccd62a9177af044edb81ac41b6.tar.gz
slx-admin-5826d219772ffaccd62a9177af044edb81ac41b6.tar.xz
slx-admin-5826d219772ffaccd62a9177af044edb81ac41b6.zip
install.php: Resolve conflicts before adding constraint
Diffstat (limited to 'install.php')
-rw-r--r--install.php15
1 files changed, 14 insertions, 1 deletions
diff --git a/install.php b/install.php
index c471f4b6..47084528 100644
--- a/install.php
+++ b/install.php
@@ -180,6 +180,19 @@ function tableAddConstraint($table, $column, $refTable, $refColumn, $actions, $i
// Kill the old one
$ret = tableDeleteConstraint($table, $test['CONSTRAINT_NAME']);
}
+ if ($delete === 'CASCADE') {
+ // Deletes are cascaded, so make sure first that all rows get purged that would
+ // violate the constraint
+ Database::exec("DELETE `$table` FROM `$table`
+ LEFT JOIN `$refTable` ON (`$table`.`$column` = `$refTable`.`$refColumn`)
+ WHERE `$refTable`.`$refColumn` IS NULL");
+ } elseif ($delete === 'SET NULL') {
+ // Similar to above; SET NULL constraint, so do that for violating entries
+ Database::exec("UPDATE `$table`
+ LEFT JOIN `$refTable` ON (`$table`.`$column` = `$refTable`.`$refColumn`)
+ SET `$table`.`$column` = NULL
+ WHERE `$refTable`.`$refColumn` IS NULL");
+ }
// Need to create
$ret = Database::exec("ALTER TABLE `$table` ADD CONSTRAINT FOREIGN KEY (`$column`)
REFERENCES `$refTable` (`$refColumn`)
@@ -188,7 +201,7 @@ function tableAddConstraint($table, $column, $refTable, $refColumn, $actions, $i
if ($ignoreError) {
return UPDATE_FAILED;
} else {
- finalResponse(UPDATE_FAILED, 'DB: Cannot add constraint: ' . Database::lastError());
+ finalResponse(UPDATE_FAILED, "Cannot add constraint $table.$column -> $refTable.$refColumn: " . Database::lastError());
}
}
return UPDATE_DONE;