From 1bf4c75e8bfd32160ee7316c492ddc436f673f37 Mon Sep 17 00:00:00 2001 From: Thomas Müller Date: Wed, 30 Mar 2016 23:38:26 +0200 Subject: Show individual sql schema migration steps during upgrade - on web as well as on the command line --- lib/base.php | 1 + lib/private/db/mdb2schemamanager.php | 19 ++++++++++--------- lib/private/db/migrator.php | 26 +++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/base.php b/lib/base.php index f3076a1181a..706322fb542 100644 --- a/lib/base.php +++ b/lib/base.php @@ -365,6 +365,7 @@ class OC { $systemConfig->setValue('theme', ''); \OCP\Util::addScript('config'); // needed for web root \OCP\Util::addScript('update'); + \OCP\Util::addStyle('update'); // check whether this is a core update or apps update $installedVersion = $systemConfig->getValue('version', '0.0.0'); diff --git a/lib/private/db/mdb2schemamanager.php b/lib/private/db/mdb2schemamanager.php index bcabb6fe57a..f73f6b4351a 100644 --- a/lib/private/db/mdb2schemamanager.php +++ b/lib/private/db/mdb2schemamanager.php @@ -32,15 +32,14 @@ use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\DBAL\Platforms\OraclePlatform; use Doctrine\DBAL\Platforms\PostgreSqlPlatform; use Doctrine\DBAL\Platforms\SqlitePlatform; +use OCP\IDBConnection; class MDB2SchemaManager { - /** - * @var \OC\DB\Connection $conn - */ + /** @var \OC\DB\Connection $conn */ protected $conn; /** - * @param \OCP\IDBConnection $conn + * @param IDBConnection $conn */ public function __construct($conn) { $this->conn = $conn; @@ -77,16 +76,17 @@ class MDB2SchemaManager { $random = \OC::$server->getSecureRandom(); $platform = $this->conn->getDatabasePlatform(); $config = \OC::$server->getConfig(); + $dispatcher = \OC::$server->getEventDispatcher(); if ($platform instanceof SqlitePlatform) { - return new SQLiteMigrator($this->conn, $random, $config); + return new SQLiteMigrator($this->conn, $random, $config, $dispatcher); } else if ($platform instanceof OraclePlatform) { - return new OracleMigrator($this->conn, $random, $config); + return new OracleMigrator($this->conn, $random, $config, $dispatcher); } else if ($platform instanceof MySqlPlatform) { - return new MySQLMigrator($this->conn, $random, $config); + return new MySQLMigrator($this->conn, $random, $config, $dispatcher); } else if ($platform instanceof PostgreSqlPlatform) { - return new Migrator($this->conn, $random, $config); + return new Migrator($this->conn, $random, $config, $dispatcher); } else { - return new NoCheckMigrator($this->conn, $random, $config); + return new NoCheckMigrator($this->conn, $random, $config, $dispatcher); } } @@ -94,6 +94,7 @@ class MDB2SchemaManager { * Reads database schema from file * * @param string $file file to read from + * @return \Doctrine\DBAL\Schema\Schema */ private function readSchemaFromFile($file) { $platform = $this->conn->getDatabasePlatform(); diff --git a/lib/private/db/migrator.php b/lib/private/db/migrator.php index 7ca3f981358..f477da3bad0 100644 --- a/lib/private/db/migrator.php +++ b/lib/private/db/migrator.php @@ -35,6 +35,8 @@ use \Doctrine\DBAL\Schema\SchemaConfig; use \Doctrine\DBAL\Schema\Comparator; use OCP\IConfig; use OCP\Security\ISecureRandom; +use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\GenericEvent; class Migrator { @@ -51,15 +53,23 @@ class Migrator { /** @var IConfig */ protected $config; + /** @var EventDispatcher */ + private $dispatcher; + /** - * @param Connection $connection + * @param \Doctrine\DBAL\Connection|Connection $connection * @param ISecureRandom $random * @param IConfig $config + * @param EventDispatcher $dispatcher */ - public function __construct(\Doctrine\DBAL\Connection $connection, ISecureRandom $random, IConfig $config) { + public function __construct(\Doctrine\DBAL\Connection $connection, + ISecureRandom $random, + IConfig $config, + EventDispatcher $dispatcher = null) { $this->connection = $connection; $this->random = $random; $this->config = $config; + $this->dispatcher = $dispatcher; } /** @@ -215,7 +225,10 @@ class Migrator { $schemaDiff = $this->getDiff($targetSchema, $connection); $connection->beginTransaction(); - foreach ($schemaDiff->toSql($connection->getDatabasePlatform()) as $sql) { + $sqls = $schemaDiff->toSql($connection->getDatabasePlatform()); + $step = 0; + foreach ($sqls as $sql) { + $this->emit($sql, $step++, count($sqls)); $connection->query($sql); } $connection->commit(); @@ -254,4 +267,11 @@ class Migrator { protected function getFilterExpression() { return '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; } + + protected function emit($sql, $step, $max) { + if(is_null($this->dispatcher)) { + return; + } + $this->dispatcher->dispatch('\OC\DB\Migrator::executeSql', new GenericEvent($sql, [$step+1, $max])); + } } -- cgit v1.2.3 From 4b79fb10a28d9edbace8ec44dfb43861c5570a64 Mon Sep 17 00:00:00 2001 From: Thomas Müller Date: Mon, 4 Apr 2016 16:20:53 +0200 Subject: Fix verbose output of upgrade command - not progressbar in this case and the schema migration test has one progressbar now for all tables - before we had one progressbar for each table --- core/ajax/update.php | 5 +++++ core/command/upgrade.php | 38 ++++++++++++++++++++++---------------- lib/private/db/migrator.php | 21 ++++++++++++++++++--- 3 files changed, 45 insertions(+), 19 deletions(-) (limited to 'lib') diff --git a/core/ajax/update.php b/core/ajax/update.php index 8ed0fad57e9..631a8a7871c 100644 --- a/core/ajax/update.php +++ b/core/ajax/update.php @@ -61,6 +61,11 @@ if (OC::checkUpgrade(false)) { $eventSource->send('success', (string)$l->t('[%d / %d]: %s', [$event[0], $event[1], $event->getSubject()])); } }); + $dispatcher->addListener('\OC\DB\Migrator::checkTable', function($event) use ($eventSource, $l) { + if ($event instanceof GenericEvent) { + $eventSource->send('success', (string)$l->t('[%d / %d]: Checking table %s', [$event[0], $event[1], $event->getSubject()])); + } + }); $updater->listen('\OC\Updater', 'maintenanceEnabled', function () use ($eventSource, $l) { $eventSource->send('success', (string)$l->t('Turned on maintenance mode')); diff --git a/core/command/upgrade.php b/core/command/upgrade.php index c2de699d862..cbb1f26f938 100644 --- a/core/command/upgrade.php +++ b/core/command/upgrade.php @@ -139,26 +139,32 @@ class Upgrade extends Command { $updater->setSkip3rdPartyAppsDisable($skip3rdPartyAppsDisable); $dispatcher = \OC::$server->getEventDispatcher(); $progress = new ProgressBar($output); - $progress->setFormat("%message%\n %current%/%max% [%bar%] %percent:3s%%"); - $dispatcher->addListener('\OC\DB\Migrator::executeSql', function($event) use ($progress, $output) { + $progress->setFormat(" %message%\n %current%/%max% [%bar%] %percent:3s%%"); + $listener = function($event) use ($progress, $output) { if ($event instanceof GenericEvent) { - if ($event[0] === 1) { - $output->writeln(''); - $progress->start($event[1]); - } $message = $event->getSubject(); - if (strlen($message) > 30) { - $message = substr($message, 0, 27) . '...'; - } - $progress->setMessage($message); - $progress->setProgress($event[0]); - $progress->display(); - if ($event[0] === $event[1]) { - $progress->finish(); - $output->writeln(''); + if (OutputInterface::VERBOSITY_NORMAL < $output->getVerbosity()) { + $output->writeln(' Checking table ' . $message); + } else { + if (strlen($message) > 60) { + $message = substr($message, 0, 57) . '...'; + } + $progress->setMessage($message); + if ($event[0] === 1) { + $output->writeln(''); + $progress->start($event[1]); + } + $progress->setProgress($event[0]); + if ($event[0] === $event[1]) { + $progress->setMessage('Done'); + $progress->finish(); + $output->writeln(''); + } } } - }); + }; + $dispatcher->addListener('\OC\DB\Migrator::executeSql', $listener); + $dispatcher->addListener('\OC\DB\Migrator::checkTable', $listener); $updater->listen('\OC\Updater', 'maintenanceEnabled', function () use($output) { $output->writeln('Turned on maintenance mode'); diff --git a/lib/private/db/migrator.php b/lib/private/db/migrator.php index f477da3bad0..8b8a34d9865 100644 --- a/lib/private/db/migrator.php +++ b/lib/private/db/migrator.php @@ -56,6 +56,9 @@ class Migrator { /** @var EventDispatcher */ private $dispatcher; + /** @var bool */ + private $noEmit = false; + /** * @param \Doctrine\DBAL\Connection|Connection $connection * @param ISecureRandom $random @@ -76,6 +79,7 @@ class Migrator { * @param \Doctrine\DBAL\Schema\Schema $targetSchema */ public function migrate(Schema $targetSchema) { + $this->noEmit = true; $this->applySchema($targetSchema); } @@ -100,21 +104,22 @@ class Migrator { * @throws \OC\DB\MigrationException */ public function checkMigrate(Schema $targetSchema) { - /** - * @var \Doctrine\DBAL\Schema\Table[] $tables - */ + $this->noEmit = true; + /**@var \Doctrine\DBAL\Schema\Table[] $tables */ $tables = $targetSchema->getTables(); $filterExpression = $this->getFilterExpression(); $this->connection->getConfiguration()-> setFilterSchemaAssetsExpression($filterExpression); $existingTables = $this->connection->getSchemaManager()->listTableNames(); + $step = 0; foreach ($tables as $table) { if (strpos($table->getName(), '.')) { list(, $tableName) = explode('.', $table->getName()); } else { $tableName = $table->getName(); } + $this->emitCheckStep($tableName, $step++, count($tables)); // don't need to check for new tables if (array_search($tableName, $existingTables) !== false) { $this->checkTableMigrate($table); @@ -269,9 +274,19 @@ class Migrator { } protected function emit($sql, $step, $max) { + if ($this->noEmit) { + return; + } if(is_null($this->dispatcher)) { return; } $this->dispatcher->dispatch('\OC\DB\Migrator::executeSql', new GenericEvent($sql, [$step+1, $max])); } + + private function emitCheckStep($tableName, $step, $max) { + if(is_null($this->dispatcher)) { + return; + } + $this->dispatcher->dispatch('\OC\DB\Migrator::checkTable', new GenericEvent($tableName, [$step+1, $max])); + } } -- cgit v1.2.3