예제 #1
0
 public function execute(IOStream $io)
 {
     if ($this->options('environment')) {
         $this->application()->setEnvironment($this->options('environment'));
     }
     $mapper = $this->application()->getMapper();
     if ($mapper->hasTable('migration_log')) {
         $mapper->dropTable('migration_log');
     }
     $table = new Table('migration_log', [], $mapper);
     $res = $table->addColumn('id', 'integer', ['null' => false, 'primaryKey' => true, 'autoIncrement' => true])->addColumn('migration', 'string')->addColumn('batch', 'integer')->addTimestamps()->create();
     if ($res) {
         $io->writeln('Migration table setup successfully', 'green');
     } else {
         $io->showErr('Unable to create migration table in database');
     }
 }
예제 #2
0
 /**
  * @param $tableName
  *
  * @return Table
  */
 public function getTableSchema($tableName = null)
 {
     if (!isset(static::$tableSchema)) {
         $columns = static::$saveable;
         if (is_null($tableName)) {
             $tableName = static::getTableName();
         }
         $table = new Table($tableName, ['primaryKey' => static::getPrimaryKey()]);
         foreach ($columns as $column) {
             $table->addColumn($column);
         }
         static::setTableSchema($table);
     }
     return static::$tableSchema;
 }
예제 #3
0
 public function __construct()
 {
     parent::__construct();
 }
예제 #4
0
 /**
  * {@inheritdoc}
  */
 public function create(Table $table)
 {
     $columns = $table->getColumns();
     $tableOptions = $table->getOptions();
     if (isset($tableOptions['id']) && $tableOptions['id'] === true) {
         $column = new Column();
         $column->setName('id')->setDataType('integer')->setAutoIncrement(true);
         array_unshift($columns, $column);
         $tableOptions['primaryKey'] = 'id';
     } elseif (isset($tableOptions['id']) && is_string($tableOptions['id'])) {
         $column = new Column();
         $column->setName($tableOptions['id'])->setDataType('integer')->setAutoIncrement(true);
         array_unshift($columns, $column);
         $tableOptions['primaryKey'] = $tableOptions['id'];
     }
     $sql = "CREATE TABLE {$this->quote($table->getName())} (";
     /** @var Column $column */
     foreach ($columns as $column) {
         $sql .= "{$this->quote($column->getName())} {$this->getColumnDefinition($column)},";
     }
     // Primary Key Assignment
     if (isset($tableOptions['primaryKey'])) {
         if (is_array($tableOptions['primaryKey'])) {
             $quotedCols = implode(",", $this->arrayQuote($tableOptions['primaryKey']));
             $sql .= " PRIMARY KEY ({$quotedCols})";
         } elseif (is_string($tableOptions['primaryKey'])) {
             $sql .= " PRIMARY KEY ({$this->quote($tableOptions['primaryKey'])})";
         }
     } else {
         $primaryKeysArr = $table->getPrimaryKeyNames();
         if (!empty($primaryKeysArr)) {
             $keys = implode(",", $this->arrayQuote($primaryKeysArr));
             $sql .= " PRIMARY KEY ({$keys})";
         }
     }
     // TODO: add handling for indexes
     // Foreign Key Assignment
     $foreignKeys = $table->getForeignKeys();
     if (!empty($foreignKeys)) {
         foreach ($foreignKeys as $foreignKey) {
             $sql .= ", {$this->getForeignKeyDefinition($foreignKey)}";
         }
     }
     $sql = rtrim($sql, ',');
     $sql .= ") {$table->getTableOptionsStr()};";
     $result = $this->getConnection()->exec($sql);
     if ($result !== false) {
         $result = true;
     }
     return $result;
 }
예제 #5
0
 public function __construct()
 {
     parent::__construct();
     $this->validacao = ['nome' => ['required', 'min' => 3, 'max' => 255], 'cpf' => ['required', 'unique', 'numero', 'max' => 18], 'email' => ['required', 'unique']];
 }
예제 #6
0
 /**
  * @covers \Core\Database\Table::addColumn
  * @covers \Core\Database\Table::addTimestamps
  * @covers \Core\Database\Table::addDelete
  *
  * @return Table
  */
 public function getTable2Schema()
 {
     $table = new Table('soft_delete_test_user', ['primaryKey' => 'id']);
     $table->addColumn('id', 'integer', ['size' => 10, 'autoIncrement' => true])->addColumn('fname', 'string', ['size' => 200])->addColumn('lname', 'string', ['size' => 200])->addColumn('name', 'string', ['size' => 255])->addColumn('age', 'integer', ['size' => 10])->addTimestamps()->addDelete();
     return $table;
 }
예제 #7
0
 public function __construct()
 {
     parent::__construct();
     $this->validacao = ['username' => ['unique', 'min' => 10], 'nome' => ['min' => 3]];
 }
예제 #8
0
 /**
  * @covers \Core\Database\Mapper\MySqlMapper::__construct
  * @covers \Core\Database\Mapper\MySqlMapper::dropTable
  * @covers \Core\Database\Mapper\MySqlMapper::dropTableWithForeignKeys
  * @covers \Core\Database\Table::__construct
  * @covers \Core\Database\Table::addForeignKey
  * @covers \Core\Database\Table::addColumn
  */
 public function testCreate()
 {
     $language = new MySqlMapper($this->getConfig());
     $language->dropTableWithForeignKeys('product_order');
     $language->dropTable('product');
     $language->dropTable('customer');
     $product = new Table('product', ['engine' => 'INNODB'], $this->mapper);
     $product->addColumn('id', 'integer', ['null' => false, 'primaryKey' => true, 'autoIncrement' => true])->addColumn('category', 'integer', ['null' => false, 'primaryKey' => true])->addColumn('price', 'decimal');
     $result1 = $language->create($product);
     $customer = new Table('customer', ['engine' => 'INNODB'], $this->mapper);
     $customer->addColumn('id', 'integer', ['primaryKey' => true]);
     $result2 = $language->create($customer);
     $productOrder = new Table('product_order', ['engine' => 'INNODB'], $this->mapper);
     $productOrder->addColumn('no', 'integer', ['null' => false, 'primaryKey' => true, 'autoIncrement' => true])->addColumn('product_category', 'integer', ['null' => false])->addColumn('product_id', 'integer', ['null' => false])->addColumn('customer_id', 'integer', ['null' => false])->addForeignKey(['product_category', 'product_id'], 'product', ['id', 'category'], ['onUpdate' => 'cascade', 'onDelete' => 'restrict'])->addForeignKey('customer_id', 'customer', 'id');
     $result3 = $language->create($productOrder);
     $this->assertTrue($result1);
     $this->assertTrue($result2);
     $this->assertTrue($result3);
 }