Merge pull request #9073 from annando/foreign-2

New foreign keys and database clean up jobs
This commit is contained in:
Hypolite Petovan 2020-08-26 04:20:50 -04:00 committed by GitHub
commit 176165b83b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 120 additions and 112 deletions

View File

@ -1,6 +1,6 @@
-- ------------------------------------------ -- ------------------------------------------
-- Friendica 2020.09-dev (Red Hot Poker) -- Friendica 2020.09-dev (Red Hot Poker)
-- DB_UPDATE_VERSION 1364 -- DB_UPDATE_VERSION 1365
-- ------------------------------------------ -- ------------------------------------------
@ -616,6 +616,7 @@ CREATE TABLE IF NOT EXISTS `intro` (
PRIMARY KEY(`id`), PRIMARY KEY(`id`),
INDEX `contact-id` (`contact-id`), INDEX `contact-id` (`contact-id`),
INDEX `uid` (`uid`), INDEX `uid` (`uid`),
FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE,
FOREIGN KEY (`contact-id`) REFERENCES `contact` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE FOREIGN KEY (`contact-id`) REFERENCES `contact` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE
) DEFAULT COLLATE utf8mb4_general_ci COMMENT=''; ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='';
@ -901,6 +902,8 @@ CREATE TABLE IF NOT EXISTS `notify-threads` (
PRIMARY KEY(`id`), PRIMARY KEY(`id`),
INDEX `master-parent-uri-id` (`master-parent-uri-id`), INDEX `master-parent-uri-id` (`master-parent-uri-id`),
INDEX `receiver-uid` (`receiver-uid`), INDEX `receiver-uid` (`receiver-uid`),
INDEX `notify-id` (`notify-id`),
FOREIGN KEY (`notify-id`) REFERENCES `notify` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE,
FOREIGN KEY (`receiver-uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE FOREIGN KEY (`receiver-uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE
) DEFAULT COLLATE utf8mb4_general_ci COMMENT=''; ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='';
@ -921,14 +924,13 @@ CREATE TABLE IF NOT EXISTS `oembed` (
-- --
CREATE TABLE IF NOT EXISTS `openwebauth-token` ( CREATE TABLE IF NOT EXISTS `openwebauth-token` (
`id` int unsigned NOT NULL auto_increment COMMENT 'sequential ID', `id` int unsigned NOT NULL auto_increment COMMENT 'sequential ID',
`uid` mediumint unsigned NOT NULL DEFAULT 0 COMMENT 'User id', `uid` mediumint unsigned NOT NULL DEFAULT 0 COMMENT 'User id - currently unused',
`type` varchar(32) NOT NULL DEFAULT '' COMMENT 'Verify type', `type` varchar(32) NOT NULL DEFAULT '' COMMENT 'Verify type',
`token` varchar(255) NOT NULL DEFAULT '' COMMENT 'A generated token', `token` varchar(255) NOT NULL DEFAULT '' COMMENT 'A generated token',
`meta` varchar(255) NOT NULL DEFAULT '' COMMENT '', `meta` varchar(255) NOT NULL DEFAULT '' COMMENT '',
`created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT 'datetime of creation', `created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT 'datetime of creation',
PRIMARY KEY(`id`), PRIMARY KEY(`id`),
INDEX `uid` (`uid`), INDEX `uid` (`uid`)
FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE
) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Store OpenWebAuth token to verify contacts'; ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Store OpenWebAuth token to verify contacts';
-- --
@ -1276,6 +1278,7 @@ CREATE TABLE IF NOT EXISTS `thread` (
INDEX `uid_wall_received` (`uid`,`wall`,`received`), INDEX `uid_wall_received` (`uid`,`wall`,`received`),
INDEX `private_wall_origin_commented` (`private`,`wall`,`origin`,`commented`), INDEX `private_wall_origin_commented` (`private`,`wall`,`origin`,`commented`),
INDEX `uri-id` (`uri-id`), INDEX `uri-id` (`uri-id`),
FOREIGN KEY (`iid`) REFERENCES `item` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE,
FOREIGN KEY (`uri-id`) REFERENCES `item-uri` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE FOREIGN KEY (`uri-id`) REFERENCES `item-uri` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE
) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Thread related data'; ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Thread related data';

View File

@ -0,0 +1,37 @@
<?php
/**
* @copyright Copyright (C) 2020, Friendica
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Worker;
use Friendica\Database\DBA;
class CleanItemUri
{
/**
* Delete unused item-uri entries
*/
public static function execute()
{
DBA::p("DELETE FROM `item-uri` WHERE NOT `id` IN (SELECT `uri-id` FROM `item`)
AND NOT `id` IN (SELECT `parent-uri-id` FROM `item`)
AND NOT `id` IN (SELECT `thr-parent-id` FROM `item`)");
}
}

View File

@ -86,6 +86,10 @@ class Cron
Worker::add(PRIORITY_MEDIUM, 'DBClean'); Worker::add(PRIORITY_MEDIUM, 'DBClean');
Worker::add(PRIORITY_LOW, 'ExpireConversations');
Worker::add(PRIORITY_LOW, 'CleanItemUri');
// check upstream version? // check upstream version?
Worker::add(PRIORITY_LOW, 'CheckVersion'); Worker::add(PRIORITY_LOW, 'CheckVersion');

View File

@ -50,7 +50,7 @@ class DBClean {
// Get the expire days for step 8 and 9 // Get the expire days for step 8 and 9
$days = DI::config()->get('system', 'dbclean-expire-days', 0); $days = DI::config()->get('system', 'dbclean-expire-days', 0);
for ($i = 1; $i <= 10; $i++) { for ($i = 1; $i <= 9; $i++) {
// Execute the background script for a step when it isn't finished. // Execute the background script for a step when it isn't finished.
// Execute step 8 and 9 only when $days is defined. // Execute step 8 and 9 only when $days is defined.
if (!DI::config()->get('system', 'finished-dbclean-'.$i, false) && (($i < 8) || ($i > 9) || ($days > 0))) { if (!DI::config()->get('system', 'finished-dbclean-'.$i, false) && (($i < 8) || ($i > 9) || ($days > 0))) {
@ -68,14 +68,13 @@ class DBClean {
* ------------------ * ------------------
* 1: Old global item entries from item table without user copy. * 1: Old global item entries from item table without user copy.
* 2: Items without parents. * 2: Items without parents.
* 3: Orphaned data from thread table. * 3: Legacy functionality (removed)
* 4: Orphaned data from notify table. * 4: Orphaned data from notify table.
* 5: Orphaned data from notify-threads table. * 5: Legacy functionality (removed)
* 6: Legacy functionality (removed) * 6: Legacy functionality (removed)
* 7: Orphaned data from term table. * 7: Legacy functionality (removed)
* 8: Expired threads. * 8: Expired threads.
* 9: Old global item entries from expired threads. * 9: Old global item entries from expired threads.
* 10: Old conversations.
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
private static function removeOrphans($stage) { private static function removeOrphans($stage) {
@ -146,83 +145,16 @@ class DBClean {
DI::config()->set('system', 'finished-dbclean-2', true); DI::config()->set('system', 'finished-dbclean-2', true);
} }
} elseif ($stage == 3) { } elseif ($stage == 3) {
$last_id = DI::config()->get('system', 'dbclean-last-id-3', 0); // The legacy functionality had been removed
Logger::log("Deleting orphaned data from thread table. Last ID: ".$last_id);
$r = DBA::p("SELECT `iid` FROM `thread`
WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`) AND `iid` >= ?
ORDER BY `iid` LIMIT ?", $last_id, $limit);
$count = DBA::numRows($r);
if ($count > 0) {
Logger::log("found thread orphans: ".$count);
while ($orphan = DBA::fetch($r)) {
$last_id = $orphan["iid"];
DBA::delete('thread', ['iid' => $orphan["iid"]]);
}
Worker::add(PRIORITY_MEDIUM, 'DBClean', 3, $last_id);
} else {
Logger::log("No thread orphans found");
}
DBA::close($r);
Logger::log("Done deleting ".$count." orphaned data from thread table. Last ID: ".$last_id);
DI::config()->set('system', 'dbclean-last-id-3', $last_id);
if ($count < $limit) {
DI::config()->set('system', 'finished-dbclean-3', true); DI::config()->set('system', 'finished-dbclean-3', true);
}
} elseif ($stage == 4) { } elseif ($stage == 4) {
$last_id = DI::config()->get('system', 'dbclean-last-id-4', 0); DBA::p("DELETE FROM `notify` WHERE NOT `type` IN (1, 2, 16, 32, 512) AND NOT `iid` IN (SELECT `id` FROM `item`)");
Logger::log("Deleting orphaned data from notify table. Last ID: ".$last_id); Logger::notice("Deleted orphaned data from notify table.");
$r = DBA::p("SELECT `iid`, `id` FROM `notify`
WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`) AND `id` >= ?
ORDER BY `id` LIMIT ?", $last_id, $limit);
$count = DBA::numRows($r);
if ($count > 0) {
Logger::log("found notify orphans: ".$count);
while ($orphan = DBA::fetch($r)) {
$last_id = $orphan["id"];
DBA::delete('notify', ['iid' => $orphan["iid"]]);
}
Worker::add(PRIORITY_MEDIUM, 'DBClean', 4, $last_id);
} else {
Logger::log("No notify orphans found");
}
DBA::close($r);
Logger::log("Done deleting ".$count." orphaned data from notify table. Last ID: ".$last_id);
DI::config()->set('system', 'dbclean-last-id-4', $last_id);
if ($count < $limit) {
DI::config()->set('system', 'finished-dbclean-4', true); DI::config()->set('system', 'finished-dbclean-4', true);
}
} elseif ($stage == 5) { } elseif ($stage == 5) {
$last_id = DI::config()->get('system', 'dbclean-last-id-5', 0); // The legacy functionality had been removed
Logger::log("Deleting orphaned data from notify-threads table. Last ID: ".$last_id);
$r = DBA::p("SELECT `id` FROM `notify-threads`
WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `notify-threads`.`master-parent-item`) AND `id` >= ?
ORDER BY `id` LIMIT ?", $last_id, $limit);
$count = DBA::numRows($r);
if ($count > 0) {
Logger::log("found notify-threads orphans: ".$count);
while ($orphan = DBA::fetch($r)) {
$last_id = $orphan["id"];
DBA::delete('notify-threads', ['id' => $orphan["id"]]);
}
Worker::add(PRIORITY_MEDIUM, 'DBClean', 5, $last_id);
} else {
Logger::log("No notify-threads orphans found");
}
DBA::close($r);
Logger::log("Done deleting ".$count." orphaned data from notify-threads table. Last ID: ".$last_id);
DI::config()->set('system', 'dbclean-last-id-5', $last_id);
if ($count < $limit) {
DI::config()->set('system', 'finished-dbclean-5', true); DI::config()->set('system', 'finished-dbclean-5', true);
}
} elseif ($stage == 6) { } elseif ($stage == 6) {
// The legacy functionality had been removed // The legacy functionality had been removed
DI::config()->set('system', 'finished-dbclean-6', true); DI::config()->set('system', 'finished-dbclean-6', true);
@ -254,7 +186,7 @@ class DBClean {
Logger::log("found expired threads: ".$count); Logger::log("found expired threads: ".$count);
while ($thread = DBA::fetch($r)) { while ($thread = DBA::fetch($r)) {
$last_id = $thread["iid"]; $last_id = $thread["iid"];
DBA::delete('thread', ['iid' => $thread["iid"]]); DBA::delete('item', ['parent' => $thread["iid"]]);
} }
Worker::add(PRIORITY_MEDIUM, 'DBClean', 8, $last_id); Worker::add(PRIORITY_MEDIUM, 'DBClean', 8, $last_id);
} else { } else {
@ -293,29 +225,6 @@ class DBClean {
Logger::log("Done deleting ".$count." old global item entries from expired threads. Last ID: ".$last_id); Logger::log("Done deleting ".$count." old global item entries from expired threads. Last ID: ".$last_id);
DI::config()->set('system', 'dbclean-last-id-9', $last_id); DI::config()->set('system', 'dbclean-last-id-9', $last_id);
} elseif ($stage == 10) {
$last_id = DI::config()->get('system', 'dbclean-last-id-10', 0);
$days = intval(DI::config()->get('system', 'dbclean_expire_conversation', 90));
Logger::log("Deleting old conversations. Last created: ".$last_id);
$r = DBA::p("SELECT `received`, `item-uri` FROM `conversation`
WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
ORDER BY `received` LIMIT ?", $days, $limit);
$count = DBA::numRows($r);
if ($count > 0) {
Logger::log("found old conversations: ".$count);
while ($orphan = DBA::fetch($r)) {
$last_id = $orphan["received"];
DBA::delete('conversation', ['item-uri' => $orphan["item-uri"]]);
}
Worker::add(PRIORITY_MEDIUM, 'DBClean', 10, $last_id);
} else {
Logger::log("No old conversations found");
}
DBA::close($r);
Logger::log("Done deleting ".$count." conversations. Last created: ".$last_id);
DI::config()->set('system', 'dbclean-last-id-10', $last_id);
} }
} }
} }

View File

@ -0,0 +1,42 @@
<?php
/**
* @copyright Copyright (C) 2020, Friendica
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Worker;
use Friendica\Database\DBA;
use Friendica\DI;
class ExpireConversations
{
/**
* Delete old conversation entries
*/
public static function execute()
{
$days = intval(DI::config()->get('system', 'dbclean_expire_conversation', 90));
if (empty($days)) {
return;
}
DBA::p("DELETE FROM `conversation` WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY", $days);
}
}

View File

@ -54,7 +54,7 @@
use Friendica\Database\DBA; use Friendica\Database\DBA;
if (!defined('DB_UPDATE_VERSION')) { if (!defined('DB_UPDATE_VERSION')) {
define('DB_UPDATE_VERSION', 1364); define('DB_UPDATE_VERSION', 1365);
} }
return [ return [
@ -691,7 +691,7 @@ return [
"item" => [ "item" => [
"comment" => "Structure for all posts", "comment" => "Structure for all posts",
"fields" => [ "fields" => [
"id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "relation" => ["thread" => "iid"]], "id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1"],
"guid" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => "A unique identifier for this item"], "guid" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => "A unique identifier for this item"],
"uri" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""], "uri" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""],
"uri-id" => ["type" => "int unsigned", "foreign" => ["item-uri" => "id"], "comment" => "Id of the item-uri table entry that contains the item uri"], "uri-id" => ["type" => "int unsigned", "foreign" => ["item-uri" => "id"], "comment" => "Id of the item-uri table entry that contains the item uri"],
@ -962,7 +962,7 @@ return [
"comment" => "", "comment" => "",
"fields" => [ "fields" => [
"id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "sequential ID"], "id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "sequential ID"],
"notify-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "relation" => ["notify" => "id"], "comment" => ""], "notify-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["notify" => "id"], "comment" => ""],
"master-parent-item" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "relation" => ["item" => "id"], "comment" => ""], "master-parent-item" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "relation" => ["item" => "id"], "comment" => ""],
"master-parent-uri-id" => ["type" => "int unsigned", "relation" => ["item-uri" => "id"], "comment" => "Item-uri id of the parent of the related post"], "master-parent-uri-id" => ["type" => "int unsigned", "relation" => ["item-uri" => "id"], "comment" => "Item-uri id of the parent of the related post"],
"parent-item" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "comment" => ""], "parent-item" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "comment" => ""],
@ -973,6 +973,7 @@ return [
"PRIMARY" => ["id"], "PRIMARY" => ["id"],
"master-parent-uri-id" => ["master-parent-uri-id"], "master-parent-uri-id" => ["master-parent-uri-id"],
"receiver-uid" => ["receiver-uid"], "receiver-uid" => ["receiver-uid"],
"notify-id" => ["notify-id"],
] ]
], ],
"oembed" => [ "oembed" => [
@ -992,7 +993,7 @@ return [
"comment" => "Store OpenWebAuth token to verify contacts", "comment" => "Store OpenWebAuth token to verify contacts",
"fields" => [ "fields" => [
"id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "sequential ID"], "id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "sequential ID"],
"uid" => ["type" => "mediumint unsigned", "not null" => "1", "default" => "0", "foreign" => ["user" => "uid"], "comment" => "User id"], "uid" => ["type" => "mediumint unsigned", "not null" => "1", "default" => "0", "relation" => ["user" => "uid"], "comment" => "User id - currently unused"],
"type" => ["type" => "varchar(32)", "not null" => "1", "default" => "", "comment" => "Verify type"], "type" => ["type" => "varchar(32)", "not null" => "1", "default" => "", "comment" => "Verify type"],
"token" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => "A generated token"], "token" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => "A generated token"],
"meta" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""], "meta" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""],
@ -1305,7 +1306,7 @@ return [
"thread" => [ "thread" => [
"comment" => "Thread related data", "comment" => "Thread related data",
"fields" => [ "fields" => [
"iid" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "primary" => "1", "relation" => ["item" => "id"], "iid" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "primary" => "1", "foreign" => ["item" => "id"],
"comment" => "sequential ID"], "comment" => "sequential ID"],
"uri-id" => ["type" => "int unsigned", "foreign" => ["item-uri" => "id"], "comment" => "Id of the item-uri table entry that contains the item uri"], "uri-id" => ["type" => "int unsigned", "foreign" => ["item-uri" => "id"], "comment" => "Id of the item-uri table entry that contains the item uri"],
"uid" => ["type" => "mediumint unsigned", "not null" => "1", "default" => "0", "relation" => ["user" => "uid"], "comment" => "User id"], "uid" => ["type" => "mediumint unsigned", "not null" => "1", "default" => "0", "relation" => ["user" => "uid"], "comment" => "User id"],

View File

@ -736,3 +736,15 @@ function pre_update_1364()
return Update::SUCCESS; return Update::SUCCESS;
} }
function pre_update_1365()
{
if (!DBA::e("DELETE FROM `notify-threads` WHERE NOT `notify-id` IN (SELECT `id` FROM `notify`)")) {
return Update::FAILED;
}
if (!DBA::e("DELETE FROM `thread` WHERE NOT `iid` IN (SELECT `id` FROM `item`)")) {
return Update::FAILED;
}
}