db-migration
db-migration copied to clipboard
FR: Migration, add encapsulation for primary key, foreign key and etc
NO, its not same with Migration::addPrimaryKey() or Migration::addForeignKey(). Its used directly at create table statement.
$this->createTable('{{%auth}}', [
'user_id' => $this->integer()->notNull(),
'source' => $this->string(255)->notNull(),
'source_id' => $this->string(255)->notNull(),
$this->primaryKey(['user_id','source']), // <-- unsure with this name
$this->foreignKey('{{%user}}',['user_id'=>'id'],'CASCADE','CASCADE')
], $tableOptions);
Makes sense to me.
Currently we have $this->primaryKey() that returns for mysql int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, so if we will use $this->primaryKey() the way suggested by @mdmunir , we will lose backward compatibility.
I think, that if current implementation of migration's $this->integer() and other methods return object, why $this->createTable() can not do the same?
In this key we will have:
$this
->createTable('{{%auth}}', [
'user_id' => $this->primaryKey(),
'source' => $this->string(255)->notNull(),
'source_id' => $this->string(255)->notNull(),
], $tableOptions)
->index('user_id')
->index('source_id')
->foreignKey('user_id', '{{%user}}', 'id', 'CASCADE', 'CASCADE');
Each of this methods should execute required query and return some object, for example:
public function createTable($table, $columns, $options = null)
{
echo " > create table $table ...";
$time = microtime(true);
$this->db->createCommand()->createTable($table, $columns, $options)->execute();
echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
return $this->getDb()->getSchema()->createTableSchemaBuilder($table);
}
Related with #13
Closed in favour of https://github.com/yiisoft/db-migration/issues/13