Merge pull request #9917 from annando/no-thread

The "thread" table isn't used anymore
This commit is contained in:
Hypolite Petovan 2021-02-08 08:05:21 -05:00 committed by GitHub
commit f274c93330
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 34 additions and 223 deletions

View file

@ -1380,57 +1380,6 @@ CREATE TABLE IF NOT EXISTS `storage` (
PRIMARY KEY(`id`)
) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Data stored by Database storage backend';
--
-- TABLE thread
--
CREATE TABLE IF NOT EXISTS `thread` (
`iid` int unsigned NOT NULL DEFAULT 0 COMMENT 'sequential ID',
`uri-id` int unsigned COMMENT 'Id of the item-uri table entry that contains the item uri',
`uid` mediumint unsigned NOT NULL DEFAULT 0 COMMENT 'User id',
`contact-id` int unsigned NOT NULL DEFAULT 0 COMMENT '',
`owner-id` int unsigned NOT NULL DEFAULT 0 COMMENT 'Item owner',
`author-id` int unsigned NOT NULL DEFAULT 0 COMMENT 'Item author',
`created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '',
`edited` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '',
`commented` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '',
`received` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '',
`changed` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '',
`wall` boolean NOT NULL DEFAULT '0' COMMENT '',
`private` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '0=public, 1=private, 2=unlisted',
`pubmail` boolean NOT NULL DEFAULT '0' COMMENT '',
`moderated` boolean NOT NULL DEFAULT '0' COMMENT '',
`visible` boolean NOT NULL DEFAULT '0' COMMENT '',
`starred` boolean NOT NULL DEFAULT '0' COMMENT '',
`ignored` boolean NOT NULL DEFAULT '0' COMMENT '',
`post-type` tinyint unsigned NOT NULL DEFAULT 0 COMMENT 'Post type (personal note, bookmark, ...)',
`unseen` boolean NOT NULL DEFAULT '1' COMMENT '',
`deleted` boolean NOT NULL DEFAULT '0' COMMENT '',
`origin` boolean NOT NULL DEFAULT '0' COMMENT '',
`forum_mode` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '',
`mention` boolean NOT NULL DEFAULT '0' COMMENT '',
`network` char(4) NOT NULL DEFAULT '' COMMENT '',
`bookmark` boolean COMMENT '',
PRIMARY KEY(`iid`),
INDEX `uid_network_commented` (`uid`,`network`,`commented`),
INDEX `uid_network_received` (`uid`,`network`,`received`),
INDEX `uid_contactid_commented` (`uid`,`contact-id`,`commented`),
INDEX `uid_contactid_received` (`uid`,`contact-id`,`received`),
INDEX `contactid` (`contact-id`),
INDEX `ownerid` (`owner-id`),
INDEX `authorid` (`author-id`),
INDEX `uid_received` (`uid`,`received`),
INDEX `uid_commented` (`uid`,`commented`),
INDEX `uid_wall_received` (`uid`,`wall`,`received`),
INDEX `private_wall_origin_commented` (`private`,`wall`,`origin`,`commented`),
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 (`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 (`owner-id`) REFERENCES `contact` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT,
FOREIGN KEY (`author-id`) REFERENCES `contact` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT
) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Thread related data';
--
-- TABLE tokens
--

View file

@ -478,7 +478,7 @@ class DBA
* @return boolean|object
*
* Example:
* $table = "item";
* $table = "post";
* $fields = array("id", "uri", "uid", "network");
*
* $condition = array("uid" => 1, "network" => 'dspr');
@ -505,7 +505,7 @@ class DBA
* @return int
*
* Example:
* $table = "item";
* $table = "post";
*
* $condition = ["uid" => 1, "network" => 'dspr'];
* or:

View file

@ -80,7 +80,7 @@ class DBStructure
$old_tables = ['fserver', 'gcign', 'gcontact', 'gcontact-relation', 'gfollower' ,'glink', 'item-delivery-data',
'item-activity', 'item-content', 'item_id', 'participation', 'poll', 'poll_result', 'queue', 'retriever_rule',
'sign', 'spam', 'term', 'user-item'];
'sign', 'spam', 'term', 'thread', 'user-item'];
$tables = DBA::selectToArray(['INFORMATION_SCHEMA' => 'TABLES'], ['TABLE_NAME'],
['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_TYPE' => 'BASE TABLE']);

