From f7b103af3d72c5574f07fd08f11a0a97dc8c3bf8 Mon Sep 17 00:00:00 2001 From: Jean-Yves Gaulier Date: Thu, 16 Aug 2018 10:07:40 +0200 Subject: [PATCH] PHRAS-2161_coll-id-in-log-docs_MASTER - explicit message when databox is failed to mount. - reconnect db for long migration scripts - mark (comment) doctrine "binary_strings" type so the upgrade is not done every time because the field was read-back as "varchar" - proposal on long migration scripts (could be faster) --- .../Controller/Admin/DataboxesController.php | 3 ++- .../Database/DatabaseMaintenanceService.php | 27 +++++++++++++++++++ .../Phrasea/Model/Types/BinaryString.php | 22 ++++++++++++++- lib/classes/module/console/systemUpgrade.php | 9 +++++-- 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/lib/Alchemy/Phrasea/Controller/Admin/DataboxesController.php b/lib/Alchemy/Phrasea/Controller/Admin/DataboxesController.php index dbfde3393e..7acbe34fb0 100644 --- a/lib/Alchemy/Phrasea/Controller/Admin/DataboxesController.php +++ b/lib/Alchemy/Phrasea/Controller/Admin/DataboxesController.php @@ -177,7 +177,8 @@ class DataboxesController extends Controller catch (\Exception $exception) { return $this->app->redirectPath('admin_databases', [ 'success' => 0, - 'error' => 'mount-failed' + // 'error' => 'mount-failed' + 'error' => $exception->getMessage() ]); } } diff --git a/lib/Alchemy/Phrasea/Core/Database/DatabaseMaintenanceService.php b/lib/Alchemy/Phrasea/Core/Database/DatabaseMaintenanceService.php index 1dd1430876..83213addf8 100644 --- a/lib/Alchemy/Phrasea/Core/Database/DatabaseMaintenanceService.php +++ b/lib/Alchemy/Phrasea/Core/Database/DatabaseMaintenanceService.php @@ -66,6 +66,8 @@ class DatabaseMaintenanceService public function upgradeDatabase(\base $base, $applyPatches) { + $this->reconnect(); + $recommends = []; $allTables = []; @@ -126,6 +128,8 @@ class DatabaseMaintenanceService */ public function alterTableEngine($tableName, $engine, array & $recommends) { + $this->reconnect(); + $sql = 'ALTER TABLE `' . $tableName . '` ENGINE = ' . $engine; try { @@ -145,6 +149,8 @@ class DatabaseMaintenanceService */ public function createTable(\SimpleXMLElement $table) { + $this->reconnect(); + $field_stmt = $defaults_stmt = []; $create_stmt = "CREATE TABLE IF NOT EXISTS `" . $table['name'] . "` ("; @@ -270,6 +276,8 @@ class DatabaseMaintenanceService public function upgradeTable(\SimpleXMLElement $table) { + $this->reconnect(); + $correct_table = ['fields' => [], 'indexes' => [], 'collation' => []]; $alter = $alter_pre = $return = []; @@ -476,6 +484,8 @@ class DatabaseMaintenanceService } foreach ($alter_pre as $a) { + $this->reconnect(); + try { $this->connection->exec($a); } catch (\Exception $e) { @@ -488,6 +498,8 @@ class DatabaseMaintenanceService } foreach ($alter as $a) { + $this->reconnect(); + try { $this->connection->exec($a); } catch (\Exception $e) { @@ -561,6 +573,7 @@ class DatabaseMaintenanceService $this->app['swiftmailer.transport'] = null; foreach ($list_patches as $patch) { + // Gets doctrine migrations required for current patch foreach ($patch->getDoctrineMigrations() as $doctrineVersion) { /** @var \Doctrine\DBAL\Migrations\Version $version */ @@ -579,6 +592,8 @@ class DatabaseMaintenanceService // Execute migration if not marked as migrated and not already applied by an older patch if (!$migration->isAlreadyApplied()) { + $this->reconnect(); + $version->execute('up'); continue; } @@ -586,10 +601,14 @@ class DatabaseMaintenanceService // Or mark it as migrated $version->markMigrated(); } else { + $this->reconnect(); + $version->execute('up'); } } + $this->reconnect(); + if (false === $patch->apply($base, $this->app)) { $success = false; } @@ -597,4 +616,12 @@ class DatabaseMaintenanceService return $success; } + + private function reconnect() + { + if($this->connection->ping() === false) { + $this->connection->close(); + $this->connection->connect(); + } + } } diff --git a/lib/Alchemy/Phrasea/Model/Types/BinaryString.php b/lib/Alchemy/Phrasea/Model/Types/BinaryString.php index 45cb988fc8..dc538adb49 100644 --- a/lib/Alchemy/Phrasea/Model/Types/BinaryString.php +++ b/lib/Alchemy/Phrasea/Model/Types/BinaryString.php @@ -12,6 +12,7 @@ namespace Alchemy\Phrasea\Model\Types; use Doctrine\DBAL\Types\Type; +use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform; class BinaryString extends Type @@ -23,10 +24,21 @@ class BinaryString extends Type return static::BINARY_STRING; } + /** + * {@inheritdoc} + * + * @see: https://blog.vandenbrand.org/2015/06/25/creating-a-custom-doctrine-dbal-type-the-right-way/ + * about the reason of the COMMENT in the column + */ public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) { if ($platform->getName() === 'mysql') { - return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration)." ". $platform->getCollationFieldDeclaration('utf8_bin'); + /** @var MySqlPlatform $platform */ + return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration) + // . " CHARACTER SET utf8" + . " " . $platform->getColumnCollationDeclarationSQL('utf8_bin') + . " COMMENT '(DC2Type:binary_string)'" + ; } return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration); @@ -39,4 +51,12 @@ class BinaryString extends Type { return $platform->getVarcharDefaultLength(); } + + /** + * @inheritdoc + */ + public function requiresSQLCommentHint(AbstractPlatform $platform) + { + return true; + } } diff --git a/lib/classes/module/console/systemUpgrade.php b/lib/classes/module/console/systemUpgrade.php index 22061fea13..acfcfde8ae 100644 --- a/lib/classes/module/console/systemUpgrade.php +++ b/lib/classes/module/console/systemUpgrade.php @@ -81,8 +81,13 @@ class module_console_systemUpgrade extends Command return $output->writeln(sprintf('You have to fix your database before upgrade with the system:mailCheck command ')); } - $queries = $this->getService('phraseanet.appbox')->forceUpgrade($upgrader, $this->container); - + /** @var appbox $appBox */ + $appBox = $this->getService('phraseanet.appbox'); + $queries = $appBox->forceUpgrade($upgrader, $this->container); + /** + * todo (?) combine schema changes on a table as a simngle sql + * because on big tables like logs, adding 2 columns is 2 very long sql + */ if ($input->getOption('dump') || $input->getOption('stderr')) { if (0 < count($queries)) { $output->writeln("Some SQL queries can be executed to optimize\n");