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
{
*/
protected $description = 'Make database migrations for Magic Models';
+ protected $_schema;
+
/**
* Execute the console command.
*
*/
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);
+ }
}
/**
*/
public function _migrate($model)
{
-
+ $model->setSchema($this->_schema);
}
}
namespace Cubist\Backpack\app\Magic\Fields;
use Cubist\Backpack\app\Magic\CubistMagicAttribute;
+use Doctrine\DBAL\Schema\Table;
use Exception;
use Illuminate\Support\Str;
protected $_rules = [];
protected $_adminType = 'text';
protected $_databaseType = 'string';
+ protected $_databaseUnique = false;
+ protected $_databaseIndex = false;
+ protected $_databaseAttributes = [];
/**
* @param $attributes
public function getDefaultAttributes()
{
return ['type' => $this->_adminType, 'column' => false, 'form' => 'both', 'rules' => '',
- 'db_type' => $this->_databaseType,
'fillable' => true, 'guarded' => false, 'hidden' => false];
}
{
}
+
+ /**
+ * @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]);
+ }
+ }
}
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;
{
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');
+ }
+
+ }
}