]> _ Git - cubist_cms-back.git/commitdiff
wip #3712 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 17 Jun 2020 13:48:29 +0000 (15:48 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 17 Jun 2020 13:48:29 +0000 (15:48 +0200)
src/app/Console/Commands/MigrateCommand.php
src/app/Magic/Fields/APIToken.php
src/app/Magic/Fields/Field.php

index 5ccd8c702d184e73d95895c8e936cc55608de391..cc8be1daf58d33f4a1c3ede2c514420a4d7a8236 100644 (file)
@@ -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];
+    }
 }
index da365b6b7a529ee441706789bb2ee7bbb2ca6fa2..a357f15fe399904c6238843c09cdb137f5032b55 100644 (file)
@@ -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);
     }
 }
index 30776a1c51a05f490c1ad7684e1a0b560953bf1d..f26d61dbc82af3b613cdf996a2c6f04cd087fe4a 100644 (file)
@@ -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]);