use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\RedirectResponse;
+use OCP\DB\Events\AddMissingColumnsEvent;
use OCP\DB\Events\AddMissingIndicesEvent;
use OCP\DB\Types;
use OCP\EventDispatcher\IEventDispatcher;
}
protected function hasMissingColumns(): array {
- $indexInfo = new MissingColumnInformation();
+ $columnInfo = new MissingColumnInformation();
// Dispatch event so apps can also hint for pending index updates if needed
- $event = new GenericEvent($indexInfo);
+ $event = new GenericEvent($columnInfo);
$this->dispatcher->dispatch(IDBConnection::CHECK_MISSING_COLUMNS_EVENT, $event);
- return $indexInfo->getListOfMissingColumns();
+ $event = new AddMissingColumnsEvent();
+ $this->eventDispatcher->dispatchTyped($event);
+ $missingColumns = $event->getMissingColumns();
+
+ if (!empty($missingColumns)) {
+ $schema = new SchemaWrapper(\OCP\Server::get(Connection::class));
+ foreach ($missingColumns as $missingColumn) {
+ if ($schema->hasTable($missingColumn['tableName'])) {
+ $table = $schema->getTable($missingColumn['tableName']);
+ if (!$table->hasColumn($missingColumn['columnName'])) {
+ $columnInfo->addHintForMissingColumn($missingColumn['tableName'], $missingColumn['columnName']);
+ }
+ }
+ }
+ }
+
+ return $columnInfo->getListOfMissingColumns();
}
protected function isSqliteUsed() {
use OC\DB\Connection;
use OC\DB\SchemaWrapper;
+use OCP\DB\Events\AddMissingColumnsEvent;
+use OCP\DB\Types;
+use OCP\EventDispatcher\IEventDispatcher;
use OCP\IDBConnection;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
class AddMissingColumns extends Command {
public function __construct(
private Connection $connection,
- private EventDispatcherInterface $dispatcher,
+ private EventDispatcherInterface $legacyDispatcher,
+ private IEventDispatcher $dispatcher,
) {
parent::__construct();
}
}
protected function execute(InputInterface $input, OutputInterface $output): int {
- $this->addCoreColumns($output, $input->getOption('dry-run'));
+ $dryRun = $input->getOption('dry-run');
+
+ $updated = $this->addCoreColumns($output, $dryRun);
// Dispatch event so apps can also update columns if needed
$event = new GenericEvent($output);
- $this->dispatcher->dispatch(IDBConnection::ADD_MISSING_COLUMNS_EVENT, $event);
+ $this->legacyDispatcher->dispatch(IDBConnection::ADD_MISSING_COLUMNS_EVENT, $event);
+
+ $event = new AddMissingColumnsEvent();
+ $this->dispatcher->dispatchTyped($event);
+ $missingColumns = $event->getMissingColumns();
+
+ if (!empty($missingColumns)) {
+ $schema = new SchemaWrapper($this->connection);
+
+ foreach ($missingColumns as $missingColumn) {
+ if ($schema->hasTable($missingColumn['tableName'])) {
+ $table = $schema->getTable($missingColumn['tableName']);
+ if (!$table->hasColumn($missingColumn['columnName'])) {
+ $output->writeln('<info>Adding additional ' . $missingColumn['columnName'] . ' column to the ' . $missingColumn['tableName'] . ' table, this can take some time...</info>');
+ $table->addColumn($missingColumn['columnName'], $missingColumn['typeName'], $missingColumn['options']);
+ $sqlQueries = $this->connection->migrateToSchema($schema->getWrappedSchema(), $dryRun);
+ if ($dryRun && $sqlQueries !== null) {
+ $output->writeln($sqlQueries);
+ }
+ $updated = true;
+ $output->writeln('<info>' . $missingColumn['tableName'] . ' table updated successfully.</info>');
+ }
+ }
+ }
+ }
+
+ if (!$updated) {
+ $output->writeln('<info>Done.</info>');
+ }
+
return 0;
}
/**
- * add missing indices to the share table
+ * Add missing column for core tables
*
* @param OutputInterface $output
* @param bool $dryRun If true, will return the sql queries instead of running them.
+ * @return bool True when the schema changed
* @throws \Doctrine\DBAL\Schema\SchemaException
*/
- private function addCoreColumns(OutputInterface $output, bool $dryRun): void {
+ private function addCoreColumns(OutputInterface $output, bool $dryRun): bool {
$output->writeln('<info>Check columns of the comments table.</info>');
$schema = new SchemaWrapper($this->connection);
$table = $schema->getTable('comments');
if (!$table->hasColumn('reference_id')) {
$output->writeln('<info>Adding additional reference_id column to the comments table, this can take some time...</info>');
- $table->addColumn('reference_id', 'string', [
+ $table->addColumn('reference_id', Types::STRING, [
'notnull' => false,
'length' => 64,
]);
}
}
- if (!$updated) {
- $output->writeln('<info>Done.</info>');
- }
+ return $updated;
}
}
'OCP\\Contacts\\ContactsMenu\\IProvider' => $baseDir . '/lib/public/Contacts/ContactsMenu/IProvider.php',
'OCP\\Contacts\\Events\\ContactInteractedWithEvent' => $baseDir . '/lib/public/Contacts/Events/ContactInteractedWithEvent.php',
'OCP\\Contacts\\IManager' => $baseDir . '/lib/public/Contacts/IManager.php',
+ 'OCP\\DB\\Events\\AddMissingColumnsEvent' => $baseDir . '/lib/public/DB/Events/AddMissingColumnsEvent.php',
'OCP\\DB\\Events\\AddMissingIndicesEvent' => $baseDir . '/lib/public/DB/Events/AddMissingIndicesEvent.php',
'OCP\\DB\\Exception' => $baseDir . '/lib/public/DB/Exception.php',
'OCP\\DB\\IPreparedStatement' => $baseDir . '/lib/public/DB/IPreparedStatement.php',
'OCP\\Contacts\\ContactsMenu\\IProvider' => __DIR__ . '/../../..' . '/lib/public/Contacts/ContactsMenu/IProvider.php',
'OCP\\Contacts\\Events\\ContactInteractedWithEvent' => __DIR__ . '/../../..' . '/lib/public/Contacts/Events/ContactInteractedWithEvent.php',
'OCP\\Contacts\\IManager' => __DIR__ . '/../../..' . '/lib/public/Contacts/IManager.php',
+ 'OCP\\DB\\Events\\AddMissingColumnsEvent' => __DIR__ . '/../../..' . '/lib/public/DB/Events/AddMissingColumnsEvent.php',
'OCP\\DB\\Events\\AddMissingIndicesEvent' => __DIR__ . '/../../..' . '/lib/public/DB/Events/AddMissingIndicesEvent.php',
'OCP\\DB\\Exception' => __DIR__ . '/../../..' . '/lib/public/DB/Exception.php',
'OCP\\DB\\IPreparedStatement' => __DIR__ . '/../../..' . '/lib/public/DB/IPreparedStatement.php',
--- /dev/null
+<?php
+
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
+ *
+ * @author Joas Schilling <coding@schilljs.com>
+ *
+ * @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 <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCP\DB\Events;
+
+/**
+ * Event to allow apps to register information about missing database columns
+ *
+ * This event will be dispatched for checking on the admin settings and when running
+ * occ db:add-missing-columns which will then create those columns
+ *
+ * @since 28.0.0
+ */
+class AddMissingColumnsEvent extends \OCP\EventDispatcher\Event {
+ /** @var array<array-key, array{tableName: string, columnName: string, typeName: string, options: array{}}> */
+ private array $missingColumns = [];
+
+ /**
+ * @param mixed[] $options
+ * @since 28.0.0
+ */
+ public function addMissingColumn(string $tableName, string $columnName, string $typeName, array $options): void {
+ $this->missingColumns[] = [
+ 'tableName' => $tableName,
+ 'columnName' => $columnName,
+ 'typeName' => $typeName,
+ 'options' => $options,
+ ];
+ }
+
+ /**
+ * @since 28.0.0
+ * @return array<array-key, array{tableName: string, columnName: string, typeName: string, options: array{}}>
+ */
+ public function getMissingColumns(): array {
+ return $this->missingColumns;
+ }
+}
declare(strict_types=1);
/**
- * @copyright Copyright (c) 2023 Julius Härtl <jus@bitgrid.net
+ * @copyright Copyright (c) 2023 Julius Härtl <jus@bitgrid.net>
*
- * @author Julius Härtl <jus@bitgrid.net
+ * @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
namespace OCP;
use Doctrine\DBAL\Schema\Schema;
+use OCP\DB\Events\AddMissingColumnsEvent;
use OCP\DB\Events\AddMissingIndicesEvent;
use OCP\DB\Exception;
use OCP\DB\IPreparedStatement;
public const CHECK_MISSING_PRIMARY_KEYS_EVENT = self::class . '::CHECK_MISSING_PRIMARY_KEYS';
/**
- * @deprecated 22.0.0 this is an internal event
+ * @deprecated 22.0.0 this is an internal event, use {@see AddMissingColumnsEvent} instead
*/
public const ADD_MISSING_COLUMNS_EVENT = self::class . '::ADD_MISSING_COLUMNS';
/**
- * @deprecated 22.0.0 this is an internal event
+ * @deprecated 22.0.0 this is an internal event, use {@see AddMissingColumnsEvent} instead
*/
public const CHECK_MISSING_COLUMNS_EVENT = self::class . '::CHECK_MISSING_COLUMNS';