From: Vincent Vanwaelscappel Date: Wed, 17 Jun 2020 13:48:29 +0000 (+0200) Subject: wip #3712 @0.5 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=c00e5a9cbcbe87d13b5d1e55b39f6de53e0e7975;p=cubist_cms-back.git wip #3712 @0.5 --- diff --git a/src/app/Console/Commands/MigrateCommand.php b/src/app/Console/Commands/MigrateCommand.php index 5ccd8c7..cc8be1d 100644 --- a/src/app/Console/Commands/MigrateCommand.php +++ b/src/app/Console/Commands/MigrateCommand.php @@ -24,8 +24,7 @@ class MigrateCommand extends CubistMagicCommand * @var string */ protected $description = 'Make database migrations for Magic Models'; - - protected $_schema; + protected $_schemas = []; /** * Execute the console command. @@ -34,32 +33,34 @@ class MigrateCommand extends CubistMagicCommand */ public function handle() { - $this->_schema = new Schema(); - $this->_handleMagicFolder([$this, '_migrate']); - - $prefix = 'database.connections.' . config('database.default') . '.'; - $drivers = ['mysql' => 'pdo_mysql']; - $comparator = new Comparator(); - $params = [ - 'host' => config($prefix . 'host'), - 'port' => config($prefix . 'port'), - 'user' => config($prefix . 'username'), - 'password' => config($prefix . 'password'), - 'driver' => $drivers[config($prefix . 'driver')], - 'dbname' => config($prefix . 'database'), - ]; - $this->line(print_r($params, true)); - $connection = DriverManager::getConnection($params); - $connection->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); - $currentSchema = $connection->getSchemaManager()->createSchema(); - $diff = $comparator->compare($currentSchema, $this->_schema); + foreach ($this->_schemas as $connection => $schema) { + if (!$connection) { + $connection = config('database.default'); + } + $prefix = 'database.connections.' . $connection . '.'; + + $comparator = new Comparator(); + $params = [ + 'host' => config($prefix . 'host'), + 'port' => config($prefix . 'port'), + 'user' => config($prefix . 'username'), + 'password' => config($prefix . 'password'), + 'driver' => $drivers[config($prefix . 'driver')], + 'dbname' => config($prefix . 'database'), + ]; + $this->line(print_r($params, true)); + $connection = DriverManager::getConnection($params); + $connection->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); + $currentSchema = $connection->getSchemaManager()->createSchema(); + $diff = $comparator->compare($currentSchema, $schema); - $queries = $diff->toSaveSql($connection->getDatabasePlatform()); - foreach ($queries as $q) { - $connection->exec($q); + $queries = $diff->toSaveSql($connection->getDatabasePlatform()); + foreach ($queries as $q) { + $connection->exec($q); + } } Media::whereNull('uuid')->cursor()->each( @@ -73,9 +74,18 @@ class MigrateCommand extends CubistMagicCommand public function _migrate($model) { $this->line('Handling ' . get_class($model)); - $res = $model->setSchema($this->_schema); + + $res = $model->setSchema($this->_getSchema($model->getConnectionName())); if (null !== $res) { $this->line('-- Migrate ' . $res->getName()); } } + + protected function _getSchema($connection) + { + if (!isset($this->_schemas[$connection])) { + $this->_schemas[$connection] = new Schema(); + } + return $this->_schemas[$connection]; + } } diff --git a/src/app/Magic/Fields/APIToken.php b/src/app/Magic/Fields/APIToken.php index da365b6..a357f15 100644 --- a/src/app/Magic/Fields/APIToken.php +++ b/src/app/Magic/Fields/APIToken.php @@ -10,11 +10,14 @@ class APIToken extends Text { protected $_databaseUnique = true; - protected $_databaseAttributes = ['length' => 60]; + protected $_databaseType = 'string'; + protected $_translatable=false; public function __construct($attributes) { - $attributes['default'] = Str::random(60); + $length = 60; + $attributes['default'] = Str::random($length); + $this->_databaseAttributes['length'] = $length; parent::__construct($attributes); } } diff --git a/src/app/Magic/Fields/Field.php b/src/app/Magic/Fields/Field.php index 30776a1..f26d61d 100644 --- a/src/app/Magic/Fields/Field.php +++ b/src/app/Magic/Fields/Field.php @@ -31,7 +31,6 @@ class Field implements \ArrayAccess protected $_databaseType = 'text'; protected $_databaseUnique = false; protected $_databaseIndex = false; - protected $_databaseLength = null; /** @var bool|string|array */ protected $_cast = false; @@ -190,11 +189,15 @@ class Field implements \ArrayAccess if ($name === 'created_at' || $name === 'updated_at') { return; } + + $attributes = array_merge( + ['notnull' => !$this->getAttribute('allow_null', true)], + $this->_databaseAttributes + ); + $table->addColumn($name, CubistMagicAbstractModel::toDoctrineType($this->getDatabaseType()), - array_merge( - ['notnull' => !$this->getAttribute('allow_null', true)], - $this->_databaseAttributes) + $attributes ); if ($this->_databaseUnique) { $table->addUniqueIndex([$name]);