yiisoft / db-migration

The package implementing migration for yiisoft/db.
https://www.yiiframework.com/
BSD 3-Clause "New" or "Revised" License
29 stars 16 forks source link

Better constraints management in migrations #1

Open arogachev opened 5 years ago

arogachev commented 5 years ago

Currently we have to specify foreign key name with both adding and dropping.

$this->addForeignKey(
    'tests_questions_test_id_fkey',
    'tests_questions',
    'test_id',
    'tests',
    'id',
    'CASCADE',
    'CASCADE'
);
$this->dropForeignKey('tests_questions_test_id_fkey', 'tests_questions');

Some DBMS like MySQL generate it automatically, but the dropping part for me is more frustrating. You have to look through all previous migrations and find the name of corresponding foreign key. One solution will be following some convention and ask team members to follow it too, but it will be great if:

1) Foreign key names will be generated automatically so we can just write this:

$this->addForeignKey(
    'tests_questions',
    'test_id',
    'tests',
    'id',
    'CASCADE',
    'CASCADE'
);

2) And for the dropping part we can specify relations something like that:

$this->dropForeignKey('tests_questions', ['test_id' => 'tests.id']);

And foreign key will be found and dropped automatically.

That way developer only cares about relations and not names.

I think It can be applied to primary keys, indices, etc. too.

What do you think? Is it possible with DBMS?

Maybe use some naming convention if DBMS don't support auto generation? In this case we can build constraint name string depending on relations and add / drop it.

Funding

Fund with Polar

arogachev commented 9 years ago

Automatic generation can be applied for example if the constraint name is skipped, so user can choose between manual / automatical handling of it.

Example with users and messages tables.

$this->addForeignKey(
    null,
    'messages',
    'user_id',
    'users',
    'id',
    'CASCADE',
    'CASCADE'
);

Generated foreign key name will be for example: messages_user_id-users-id_fk.

Dropping:

$this->dropForeignKey(null, 'messages', ['id' => 'users.id']);

We can retrieve this string messages_user_id-users-id_fk based on passed relations and drop corresponding foreign key.

arogachev commented 9 years ago

Seems like this concept already used in Phinx. Not sure about their implementation of it.

arogachev commented 9 years ago

And in Phinx relations are not even specified for dropping foreign keys, column name is enough.

arogachev commented 9 years ago

Also similar concept is used in Laravel (for dropping you need to specify string though).

nineinchnick commented 9 years ago

I like the idea. I'm using PostgreSQL and it builds constraint names from it's definition, like table name and column names for FK and index constraints, adding _fkey or _idx at the end. But there's a 63 character limit for name length, so that could be an issue.

Other DBMS need to be checked about this.

unclead commented 9 years ago

:+1:

omnilight commented 9 years ago

Absolutely aggree, currently it is a pain to write foreight keys every time

samdark commented 9 years ago

The issue is that each DBMS names its keys differently so I would not rely on it. Personally I'm using idx-table-my_column1-my_column2 and fk-table-column_1-foreign_table-foreign_column.

samdark commented 9 years ago

What worries me is that there could be an index for multiple columns and there could be multiple indexes of different types for a single column.

samdark commented 9 years ago

Many foreign keys for a single column happens daily.

arogachev commented 9 years ago

OK, so it's better to use uniform naming convention instead. But as @nineinchnick said, DBMS need to be checked for max length limitations etc.

arogachev commented 9 years ago

@samdark Could you provide an example of multiple foreign keys?

samdark commented 9 years ago

That's morning sleepy typo. Of course, I've meant indexes.

TerraSkye commented 9 years ago

you could overide it yourself for this particular case and let it auto generate. its the same i did, altough i have a function for the keys.

at the end off the UP i add all the fk's with auto generated names.

at the beginning of the "down" i remove the keys by the using the same configuration. thus the same way its build.

arogachev commented 9 years ago

Of course we can override and implement by ourselves, but I personally expect this feature out of the box.

TerraSkye commented 9 years ago

the problem with that is that project that move to Yii with an existing database cant use that feature. even better would be that u could specify the markup yourself ea : "FK"- {basteTable}-{columns}-{reftable}-{columns}

arogachev commented 9 years ago

Some kind of migration mechanism can be implemented. Alternatively users can continue to use existing algorithm (I described it above).

samdark commented 5 years ago

https://github.com/yiisoft/yii2/pull/13018

samdark commented 1 year ago

https://github.com/yiisoft/db-migration/issues/5 seems the issue was incorrectly split when moving.