From e68a489a1ef1cc57df4830255353190ccd0482eb Mon Sep 17 00:00:00 2001 From: Taras Machyshyn Date: Mon, 16 Dec 2013 14:27:27 +0200 Subject: [PATCH] add rebiuld action /app/rebiuld --- application/Espo/Controllers/Rebuild.php | 26 ++++++++++++++ application/Espo/Core/Application.php | 18 ++++------ application/Espo/Core/Container.php | 9 +++++ .../Espo/Core/Utils/Database/Converter.php | 2 ++ .../Utils/Database/{ => Converters}/Links.php | 4 +-- .../Core/Utils/Database/Converters/Orm.php | 36 ++++++++++++++++++- .../Database/{ => Converters}/Relations.php | 2 +- .../Core/Utils/Database/Converters/Schema.php | 17 +++------ .../Espo/Core/Utils/Database/Schema.php | 1 + application/Espo/Core/Utils/Metadata.php | 17 +++++++-- 10 files changed, 102 insertions(+), 30 deletions(-) create mode 100644 application/Espo/Controllers/Rebuild.php rename application/Espo/Core/Utils/Database/{ => Converters}/Links.php (97%) rename application/Espo/Core/Utils/Database/{ => Converters}/Relations.php (98%) diff --git a/application/Espo/Controllers/Rebuild.php b/application/Espo/Controllers/Rebuild.php new file mode 100644 index 0000000000..767a82218d --- /dev/null +++ b/application/Espo/Controllers/Rebuild.php @@ -0,0 +1,26 @@ +getContainer()->get('schema')->rebuild(); + } catch (\Exception $e) { + $result = false; + $GLOBALS['log']->add('EXCEPTION', 'Fault to rebuild database schema'.'. Details: '.$e->getMessage()); + } + + if ($result === false) { + throw new Error("Error while rebuilding database"); + } + + return json_encode($result); + } + +} diff --git a/application/Espo/Core/Application.php b/application/Espo/Core/Application.php index 396da464de..375a55815e 100644 --- a/application/Espo/Core/Application.php +++ b/application/Espo/Core/Application.php @@ -77,16 +77,6 @@ class Application $isNotCached = !$this->getMetadata()->isCached(); $this->getMetadata()->init($isNotCached); - - if ($isNotCached) { - $schema = new \Espo\Core\Utils\Database\Schema($this->container->get('config'), $this->getMetadata(), $this->container->get('fileManager')); - - try{ - $schema->rebuild(); - } catch (\Exception $e) { - $GLOBALS['log']->add('EXCEPTION', 'Fault to rebuild database schema'.'. Details: '.$e->getMessage()); - } - } } protected function routeHooks() @@ -180,7 +170,7 @@ class Application 'method' => 'get', 'route' => 'metadata', 'params' => array( - 'controller' => 'Metadata', + 'controller' => 'Metadata', ), ), array( @@ -239,6 +229,12 @@ EOT; ); })->via('PATCH'); + $this->getSlim()->get('/app/rebuild/', function() { + return array( + 'controller' => 'Rebuild', + ); + }); + /*$this->getSlim()->get('/:controller/:id', function() { diff --git a/application/Espo/Core/Container.php b/application/Espo/Core/Container.php index 62031ba721..3792284649 100644 --- a/application/Espo/Core/Container.php +++ b/application/Espo/Core/Container.php @@ -138,6 +138,15 @@ class Container $this->get('user') ); } + + private function loadSchema() + { + return new \Espo\Core\Utils\Database\Schema( + $this->get('config'), + $this->get('metadata'), + $this->get('fileManager') + ); + } public function setUser($user) { diff --git a/application/Espo/Core/Utils/Database/Converter.php b/application/Espo/Core/Utils/Database/Converter.php index a806e6def5..c2880631e4 100644 --- a/application/Espo/Core/Utils/Database/Converter.php +++ b/application/Espo/Core/Utils/Database/Converter.php @@ -79,6 +79,8 @@ class Converter $databaseMeta = Util::merge($databaseMeta, $this->getOrmConverter()->process($entityName, $entityMeta, $entityDefs)); } + $databaseMeta = $this->getOrmConverter()->prepare($databaseMeta); + $schema = $this->getSchemaConverter()->process($databaseMeta, $entityDefs); $this->setSchemaFromMetadata($schema); diff --git a/application/Espo/Core/Utils/Database/Links.php b/application/Espo/Core/Utils/Database/Converters/Links.php similarity index 97% rename from application/Espo/Core/Utils/Database/Links.php rename to application/Espo/Core/Utils/Database/Converters/Links.php index 94f9544f2c..d3c43e582f 100644 --- a/application/Espo/Core/Utils/Database/Links.php +++ b/application/Espo/Core/Utils/Database/Converters/Links.php @@ -1,6 +1,6 @@ metadata = $metadata; - $this->relations = new \Espo\Core\Utils\Database\Relations(); + $this->relations = new Relations(); } protected function getMetadata() diff --git a/application/Espo/Core/Utils/Database/Converters/Orm.php b/application/Espo/Core/Utils/Database/Converters/Orm.php index 7d14bbd3cc..bd46ed8368 100644 --- a/application/Espo/Core/Utils/Database/Converters/Orm.php +++ b/application/Espo/Core/Utils/Database/Converters/Orm.php @@ -34,12 +34,17 @@ class Orm ), ); + protected $idParams = array( + 'dbType' => 'varchar', + 'len' => '24', + ); + public function __construct(\Espo\Core\Utils\Metadata $metadata) { $this->metadata = $metadata; - $this->links = new \Espo\Core\Utils\Database\Links($this->metadata); + $this->links = new \Espo\Core\Utils\Database\Converters\Links($this->metadata); } @@ -74,6 +79,34 @@ class Orm } + public function prepare(array $meta) + { + foreach($meta as $entityName => &$entityParams) { + foreach($entityParams['fields'] as $fieldName => &$fieldParams) { + + switch ($fieldParams['type']) { + case 'id': + case 'foreignId': + $fieldParams = array_merge($fieldParams, $this->idParams); + break; + + case 'foreignType': + $fieldParams['dbType'] = Entity::VARCHAR; + $fieldParams['len'] = $this->defaultLength['varchar']; + break; + + case 'array': + case 'json_array': + unset($fieldParams['default']); //for db type TEXT can't be defined a default value + break; + } + } + } + + return $meta; + } + + @@ -90,6 +123,7 @@ class Orm $outputMeta = array( 'id' => array( 'type' => Entity::ID, + 'dbType' => 'varchar', ), ); diff --git a/application/Espo/Core/Utils/Database/Relations.php b/application/Espo/Core/Utils/Database/Converters/Relations.php similarity index 98% rename from application/Espo/Core/Utils/Database/Relations.php rename to application/Espo/Core/Utils/Database/Converters/Relations.php index d91efd5497..8a2de47484 100644 --- a/application/Espo/Core/Utils/Database/Relations.php +++ b/application/Espo/Core/Utils/Database/Converters/Relations.php @@ -1,6 +1,6 @@ 'varchar', - 'len' => '24', - ); - //pair espo::doctrine protected $allowedDbFieldParams = array( 'len' => 'length', @@ -61,25 +56,21 @@ class Schema case 'id': $primaryColumns[] = Util::toUnderScore($fieldName); - case 'id': - case 'foreignId': - $fieldParams = $this->idParams; - break; - case 'array': case 'json_array': $fieldParams['default'] = ''; //for db type TEXT can't be defined a default value break; } - if (!in_array($fieldParams['type'], $this->typeList)) { - $GLOBALS['log']->add('DEBUG', 'Field type ['.$fieldParams['type'].'] does not exist '.$entityName.':'.$fieldName); + $fieldType = isset($fieldParams['dbType']) ? $fieldParams['dbType'] : $fieldParams['type']; + if (!in_array($fieldType, $this->typeList)) { + $GLOBALS['log']->add('DEBUG', 'Field type ['.$fieldType.'] does not exist '.$entityName.':'.$fieldName); continue; } //echo Util::toUnderScore($fieldName).', '.$fieldParams['type'].', '.print_r($this->getDbFieldParams($fieldParams),1).'
'; - $tables[$entityName]->addColumn(Util::toUnderScore($fieldName), $fieldParams['type'], $this->getDbFieldParams($fieldParams)); + $tables[$entityName]->addColumn(Util::toUnderScore($fieldName), $fieldType, $this->getDbFieldParams($fieldParams)); } $tables[$entityName]->setPrimaryKey($primaryColumns); diff --git a/application/Espo/Core/Utils/Database/Schema.php b/application/Espo/Core/Utils/Database/Schema.php index 95cce5bf61..f08f573f50 100644 --- a/application/Espo/Core/Utils/Database/Schema.php +++ b/application/Espo/Core/Utils/Database/Schema.php @@ -128,6 +128,7 @@ class Schema $result = true; $connection = $this->getConnection(); foreach ($queries as $sql) { + $GLOBALS['log']->add('DEBUG', 'SCHEMA, Execute Query: '.$sql); try { $result &= (bool) $connection->executeQuery($sql); } catch (\Exception $e) { diff --git a/application/Espo/Core/Utils/Metadata.php b/application/Espo/Core/Utils/Metadata.php index a20637ba52..a2ef8028a2 100644 --- a/application/Espo/Core/Utils/Metadata.php +++ b/application/Espo/Core/Utils/Metadata.php @@ -15,12 +15,15 @@ class Metadata private $config; private $uniteFiles; private $fileManager; + private $converter; public function __construct(\Espo\Core\Utils\Config $config, \Espo\Core\Utils\File\Manager $fileManager, \Espo\Core\Utils\File\UniteFiles $uniteFiles) { $this->config = $config; $this->uniteFiles = $uniteFiles; $this->fileManager = $fileManager; + + $this->converter = new \Espo\Core\Utils\Database\Converter($this); } protected function getConfig() @@ -29,7 +32,6 @@ class Metadata } - protected function getUniteFiles() { return $this->uniteFiles; @@ -40,6 +42,11 @@ class Metadata return $this->fileManager; } + protected function getConverter() + { + return $this->converter; + } + public function isCached() { @@ -198,7 +205,13 @@ class Metadata return $this->espoMetadata; } - $this->espoMetadata = $this->getFileManager()->getContent($this->getMetaConfig()->cachePath, 'espoMetadata.php'); + $espoMetadataFile = Util::concatPath($this->getMetaConfig()->cachePath, 'espoMetadata.php'); + + if (!file_exists($espoMetadataFile)) { + $this->getConverter()->process(); + } + + $this->espoMetadata = $this->getFileManager()->getContent($espoMetadataFile); return $this->espoMetadata; }