]> _ Git - cubist_cms-back.git/commitdiff
#2783
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 24 May 2019 17:26:48 +0000 (19:26 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 24 May 2019 17:26:48 +0000 (19:26 +0200)
src/app/Console/Commands/MigrateCommand.php
src/app/Magic/Fields/Field.php
src/app/Magic/Models/CubistMagicModelAbstract.php

index 19b9ae8b26d4188bedfc4fe67a868eaa181414e8..81470f62709727ae3b34cdc2fc5f0649ed4fcbe6 100644 (file)
@@ -5,6 +5,9 @@ namespace app\Console\Command;
 
 use app\Console\Commands\CubistCommand;
 use Cubist\Backpack\app\Magic\Models\CubistMagicModelAbstract;
+use Doctrine\DBAL\DriverManager;
+use Doctrine\DBAL\Schema\Comparator;
+use Doctrine\DBAL\Schema\Schema;
 
 class MigrateCommand extends CubistCommand
 {
@@ -22,6 +25,8 @@ class MigrateCommand extends CubistCommand
      */
     protected $description = 'Make database migrations for Magic Models';
 
+    protected $_schema;
+
     /**
      * Execute the console command.
      *
@@ -29,7 +34,29 @@ class MigrateCommand extends CubistCommand
      */
     public function handle()
     {
+        $this->_schema = new Schema();
+
         $this->_handleMagicFolder([$this, '_migrate']);
+
+        $comparator = new Comparator();
+        $connection = DriverManager::getConnection([
+            'host' => env('DB_HOST'),
+            'port' => env('DB_PORT'),
+            'user' => env('DB_USERNAME'),
+            'password' => env('DB_PASSWORD'),
+            'driver' => env('DB_CONNECTION'),
+            'dbname' => env('DB_DATABASE'),
+        ],
+            env('DB_CONNECTION')
+        );
+        $currentSchema = $connection->getSchemaManager()->createSchema();
+        $diff = $comparator->compare($currentSchema, $this->_schema);
+
+        $queries = $diff->toSaveSql($connection->getDatabasePlatform());
+        foreach ($queries as $q) {
+            $this->line($q);
+            $connection->exec($q);
+        }
     }
 
     /**
@@ -37,6 +64,6 @@ class MigrateCommand extends CubistCommand
      */
     public function _migrate($model)
     {
-
+        $model->setSchema($this->_schema);
     }
 }
index c4c241495f0eff743e6d804b54fce3699087df41..df9fc86589799b3a50fbb00b32ad5355aef7ec92 100644 (file)
@@ -4,6 +4,7 @@
 namespace Cubist\Backpack\app\Magic\Fields;
 
 use Cubist\Backpack\app\Magic\CubistMagicAttribute;
+use Doctrine\DBAL\Schema\Table;
 use Exception;
 use Illuminate\Support\Str;
 
@@ -14,6 +15,9 @@ class Field
     protected $_rules = [];
     protected $_adminType = 'text';
     protected $_databaseType = 'string';
+    protected $_databaseUnique = false;
+    protected $_databaseIndex = false;
+    protected $_databaseAttributes = [];
 
     /**
      * @param $attributes
@@ -48,7 +52,6 @@ class Field
     public function getDefaultAttributes()
     {
         return ['type' => $this->_adminType, 'column' => false, 'form' => 'both', 'rules' => '',
-            'db_type' => $this->_databaseType,
             'fillable' => true, 'guarded' => false, 'hidden' => false];
     }
 
@@ -100,4 +103,19 @@ class Field
     {
 
     }
+
+    /**
+     * @param $table Table
+     */
+    public function defineDbColumn($table)
+    {
+        $name = $this->getAttribute('name');
+        $table->addColumn($name, $this->_databaseType, $this->_databaseAttributes);
+        if ($this->_databaseUnique) {
+            $table->addUniqueIndex([$name]);
+        }
+        if ($this->_databaseIndex) {
+            $table->addIndex([$name]);
+        }
+    }
 }
index a472d5265879f36ccb6615f67d722da473e767cd..5205a82870a7d8940761d645cddca1e5a305d109 100644 (file)
@@ -8,6 +8,7 @@ use Cubist\Backpack\app\Magic\Fields\Field;
 use Cubist\Backpack\app\Magic\Util;
 use Cviebrock\EloquentSluggable\Sluggable;
 use Cviebrock\EloquentSluggable\SluggableScopeHelpers;
+use Doctrine\DBAL\Schema\Schema;
 use Illuminate\Database\Eloquent\Model;
 use Cubist\Backpack\app\Magic\CubistMagicAttribute;
 use Illuminate\Support\Str;
@@ -150,4 +151,25 @@ class CubistMagicModelAbstract extends Model
     {
         return Str::studly($this->getOption('name'));
     }
+
+    /**
+     * @param $schema Schema
+     */
+    public function setSchema($schema)
+    {
+        $table = $schema->createTable($this->table);
+        $table->addColumn($this->primaryKey, $this->keyType, ['autoincrement' => $this->incrementing, 'unsigned' => true]);
+        $table->setPrimaryKey([$this->primaryKey], 'pk_' . $this->table);
+
+        foreach ($this->_fields as $field) {
+            $field->defineDbColumn($table);
+        }
+
+        if ($this->timestamps) {
+            $table->addColumn(static::CREATED_AT, 'date');
+            $table->addColumn(static::UPDATED_AT, 'date');
+            $table->addColumn('deleted_at', 'date');
+        }
+
+    }
 }