From 26ed25748481668494ad57c5d93c951a99cfb292 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 24 Nov 2017 12:57:02 +0100 Subject: Installer: Add more helper functions (constraints, response) --- install.php | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) (limited to 'install.php') diff --git a/install.php b/install.php index 7937ec38..1345a6bd 100644 --- a/install.php +++ b/install.php @@ -99,7 +99,7 @@ function tableRename($old, $new) { * @param string $refColumn referenced column * @return false|string[] list of constraints matching the request, false on error */ -function tableGetContraints($table, $column, $refTable, $refColumn) +function tableGetConstraints($table, $column, $refTable, $refColumn) { $db = 'openslx'; if (defined('CONFIG_SQL_DB')) { @@ -119,6 +119,46 @@ function tableGetContraints($table, $column, $refTable, $refColumn) return $res->fetchAll(PDO::FETCH_COLUMN, 0); } +/** + * Because I'm stupid and can't type properly. + */ +function tableGetContraints($table, $column, $refTable, $refColumn) +{ + return tableGetConstraints($table, $column, $refTable, $refColumn); +} + +/** + * Add constraint to table if it doesn't exist already. + * On failure, trigger finalResponse with error message. + * + * @param string $table table to add constraint to + * @param string $column foreign key column of that table + * @param string $refTable destination table + * @param string $refColumn primary key column in destination table + * @param string $actions "ON xxx ON yyy" string + * @return string UPDATE_* result code + */ +function tableAddConstraint($table, $column, $refTable, $refColumn, $actions) +{ + $test = tableGetConstraints($table, $column, $refTable, $refColumn); + if ($test === false) { + // Most likely, destination table does not exist yep + return UPDATE_RETRY; + } + if (!empty($test)) { + // Already exists + return UPDATE_NOOP; + } + // Need to create + $ret = Database::exec("ALTER TABLE `$table` ADD CONSTRAINT FOREIGN KEY (`$column`) + REFERENCES `$refTable` (`$refColumn`) + $actions"); + if ($ret === false) { + finalResponse(UPDATE_FAILED, 'DB-Error: ' . Database::lastError()); + } + return UPDATE_DONE; +} + /** * Drop constraint from a table. * @@ -146,6 +186,22 @@ function tableCreate($table, $structure, $fatalOnError = true) return UPDATE_FAILED; } +function responseFromArray($array) +{ + if (in_array(UPDATE_FAILED, $array)) { + finalResponse(UPDATE_FAILED, 'Update failed!'); + } + if (in_array(UPDATE_RETRY, $array)) { + finalResponse(UPDATE_RETRY, 'Temporary failure, will try again.'); + } + if (in_array(UPDATE_DONE, $array)) { + finalResponse(UPDATE_DONE, 'Tables created/updated successfully'); + } + + finalResponse(UPDATE_NOOP, 'Everything already up to date'); + +} + /* * Rest of install script.... */ -- cgit v1.2.3-55-g7522