diff options
author | Simon Rettberg | 2017-11-24 12:57:02 +0100 |
---|---|---|
committer | Simon Rettberg | 2017-11-24 12:57:02 +0100 |
commit | 26ed25748481668494ad57c5d93c951a99cfb292 (patch) | |
tree | cb51350698a5ba1b243fdd05c62829e725349c72 | |
parent | [baseconfig_bwlp] Add new config vars (diff) | |
download | slx-admin-26ed25748481668494ad57c5d93c951a99cfb292.tar.gz slx-admin-26ed25748481668494ad57c5d93c951a99cfb292.tar.xz slx-admin-26ed25748481668494ad57c5d93c951a99cfb292.zip |
Installer: Add more helper functions (constraints, response)
-rw-r--r-- | install.php | 58 |
1 files changed, 57 insertions, 1 deletions
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')) { @@ -120,6 +120,46 @@ function tableGetContraints($table, $column, $refTable, $refColumn) } /** + * 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. * * @param string $table table name @@ -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.... */ |