X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FDatabase%2FDBStructure.php;h=6291d0ffc4b643a2f32bfbad6f2edb2ca10ff92a;hb=c4870a16d4ffbd37d6c4f379abfe2d18cacb88ff;hp=b4f8ed13291564a24e3ea069ba13f766652c7ddd;hpb=a910fd8864f5f1e9c35e9065abad24353793f313;p=friendica.git diff --git a/src/Database/DBStructure.php b/src/Database/DBStructure.php index b4f8ed1329..6291d0ffc4 100644 --- a/src/Database/DBStructure.php +++ b/src/Database/DBStructure.php @@ -1,6 +1,6 @@ set('system', 'build', $version); + DI::keyValue()->set('build', $version); echo DI::l10n()->t('The database version had been set to %s.', $version); } + /** + * Drops a specific table + * + * @param string $table the table name + * + * @return bool true if possible, otherwise false + */ + public static function dropTable(string $table): bool + { + return DBA::isResult(DBA::e('DROP TABLE ' . DBA::quoteIdentifier($table) . ';')); + } + /** * Drop unused tables * @@ -65,7 +77,7 @@ class DBStructure */ public static function dropTables(bool $execute) { - $postupdate = DI::config()->get('system', 'post_update_version', PostUpdate::VERSION); + $postupdate = DI::keyValue()->get('post_update_version') ?? PostUpdate::VERSION; if ($postupdate < PostUpdate::VERSION) { echo DI::l10n()->t('The post update is at version %d, it has to be at %d to safely drop the tables.', $postupdate, PostUpdate::VERSION); return; @@ -74,7 +86,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', 'deliverq', 'dsprphotoq', 'ffinder', 'sign', 'spam', 'term', 'user-item', 'thread', 'item', 'challenge', - 'auth_codes', 'tokens', 'clients', 'profile_check', 'host']; + 'auth_codes', 'tokens', 'clients', 'profile_check', 'host', 'conversation', 'fcontact', 'addon']; $tables = DBA::selectToArray('INFORMATION_SCHEMA.TABLES', ['TABLE_NAME'], ['TABLE_SCHEMA' => DBA::databaseName(), 'TABLE_TYPE' => 'BASE TABLE']); @@ -94,8 +106,7 @@ class DBStructure $sql = 'DROP TABLE ' . DBA::quoteIdentifier($table) . ';'; echo $sql . "\n"; - $result = DBA::e($sql); - if (!DBA::isResult($result)) { + if (!static::dropTable($table)) { self::printUpdateError($sql); } } else { @@ -142,6 +153,7 @@ class DBStructure * Print out database error messages * * @param string $message Message to be added to the error message + * * @return string Error message */ private static function printUpdateError(string $message): string @@ -149,12 +161,13 @@ class DBStructure echo DI::l10n()->t("\nError %d occurred during database update:\n%s\n", DBA::errorNo(), DBA::errorMessage()); - return DI::l10n()->t('Errors encountered performing database changes: ') . $message . EOL; + return DI::l10n()->t('Errors encountered performing database changes: ') . $message . '
'; } /** * Perform a database structure dryrun (means: just simulating) * + * @return string Empty string if the update is successful, error messages otherwise * @throws Exception */ public static function dryRun(): string @@ -174,14 +187,16 @@ class DBStructure public static function performUpdate(bool $enable_maintenance_mode = true, bool $verbose = false): string { if ($enable_maintenance_mode) { - DI::config()->set('system', 'maintenance', 1); + DI::config()->set('system', 'maintenance', true); } $status = self::update($verbose, true); if ($enable_maintenance_mode) { - DI::config()->set('system', 'maintenance', 0); - DI::config()->set('system', 'maintenance_reason', ''); + DI::config()->beginTransaction() + ->set('system', 'maintenance', false) + ->delete('system', 'maintenance_reason') + ->commit(); } return $status; @@ -247,7 +262,8 @@ class DBStructure // Get the definition if (is_null($definition)) { - $definition = DI::dbaDefinition()->getAll(); + // just for Update purpose, reload the DBA definition with addons to explicit get the whole definition + $definition = DI::dbaDefinition()->load(true)->getAll(); } // MySQL >= 5.7.4 doesn't support the IGNORE keyword in ALTER TABLE statements @@ -513,28 +529,28 @@ class DBStructure $fields = DBA::selectToArray('INFORMATION_SCHEMA.COLUMNS', ['COLUMN_NAME', 'COLUMN_TYPE', 'IS_NULLABLE', 'COLUMN_DEFAULT', 'EXTRA', - 'COLUMN_KEY', 'COLLATION_NAME', 'COLUMN_COMMENT'], + 'COLUMN_KEY', 'COLLATION_NAME', 'COLUMN_COMMENT'], ["`TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?", - DBA::databaseName(), $table]); + DBA::databaseName(), $table]); $foreign_keys = DBA::selectToArray('INFORMATION_SCHEMA.KEY_COLUMN_USAGE', ['COLUMN_NAME', 'CONSTRAINT_NAME', 'REFERENCED_TABLE_NAME', 'REFERENCED_COLUMN_NAME'], ["`TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `REFERENCED_TABLE_SCHEMA` IS NOT NULL", - DBA::databaseName(), $table]); + DBA::databaseName(), $table]); $table_status = DBA::selectFirst('INFORMATION_SCHEMA.TABLES', ['ENGINE', 'TABLE_COLLATION', 'TABLE_COMMENT'], ["`TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?", - DBA::databaseName(), $table]); + DBA::databaseName(), $table]); - $fielddata = []; - $indexdata = []; + $fielddata = []; + $indexdata = []; $foreigndata = []; if (DBA::isResult($foreign_keys)) { foreach ($foreign_keys as $foreign_key) { - $parameters = ['foreign' => [$foreign_key['REFERENCED_TABLE_NAME'] => $foreign_key['REFERENCED_COLUMN_NAME']]]; - $constraint = self::getConstraintName($table, $foreign_key['COLUMN_NAME'], $parameters); + $parameters = ['foreign' => [$foreign_key['REFERENCED_TABLE_NAME'] => $foreign_key['REFERENCED_COLUMN_NAME']]]; + $constraint = self::getConstraintName($table, $foreign_key['COLUMN_NAME'], $parameters); $foreigndata[$constraint] = $foreign_key; } } @@ -562,8 +578,8 @@ class DBStructure $fielddata = []; if (DBA::isResult($fields)) { foreach ($fields as $field) { - $search = ['tinyint(1)', 'tinyint(3) unsigned', 'tinyint(4)', 'smallint(5) unsigned', 'smallint(6)', 'mediumint(8) unsigned', 'mediumint(9)', 'bigint(20)', 'int(10) unsigned', 'int(11)']; - $replace = ['boolean', 'tinyint unsigned', 'tinyint', 'smallint unsigned', 'smallint', 'mediumint unsigned', 'mediumint', 'bigint', 'int unsigned', 'int']; + $search = ['tinyint(1)', 'tinyint(3) unsigned', 'tinyint(4)', 'smallint(5) unsigned', 'smallint(6)', 'mediumint(8) unsigned', 'mediumint(9)', 'bigint(20)', 'int(10) unsigned', 'int(11)']; + $replace = ['boolean', 'tinyint unsigned', 'tinyint', 'smallint unsigned', 'smallint', 'mediumint unsigned', 'mediumint', 'bigint', 'int unsigned', 'int']; $field['COLUMN_TYPE'] = str_replace($search, $replace, $field['COLUMN_TYPE']); $fielddata[$field['COLUMN_NAME']]['type'] = $field['COLUMN_TYPE']; @@ -585,13 +601,13 @@ class DBStructure } $fielddata[$field['COLUMN_NAME']]['Collation'] = $field['COLLATION_NAME']; - $fielddata[$field['COLUMN_NAME']]['comment'] = $field['COLUMN_COMMENT']; + $fielddata[$field['COLUMN_NAME']]['comment'] = $field['COLUMN_COMMENT']; } } return [ - 'fields' => $fielddata, - 'indexes' => $indexdata, + 'fields' => $fielddata, + 'indexes' => $indexdata, 'foreign_keys' => $foreigndata, 'table_status' => $table_status ]; @@ -722,7 +738,7 @@ class DBStructure { 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]); + DBA::databaseName(), $table, $field]); } /** @@ -778,7 +794,7 @@ class DBStructure } if (!DBA::exists('verb', ['id' => 0])) { - DBA::insert('verb', ['name' => '']); + DBA::insert('verb', ['name' => ''], Database::INSERT_IGNORE); $lastid = DBA::lastInsertId(); if ($lastid != 0) { DBA::update('verb', ['id' => 0], ['id' => $lastid]); @@ -795,8 +811,8 @@ class DBStructure if (self::existsTable('user') && !DBA::exists('user', ['uid' => 0])) { $user = [ - 'verified' => true, - 'page-flags' => User::PAGE_FLAGS_SOAPBOX, + 'verified' => true, + 'page-flags' => User::PAGE_FLAGS_SOAPBOX, 'account-type' => User::ACCOUNT_TYPE_RELAY, ]; DBA::insert('user', $user); @@ -814,7 +830,7 @@ class DBStructure } if (self::existsTable('contact') && !DBA::exists('contact', ['id' => 0])) { - DBA::insert('contact', ['nurl' => '']); + DBA::insert('contact', ['nurl' => ''], Database::INSERT_IGNORE); $lastid = DBA::lastInsertId(); if ($lastid != 0) { DBA::update('contact', ['id' => 0], ['id' => $lastid]); @@ -829,7 +845,7 @@ class DBStructure } if (self::existsTable('tag') && !DBA::exists('tag', ['id' => 0])) { - DBA::insert('tag', ['name' => '']); + DBA::insert('tag', ['name' => ''], Database::INSERT_IGNORE); $lastid = DBA::lastInsertId(); if ($lastid != 0) { DBA::update('tag', ['id' => 0], ['id' => $lastid]); @@ -845,7 +861,7 @@ class DBStructure if (self::existsTable('permissionset')) { if (!DBA::exists('permissionset', ['id' => 0])) { - DBA::insert('permissionset', ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']); + DBA::insert('permissionset', ['allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => ''], Database::INSERT_IGNORE); $lastid = DBA::lastInsertId(); if ($lastid != 0) { DBA::update('permissionset', ['id' => 0], ['id' => $lastid]); @@ -872,8 +888,8 @@ class DBStructure $permission = ''; } $fields = ['id' => $set['psid'], 'uid' => $set['uid'], 'allow_cid' => $permission, - 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']; - DBA::insert('permissionset', $fields); + 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '']; + DBA::insert('permissionset', $fields, Database::INSERT_IGNORE); } DBA::close($sets); } @@ -902,7 +918,7 @@ class DBStructure $isUpdate = false; $processes = DBA::select('information_schema.processlist', ['info'], [ - 'db' => DBA::databaseName(), + 'db' => DBA::databaseName(), 'command' => ['Query', 'Execute'] ]);