add rebiuld action /app/rebiuld

This commit is contained in:
Taras Machyshyn
2013-12-16 14:27:27 +02:00
parent 6533caf7e4
commit e68a489a1e
10 changed files with 102 additions and 30 deletions
+26
View File
@@ -0,0 +1,26 @@
<?php
namespace Espo\Controllers;
use \Espo\Core\Exceptions\Error;
class Rebuild extends \Espo\Core\Controllers\Base
{
public function actionRead($params, $data)
{
try{
$result = $this->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);
}
}
+7 -11
View File
@@ -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() {
+9
View File
@@ -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)
{
@@ -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);
@@ -1,6 +1,6 @@
<?php
namespace Espo\Core\Utils\Database;
namespace Espo\Core\Utils\Database\Converters;
use Espo\Core\Utils\Util;
@@ -15,7 +15,7 @@ class Links
{
$this->metadata = $metadata;
$this->relations = new \Espo\Core\Utils\Database\Relations();
$this->relations = new Relations();
}
protected function getMetadata()
@@ -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',
),
);
@@ -1,6 +1,6 @@
<?php
namespace Espo\Core\Utils\Database;
namespace Espo\Core\Utils\Database\Converters;
use Espo\Core\Utils\Util,
Espo\ORM\Entity;
@@ -11,11 +11,6 @@ class Schema
protected $typeList;
protected $idParams = array(
'type' => '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).'<br />';
$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);
@@ -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) {
+15 -2
View File
@@ -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;
}