View file

@ -1512,7 +1512,7 @@ class Database
*
*
* Example:
* $table = 'item';
* $table = 'post';
* or:
* $table = ['schema' => 'table'];
* @see DBA::buildTableString()
@ -1575,7 +1575,7 @@ class Database
* @return int
*
* Example:
* $table = "item";
* $table = "post";
*
* $condition = ["uid" => 1, "network" => 'dspr'];
* or:

View file

@ -319,10 +319,9 @@ class Item
// Set the item to "deleted"
$item_fields = ['deleted' => true, 'edited' => DateTimeFormat::utcNow(), 'changed' => DateTimeFormat::utcNow()];
DBA::update('item', $item_fields, ['id' => $item['id']]);
Post::update($item_fields, ['id' => $item['id']]);
Post\Category::storeTextByURIId($item['uri-id'], $item['uid'], '');
self::deleteThread($item['id'], $item['parent-uri-id']);
if (!Post::exists(["`uri-id` = ? AND `uid` != 0 AND NOT `deleted`", $item['uri-id']])) {
self::markForDeletion(['uri-id' => $item['uri-id'], 'uid' => 0, 'deleted' => false], $priority);
@ -900,7 +899,6 @@ class Item
// If its a post that originated here then tag the thread as "mention"
if ($item['origin'] && $item['uid']) {
DBA::update('post-thread-user', ['mention' => true], ['uri-id' => $item['parent-uri-id'], 'uid' => $item['uid']]);
DBA::update('thread', ['mention' => true], ['iid' => $parent_id]);
Logger::info('tagged thread as mention', ['parent' => $parent_id, 'parent-uri-id' => $item['parent-uri-id'], 'uid' => $item['uid']]);
}
@ -919,7 +917,7 @@ class Item
$item["global"] = true;
// Set the global flag on all items if this was a global item entry
DBA::update('item', ['global' => true], ['uri-id' => $item['uri-id']]);
Post::update(['global' => true], ['uri-id' => $item['uri-id']]);
} else {
$item['global'] = Post::exists(['uid' => 0, 'uri-id' => $item['uri-id']]);
}
@ -1068,15 +1066,9 @@ class Item
}
if ($update_commented) {
DBA::update('item', ['commented' => DateTimeFormat::utcNow(), 'changed' => DateTimeFormat::utcNow()], ['id' => $parent_id]);
Post::update(['commented' => DateTimeFormat::utcNow(), 'changed' => DateTimeFormat::utcNow()], ['id' => $parent_id]);
} else {
DBA::update('item', ['changed' => DateTimeFormat::utcNow()], ['id' => $parent_id]);
}
if ($item['gravity'] === GRAVITY_PARENT) {
self::addThread($current_post);
} else {
self::updateThread($parent_id);
Post::update(['changed' => DateTimeFormat::utcNow()], ['id' => $parent_id]);
}
// In that function we check if this is a forum post. Additionally we delete the item under certain circumstances
@ -2423,75 +2415,6 @@ class Item
return true;
}
private static function addThread($itemid, $onlyshadow = false)
{
$fields = ['uid', 'created', 'edited', 'commented', 'received', 'changed', 'wall', 'private', 'pubmail',
'moderated', 'visible', 'starred', 'contact-id', 'post-type', 'uri-id',
'deleted', 'origin', 'forum_mode', 'mention', 'network', 'author-id', 'owner-id'];
$condition = ["`id` = ? AND (`parent` = ? OR `parent` = 0)", $itemid, $itemid];
$item = Post::selectFirst($fields, $condition);
if (!DBA::isResult($item)) {
return;
}
$item['iid'] = $itemid;
if (!$onlyshadow) {
$result = DBA::replace('thread', $item);
Logger::info('Add thread', ['item' => $itemid, 'result' => $result]);
}
}
private static function updateThread($itemid, $setmention = false)
{
$fields = ['uid', 'guid', 'created', 'edited', 'commented', 'received', 'changed', 'post-type',
'wall', 'private', 'pubmail', 'moderated', 'visible', 'starred', 'contact-id', 'uri-id',
'deleted', 'origin', 'forum_mode', 'network', 'author-id', 'owner-id'];
$item = Post::selectFirst($fields, ['id' => $itemid, 'gravity' => GRAVITY_PARENT]);
if (!DBA::isResult($item)) {
return;
}
if ($setmention) {
$item["mention"] = 1;
}
$fields = [];
foreach ($item as $field => $data) {
if (!in_array($field, ["guid"])) {
$fields[$field] = $data;
}
}
$result = DBA::update('thread', $fields, ['iid' => $itemid]);
Logger::info('Update thread', ['item' => $itemid, 'guid' => $item["guid"], 'result' => $result]);
}
private static function deleteThread($itemid, $uri_id)
{
$item = DBA::selectFirst('thread', ['uid'], ['iid' => $itemid]);
if (!DBA::isResult($item)) {
Logger::info('No thread found', ['id' => $itemid]);
return;
}
$result = DBA::delete('thread', ['iid' => $itemid], ['cascade' => false]);
Logger::info('Deleted thread', ['item' => $itemid, 'result' => $result]);
$condition = ["`uri-id` = ? AND NOT `deleted` AND NOT (`uid` IN (?, 0))", $uri_id, $item["uid"]];
if (!Post::exists($condition)) {
DBA::delete('item', ['uri-id' => $uri_id, 'uid' => 0]);
Post\User::delete(['uri-id' => $uri_id, 'uid' => 0]);
Logger::debug('Deleted shadow item', ['id' => $itemid, 'uri-id' => $uri_id]);
}
}
/**
* Fetch the SQL condition for the given user id
*

View file

@ -477,18 +477,6 @@ class Post
$affected = max($affected, DBA::affectedRows());
}
$update_fields = DBStructure::getFieldsForTable('thread', $fields);
if (!empty($update_fields)) {
$rows = DBA::selectToArray('post-view', ['id'], $thread_condition);
$ids = array_column($rows, 'id');
if (!DBA::update('thread', $update_fields, ['iid' => $ids])) {
DBA::rollback();
Logger::notice('Updating thread failed', ['fields' => $update_fields, 'condition' => $thread_condition]);
return false;
}
$affected = max($affected, DBA::affectedRows());
}
$update_fields = [];
foreach (Item::USED_FIELDLIST as $field) {
if (array_key_exists($field, $fields)) {

View file

@ -104,7 +104,7 @@ class Site extends BaseAdmin
// update profile links in the format "http://server.tld"
update_table($a, "profile", ['photo', 'thumb'], $old_url, $new_url);
update_table($a, "contact", ['photo', 'thumb', 'micro', 'url', 'nurl', 'alias', 'request', 'notify', 'poll', 'confirm', 'poco', 'avatar'], $old_url, $new_url);
update_table($a, "item", ['owner-link', 'author-link', 'body', 'plink', 'tag'], $old_url, $new_url);
update_table($a, "post-content", ['body'], $old_url, $new_url);
// update profile addresses in the format "user@server.tld"
update_table($a, "contact", ['addr'], $old_host, $new_host);

View file

@ -242,7 +242,7 @@ class UserExport extends BaseSettings
self::exportAccount($a);
echo "\n";
$total = DBA::count('item', ['uid' => local_user()]);
$total = Post::count(['uid' => local_user()]);
// chunk the output to avoid exhausting memory
for ($x = 0; $x < $total; $x += 500) {

View file

@ -23,6 +23,7 @@ namespace Friendica\Worker;
use Friendica\Core\Logger;
use Friendica\Database\DBA;
use Friendica\Model\Post;
class CleanItemUri
{
@ -33,7 +34,7 @@ class CleanItemUri
{
// We have to avoid deleting newly created "item-uri" entries.
// So we fetch a post that had been stored yesterday and only delete older ones.
$item = DBA::selectFirst('item', ['uri-id'], ["`uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY", 0, 1],
$item = Post::selectFirst(['uri-id'], ["`uid` = ? AND `received` < UTC_TIMESTAMP() - INTERVAL ? DAY", 0, 1],
['order' => ['received' => true]]);
if (empty($item['uri-id'])) {
Logger::warning('No item with uri-id found - we better quit here');

View file

@ -44,11 +44,12 @@ class Expire
Logger::log('Delete expired items', Logger::DEBUG);
// physically remove anything that has been deleted for more than two months
$condition = ["`deleted` AND `changed` < UTC_TIMESTAMP() - INTERVAL 60 DAY"];
$rows = DBA::select('item', ['id', 'guid', 'uri-id', 'uid'], $condition);
while ($row = DBA::fetch($rows)) {
$rows = Post::select(['id', 'guid', 'uri-id', 'uid'], $condition);
while ($row = Post::fetch($rows)) {
Logger::info('Delete expired item', ['id' => $row['id'], 'guid' => $row['guid']]);
DBA::delete('item', ['id' => $row['id']]);
Post\User::delete(['uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
Post\ThreadUser::delete(['uri-id' => $row['uri-id'], 'uid' => $row['uid']]);
}
DBA::close($rows);

View file

@ -25,6 +25,7 @@ use Friendica\Core\Logger;
use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Item;
class ExpirePosts
{
@ -47,21 +48,18 @@ class ExpirePosts
if (!empty($expire_days)) {
do {
Logger::notice('Start deleting expired threads', ['expiry_days' => $expire_days]);
/// @todo replace that query later
$ret = DBA::e("DELETE FROM `item-uri` WHERE `id` IN
(SELECT `uri-id` FROM `thread`
INNER JOIN `contact` ON `id` = `contact-id` AND NOT `notify_new_posts`
WHERE `thread`.`received` < UTC_TIMESTAMP() - INTERVAL ? DAY
AND NOT `thread`.`mention` AND NOT `thread`.`starred`
AND NOT `thread`.`wall` AND NOT `thread`.`origin`
AND `thread`.`uid` != 0 AND NOT `iid` IN (SELECT `parent` FROM `item`
WHERE (`item`.`starred` OR (`item`.`resource-id` != '')
OR (`item`.`event-id` != '') OR (`item`.`attach` != '')
OR `item`.`wall` OR `item`.`origin`
OR `item`.`uri-id` IN (SELECT `uri-id` FROM `post-category`
WHERE `post-category`.`uri-id` = `item`.`uri-id`))
AND `item`.`parent` = `thread`.`iid`))
ORDER BY `id` LIMIT ?", $expire_days, $limit);
(SELECT `uri-id` FROM `post-thread` WHERE `received` < UTC_TIMESTAMP() - INTERVAL ? DAY
AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-thread-user` WHERE (`mention` OR `starred` OR `wall` OR `pinned`) AND `uri-id` = `post-thread`.`uri-id`)
AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-user` WHERE `origin` AND `uri-id` = `post-thread`.`uri-id`)
AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-category` WHERE `uri-id` = `post-thread`.`uri-id`)
AND NOT `uri-id` IN (SELECT `uri-id` FROM `post-media` WHERE `uri-id` = `post-thread`.`uri-id`)
AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `item` INNER JOIN `contact` ON `contact`.`id` = `contact-id` AND `notify_new_posts`
WHERE `parent-uri-id` = `post-thread`.`uri-id`)
AND NOT `uri-id` IN (SELECT `parent-uri-id` FROM `item`
WHERE (`origin` OR `starred` OR `resource-id` != 0 OR `event-id` != 0 OR `wall` OR `attach` != '' OR `post-type` = ?)
AND `parent-uri-id` = `post-thread`.`uri-id`))
ORDER BY `id` LIMIT ?", $expire_days, Item::PT_PERSONAL_NOTE, $limit);
$rows = DBA::affectedRows();
Logger::notice('Deleted expired threads', ['result' => $ret, 'rows' => $rows]);

View file

@ -23,6 +23,7 @@ namespace Friendica\Worker;
use Friendica\Core\Logger;
use Friendica\Database\DBA;
use Friendica\Model\Post;
class MergeContact
{
@ -43,8 +44,7 @@ class MergeContact
Logger::info('Handling duplicate', ['search' => $old_cid, 'replace' => $new_cid]);
// Search and replace
DBA::update('item', ['contact-id' => $new_cid], ['contact-id' => $old_cid]);
DBA::update('thread', ['contact-id' => $new_cid], ['contact-id' => $old_cid]);
Post::update(['contact-id' => $new_cid], ['contact-id' => $old_cid]);
DBA::update('mail', ['contact-id' => $new_cid], ['contact-id' => $old_cid]);
DBA::update('photo', ['contact-id' => $new_cid], ['contact-id' => $old_cid]);
DBA::update('event', ['cid' => $new_cid], ['cid' => $old_cid]);
@ -53,11 +53,9 @@ class MergeContact
if ($uid == 0) {
DBA::update('post-tag', ['cid' => $new_cid], ['cid' => $old_cid]);
DBA::delete('post-tag', ['cid' => $old_cid]);
DBA::update('item', ['author-id' => $new_cid], ['author-id' => $old_cid]);
DBA::update('item', ['owner-id' => $new_cid], ['owner-id' => $old_cid]);
DBA::update('item', ['causer-id' => $new_cid], ['causer-id' => $old_cid]);
DBA::update('thread', ['author-id' => $new_cid], ['author-id' => $old_cid]);
DBA::update('thread', ['owner-id' => $new_cid], ['owner-id' => $old_cid]);
Post::update(['author-id' => $new_cid], ['author-id' => $old_cid]);
Post::update(['owner-id' => $new_cid], ['owner-id' => $old_cid]);
Post::update(['causer-id' => $new_cid], ['causer-id' => $old_cid]);
} else {
/// @todo Check if some other data needs to be adjusted as well, possibly the "rel" status?
}

View file

@ -26,7 +26,7 @@ use Friendica\Model\ItemURI;
/**
* Do some repairs in database entries
*
* @todo This class can be deleted without replacement when the item table is removed
*/
class RepairDatabase
{

View file

@ -1429,53 +1429,6 @@ return [
"PRIMARY" => ["id"]
]
],
"thread" => [
"comment" => "Thread related data",
"fields" => [
"iid" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "primary" => "1", "foreign" => ["item" => "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"],
"uid" => ["type" => "mediumint unsigned", "not null" => "1", "default" => "0", "foreign" => ["user" => "uid"], "comment" => "User id"],
"contact-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id"], "comment" => ""],
"owner-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Item owner"],
"author-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id", "on delete" => "restrict"], "comment" => "Item author"],
"created" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""],
"edited" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""],
"commented" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""],
"received" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""],
"changed" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""],
"wall" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""],
"private" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "comment" => "0=public, 1=private, 2=unlisted"],
"pubmail" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""],
"moderated" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""],
"visible" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""],
"starred" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""],
"ignored" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""],
"post-type" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "comment" => "Post type (personal note, bookmark, ...)"],
"unseen" => ["type" => "boolean", "not null" => "1", "default" => "1", "comment" => ""],
"deleted" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""],
"origin" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""],
"forum_mode" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "comment" => ""],
"mention" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""],
"network" => ["type" => "char(4)", "not null" => "1", "default" => "", "comment" => ""],
"bookmark" => ["type" => "boolean", "comment" => ""],
],
"indexes" => [
"PRIMARY" => ["iid"],
"uid_network_commented" => ["uid", "network", "commented"],
"uid_network_received" => ["uid", "network", "received"],
"uid_contactid_commented" => ["uid", "contact-id", "commented"],
"uid_contactid_received" => ["uid", "contact-id", "received"],
"contactid" => ["contact-id"],
"ownerid" => ["owner-id"],
"authorid" => ["author-id"],
"uid_received" => ["uid", "received"],
"uid_commented" => ["uid", "commented"],
"uid_wall_received" => ["uid", "wall", "received"],
"private_wall_origin_commented" => ["private", "wall", "origin", "commented"],
"uri-id" => ["uri-id"],
]
],
"tokens" => [
"comment" => "OAuth usage",
"fields" => [