db-migration icon indicating copy to clipboard operation
db-migration copied to clipboard

FR: Migration, add encapsulation for primary key, foreign key and etc

Open mdmunir opened this issue 10 years ago • 3 comments

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);

mdmunir avatar Sep 10 '15 14:09 mdmunir

Makes sense to me.

samdark avatar Sep 10 '15 15:09 samdark

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);
}

omnilight avatar Sep 17 '15 11:09 omnilight

Related with #13

Tigrov avatar Oct 16 '23 13:10 Tigrov

Closed in favour of https://github.com/yiisoft/db-migration/issues/13

Tigrov avatar Jan 07 '25 09:01 Tigrov