From d35c502b60cd141ed55e9c4820c47bf918546790 Mon Sep 17 00:00:00 2001 From: Umherirrender Date: Tue, 19 Apr 2022 23:01:10 +0200 Subject: [PATCH] schema: Drop foreign keys from table echo_push_subscription MediaWiki does not use foreign keys in the database schema Additional changes to the schema: - Mysql and sqlite creates index for each foreign keys, keep the index to allow fast joins with this columns by explicit statement on new wikis - Explicit create statement for the UNIQUE index on column eps_token_sha256 to make the index more visible on the sql file - Rename existing index to match the prefix for existing indexes Bug: T306473 Change-Id: I4bd29a6d0f9515e1a678c2a967799b90ef22f7b9 --- includes/Hooks.php | 8 ++++-- sql/echo_push_subscription.sql | 14 +++++----- ...push_subscription-foreign-keys-indexes.sql | 10 +++++++ ...push_subscription-foreign-keys-indexes.sql | 27 +++++++++++++++++++ 4 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 sql/mysql/patch-cleanup-push_subscription-foreign-keys-indexes.sql create mode 100644 sql/sqlite/patch-cleanup-push_subscription-foreign-keys-indexes.sql diff --git a/includes/Hooks.php b/includes/Hooks.php index a62a51df6..2110bd4a1 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -238,12 +238,16 @@ class Hooks implements RecentChange_saveHook { // 1.35 $updater->addExtensionTable( 'echo_push_provider', "$dir/echo_push_provider.sql" ); + $updater->addExtensionTable( 'echo_push_subscription', "$dir/echo_push_subscription.sql" ); // 1.36 $updater->addExtensionTable( 'echo_push_topic', "$dir/echo_push_topic.sql" ); - // 1.35 - order of tables needed for declaring references - $updater->addExtensionTable( 'echo_push_subscription', "$dir/echo_push_subscription.sql" ); + // 1.39 + if ( $dbType === 'mysql' || $dbType === 'sqlite' ) { + $updater->modifyExtensionTable( 'echo_push_subscription', + "$dir/$dbType/patch-cleanup-push_subscription-foreign-keys-indexes.sql" ); + } global $wgEchoSharedTrackingCluster, $wgEchoSharedTrackingDB; // Following tables should only be created if both cluster and database are false. diff --git a/sql/echo_push_subscription.sql b/sql/echo_push_subscription.sql index 988665388..2e42f0494 100644 --- a/sql/echo_push_subscription.sql +++ b/sql/echo_push_subscription.sql @@ -7,7 +7,7 @@ CREATE TABLE /*_*/echo_push_subscription ( eps_token BLOB NOT NULL, -- SHA256 digest of the push subscription token (to be used as a uniqueness constraint, since -- the tokens themselves may be large) - eps_token_sha256 CHAR(64) NOT NULL UNIQUE, + eps_token_sha256 CHAR(64) NOT NULL, -- push provider ID, expected to reference values 'fcm' or 'apns' eps_provider TINYINT UNSIGNED NOT NULL, -- last updated timestamp @@ -15,11 +15,11 @@ CREATE TABLE /*_*/echo_push_subscription ( -- push subscription metadata (e.g APNS notification topic) eps_data BLOB, -- APNS topic ID, references a row ID (ept_id) from echo_push_topic - eps_topic TINYINT UNSIGNED, - - FOREIGN KEY (eps_provider) REFERENCES /*_*/echo_push_provider(epp_id), - FOREIGN KEY (eps_topic) REFERENCES /*_*/echo_push_topic(ept_id) + eps_topic TINYINT UNSIGNED ) /*$wgDBTableOptions*/; -CREATE INDEX /*i*/echo_push_subscription_user_id ON /*_*/echo_push_subscription (eps_user); -CREATE INDEX /*i*/echo_push_subscription_token ON /*_*/echo_push_subscription (eps_token(10)); +CREATE UNIQUE INDEX /*i*/eps_token_sha256 ON /*_*/echo_push_subscription (eps_token_sha256); +CREATE INDEX /*i*/eps_provider ON /*_*/echo_push_subscription (eps_provider); +CREATE INDEX /*i*/eps_topic ON /*_*/echo_push_subscription (eps_topic); +CREATE INDEX /*i*/eps_user ON /*_*/echo_push_subscription (eps_user); +CREATE INDEX /*i*/eps_token ON /*_*/echo_push_subscription (eps_token(10)); diff --git a/sql/mysql/patch-cleanup-push_subscription-foreign-keys-indexes.sql b/sql/mysql/patch-cleanup-push_subscription-foreign-keys-indexes.sql new file mode 100644 index 000000000..c3c277046 --- /dev/null +++ b/sql/mysql/patch-cleanup-push_subscription-foreign-keys-indexes.sql @@ -0,0 +1,10 @@ +-- Drop foreign keys from echo_push_subscription - T306473 +ALTER TABLE /*_*/echo_push_subscription DROP FOREIGN KEY IF EXISTS /*_*/echo_push_subscription_ibfk_1; +ALTER TABLE /*_*/echo_push_subscription DROP FOREIGN KEY IF EXISTS /*_*/echo_push_subscription_ibfk_2; + +-- Rename index to match table prefix +DROP INDEX IF EXISTS /*i*/echo_push_subscription_user_id ON /*_*/echo_push_subscription; +CREATE INDEX IF NOT EXISTS /*i*/eps_user ON /*_*/echo_push_subscription (eps_user); + +DROP INDEX IF EXISTS /*i*/echo_push_subscription_token ON /*_*/echo_push_subscription; +CREATE INDEX IF NOT EXISTS /*i*/eps_token ON /*_*/echo_push_subscription (eps_token(10)); diff --git a/sql/sqlite/patch-cleanup-push_subscription-foreign-keys-indexes.sql b/sql/sqlite/patch-cleanup-push_subscription-foreign-keys-indexes.sql new file mode 100644 index 000000000..6460288e6 --- /dev/null +++ b/sql/sqlite/patch-cleanup-push_subscription-foreign-keys-indexes.sql @@ -0,0 +1,27 @@ +-- Drop foreign keys from echo_push_subscription and rename index to match table prefix - T306473 + +DROP TABLE IF EXISTS /*_*/echo_push_subscription_tmp; +CREATE TABLE /*_*/echo_push_subscription_tmp ( + eps_id INT UNSIGNED NOT NULL PRIMARY KEY auto_increment, + eps_user INT UNSIGNED NOT NULL, + eps_token BLOB NOT NULL, + eps_token_sha256 CHAR(64) NOT NULL, + eps_provider TINYINT UNSIGNED NOT NULL, + eps_updated TIMESTAMP NOT NULL, + eps_data BLOB, + eps_topic TINYINT UNSIGNED +) /*$wgDBTableOptions*/; + +INSERT INTO /*_*/echo_push_subscription_tmp + SELECT eps_id, eps_user, eps_token, eps_token_sha256, eps_provider, eps_updated, eps_data, eps_topic + FROM /*_*/echo_push_subscription; + +DROP TABLE /*_*/echo_push_subscription; + +ALTER TABLE /*_*/echo_push_subscription_tmp RENAME TO /*_*/echo_push_subscription; + +CREATE UNIQUE INDEX /*i*/eps_token_sha256 ON /*_*/echo_push_subscription (eps_token_sha256); +CREATE INDEX /*i*/eps_provider ON /*_*/echo_push_subscription (eps_provider); +CREATE INDEX /*i*/eps_topic ON /*_*/echo_push_subscription (eps_topic); +CREATE INDEX /*i*/eps_user ON /*_*/echo_push_subscription (eps_user); +CREATE INDEX /*i*/eps_token ON /*_*/echo_push_subscription (eps_token(10));