From d9f5a056258b6a5daebe123a34cff6d67a2cf7ce Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 24 May 2019 19:26:48 +0200 Subject: [PATCH] #2783 --- src/app/Console/Commands/MigrateCommand.php | 29 ++++++++++++++++++- src/app/Magic/Fields/Field.php | 20 ++++++++++++- .../Magic/Models/CubistMagicModelAbstract.php | 22 ++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/app/Console/Commands/MigrateCommand.php b/src/app/Console/Commands/MigrateCommand.php index 19b9ae8..81470f6 100644 --- a/src/app/Console/Commands/MigrateCommand.php +++ b/src/app/Console/Commands/MigrateCommand.php @@ -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); } } diff --git a/src/app/Magic/Fields/Field.php b/src/app/Magic/Fields/Field.php index c4c2414..df9fc86 100644 --- a/src/app/Magic/Fields/Field.php +++ b/src/app/Magic/Fields/Field.php @@ -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]); + } + } } diff --git a/src/app/Magic/Models/CubistMagicModelAbstract.php b/src/app/Magic/Models/CubistMagicModelAbstract.php index a472d52..5205a82 100644 --- a/src/app/Magic/Models/CubistMagicModelAbstract.php +++ b/src/app/Magic/Models/CubistMagicModelAbstract.php @@ -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'); + } + + } } -- 2.39.5