From c83172a3c8d79aeb0f6483162180289739f78277 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Mon, 6 Jan 2020 16:29:32 +0100 Subject: [PATCH] Idempotent modification of the database Fixes #3125 --- com.woltlab.wcf/files_pre.tar | Bin 200192 -> 201216 bytes com.woltlab.wcf/update_5.2.sql | 4 +- ...ate_com.woltlab.wcf_5.2_reactionUpdate.php | 41 +++++++++++++++--- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/com.woltlab.wcf/files_pre.tar b/com.woltlab.wcf/files_pre.tar index 5a42367369b780ab8b6e02c2c754bacd99c6d319..4d8e91d117a10bcb5585328b63907786ea89462f 100644 GIT binary patch delta 1991 zcmZWq%}*Og6vsn?w3as|X_N*?#Q+-?s4;6CATff5!a@y}O~DDOAVS0L*k0kU><1re zD^S{VD%F)ns;Vl7sOqh)+DqG0|AVTBUV3N`J?4;-Lih+u->hx0iM4n|DPdq=O2Txmq%8=4u7Yq$5HwFiS!G5JqQGx?Q!Tvg>c(Gpp!9yB6tYHTQ znnkZ;QM|4tmvmy2rHrvkm!j}BEVAehYWIjNw}ZOb7k_!0eSNsC@)6ljJLn#zG@RM?0b4dFYdv&iDReprZAni(hk9M4z<>4ihci~=WEOj`b02}7i=S+ zQ+316o8hpLE#ytRYf)srUDVPjdhc})b1U22#2<)W*3pixL*Rv7G?Ep)f|0~s(a4Ot ztj5OPiK@#Jk$JxD=C*&zecg0rkHLRmTlxxxR5ppWN+lK##e$1CS5b~j)k2< z$|O1***47x#yxZq5^UQ;?d(o3 zx>4`P!Qy)x%^c(kTzs>KZk4_M*+X$S2|ah02hWiV7{`6n?Jl)w6_tv*nn{UGxw^0K zqqo3!bRPv_$?u~KEPui-;yMd*foQ8dkqH@(jN+i*JuY+q2sZo=9?80&!6Udm5}Q}& z5;!syi-RDMSR#(u)B$R(2OE|;K*}ZfA>18{-cjc(aCPh* zJ_jZud;b`9Kv@aGyqa*?)WShRZ}Z#A+vV+Xmt-8uFWSMe%=jq)vWv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644 --- a/com.woltlab.wcf/update_5.2.sql +++ b/com.woltlab.wcf/update_5.2.sql @@ -1,2 +1,2 @@ -INSERT INTO wcf1_style_variable (variableName, defaultValue) VALUES ('wcfEditorTableBorder', 'rgba(221, 221, 221, 1)'); -INSERT INTO wcf1_style_variable (variableName, defaultValue) VALUES ('wcfPageThemeColor', ''); -- uses `$wcfHeaderBackground` if left empty +INSERT IGNORE INTO wcf1_style_variable (variableName, defaultValue) VALUES ('wcfEditorTableBorder', 'rgba(221, 221, 221, 1)'); +INSERT IGNORE INTO wcf1_style_variable (variableName, defaultValue) VALUES ('wcfPageThemeColor', ''); -- uses `$wcfHeaderBackground` if left empty diff --git a/wcfsetup/install/files/acp/update_com.woltlab.wcf_5.2_reactionUpdate.php b/wcfsetup/install/files/acp/update_com.woltlab.wcf_5.2_reactionUpdate.php index 9ef5c88efa..4c4338571f 100644 --- a/wcfsetup/install/files/acp/update_com.woltlab.wcf_5.2_reactionUpdate.php +++ b/wcfsetup/install/files/acp/update_com.woltlab.wcf_5.2_reactionUpdate.php @@ -27,6 +27,12 @@ try { $reactions = ['like', 'thanks', 'haha', 'confused', 'sad']; if (LIKE_ENABLE_DISLIKE) { + // Remove the existing phrase in case a previous upgrade attempt has failed. + $sql = "DELETE FROM wcf".WCF_N."_language_item + WHERE languageItem = ?"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute(['wcf.reactionType.title6']); + $reactions[] = 'thumbsDown'; $sql = "SELECT languageCategoryID @@ -50,9 +56,9 @@ try { } } - $sql = "INSERT INTO wcf".WCF_N."_reaction_type - (reactionTypeID, title, showOrder, iconFile) - VALUES (?, ?, ?, ?)"; + $sql = "INSERT IGNORE INTO wcf".WCF_N."_reaction_type + (reactionTypeID, title, showOrder, iconFile) + VALUES (?, ?, ?, ?)"; $statement = WCF::getDB()->prepareStatement($sql); for ($i = 0, $length = count($reactions); $i < $length; $i++) { $reactionTypeID = $i + 1; @@ -90,8 +96,33 @@ try { dislikes = 0"; WCF::getDB()->prepareStatement($sql)->execute(); - $statement = WCF::getDB()->prepareStatement('ALTER TABLE wcf'.WCF_N.'_like ADD FOREIGN KEY (reactionTypeID) REFERENCES wcf'.WCF_N.'_reaction_type (reactionTypeID) ON DELETE CASCADE'); - $statement->execute(); + $dbEditor = WCF::getDB()->getEditor(); + $foreignKeys = $dbEditor->getForeignKeys('wcf'.WCF_N.'_like'); + $expectedKey = 'fe5076ee92a558ce8177e3afbfc3dafc_fk'; + $hasExpectedKey = false; + + // Find the previously added foreign key, in case the upgrade was interrupted before. + foreach ($foreignKeys as $indexName => $definition) { + if ($indexName === $expectedKey) { + $hasExpectedKey = true; + break; + } + + if ($definition['referencedTable'] === 'wcf'.WCF_N.'_reaction_type') { + if (count($definition['columns']) === 1 && $definition['columns'][0] === 'reactionTypeID') { + $dbEditor->dropForeignKey('wcf'.WCF_N.'_like', $indexName); + } + } + } + + if (!$hasExpectedKey) { + $dbEditor->addForeignKey('wcf' . WCF_N . '_like', $expectedKey, [ + 'columns' => 'reactionTypeID', + 'referencedColumns' => 'reactionTypeID', + 'referencedTable' => 'wcf'.WCF_N.'_reaction_type', + 'ON DELETE' => 'CASCADE', + ]); + } WCF::getDB()->commitTransaction(); } -- 2.20.1