From 14f814e6bad12d4fe58302c3e7390ac44fae7aef Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 16 May 2020 06:12:28 +0000 Subject: [PATCH 1/2] Issue 8636: Check and fix data before foreign key creation --- src/Database/DBStructure.php | 60 ++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/src/Database/DBStructure.php b/src/Database/DBStructure.php index 566aea2348..e4af148cdf 100644 --- a/src/Database/DBStructure.php +++ b/src/Database/DBStructure.php @@ -25,6 +25,8 @@ use Exception; use Friendica\Core\Hook; use Friendica\Core\Logger; use Friendica\DI; +use Friendica\Model\Item; +use Friendica\Model\User; use Friendica\Util\DateTimeFormat; /** @@ -944,6 +946,19 @@ class DBStructure return true; } + /** + * Check if a foreign key exists for the given table field + * + * @param string $table + * @param string $field + * @return boolean + */ + public static function existsForeignKeyForField(string $table, string $field) + { + return DBA::exists(['INFORMATION_SCHEMA' => 'KEY_COLUMN_USAGE'], + ["`TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `COLUMN_NAME` = ? AND `REFERENCED_TABLE_SCHEMA` IS NOT NULL", + DBA::databaseName(), $table, $field]); + } /** * Check if a table exists * @@ -996,11 +1011,34 @@ class DBStructure } } - if (self::existsTable('permissionset') && !DBA::exists('permissionset', ['id' => 0])) { - DBA::insert('permissionset', ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']); - $lastid = DBA::lastInsertId(); - if ($lastid != 0) { - DBA::update('permissionset', ['id' => 0], ['id' => $lastid]); + if (self::existsTable('permissionset')) { + if (!DBA::exists('permissionset', ['id' => 0])) { + DBA::insert('permissionset', ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']); + $lastid = DBA::lastInsertId(); + if ($lastid != 0) { + DBA::update('permissionset', ['id' => 0], ['id' => $lastid]); + } + } + if (!self::existsForeignKeyForField('item', 'psid')) { + $sets = DBA::p("SELECT `psid`, `item`.`uid`, `item`.`private` FROM `item` + LEFT JOIN `permissionset` ON `permissionset`.`id` = `item`.`psid` + WHERE `permissionset`.`id` IS NULL AND NOT `psid` IS NULL"); + while ($set = DBA::fetch($sets)) { + if (($set['private'] == Item::PRIVATE) && ($set['uid'] != 0)) { + $owner = User::getOwnerDataById($set['uid']); + if ($owner) { + $permission = '<' . $owner['id'] . '>'; + } else { + $permission = '<>'; + } + } else { + $permission = ''; + } + $fields = ['id' => $set['psid'], 'uid' => $set['uid'], 'allow_cid' => $permission, + 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']; + DBA::insert('permissionset', $fields); + } + DBA::close($sets); } } @@ -1010,6 +1048,16 @@ class DBStructure if ($lastid != 0) { DBA::update('tag', ['id' => 0], ['id' => $lastid]); } - } + } + + if (!self::existsForeignKeyForField('tokens', 'client_id')) { + $tokens = DBA::p("SELECT `tokens`.`id` FROM `tokens` + LEFT JOIN `clients` ON `clients`.`client_id` = `tokens`.`client_id` + WHERE `clients`.`client_id` IS NULL"); + while ($token = DBA::fetch($tokens)) { + DBA::delete('token', ['id' => $token['id']]); + } + DBA::close($tokens); + } } } From 28e52f4db1362619082bde52f6a0ef684c396fc1 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 16 May 2020 06:14:25 +0000 Subject: [PATCH 2/2] Fixed table name --- src/Database/DBStructure.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/DBStructure.php b/src/Database/DBStructure.php index e4af148cdf..0ff97d0c6c 100644 --- a/src/Database/DBStructure.php +++ b/src/Database/DBStructure.php @@ -1055,7 +1055,7 @@ class DBStructure LEFT JOIN `clients` ON `clients`.`client_id` = `tokens`.`client_id` WHERE `clients`.`client_id` IS NULL"); while ($token = DBA::fetch($tokens)) { - DBA::delete('token', ['id' => $token['id']]); + DBA::delete('tokens', ['id' => $token['id']]); } DBA::close($tokens); }