Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mdmunir opened this issue Sep 10, 2015 · 3 comments
Open
Labels
type:enhancement Enhancement

Comments

@mdmunir
Copy link

mdmunir commented Sep 10, 2015

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);
@samdark
Copy link
Member

samdark commented Sep 10, 2015

Makes sense to me.

@omnilight
Copy link

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

@samdark samdark transferred this issue from yiisoft/yii2 Apr 23, 2019
@samdark samdark transferred this issue from yiisoft/db Apr 24, 2019
@Tigrov Tigrov added the type:enhancement Enhancement label Oct 16, 2023
@Tigrov
Copy link
Member

Tigrov commented Oct 16, 2023

Related with #13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:enhancement Enhancement
Projects
None yet
Development

No branches or pull requests

4 participants