From: Michael Date: Sat, 16 May 2020 06:12:28 +0000 (+0000) Subject: Issue 8636: Check and fix data before foreign key creation X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=14f814e6bad12d4fe58302c3e7390ac44fae7aef;p=friendica.git Issue 8636: Check and fix data before foreign key creation --- 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); + } } }