Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

[Proposal] Make the migrations atomic #1970

Closed
sobhanatar opened this issue Dec 16, 2019 · 4 comments
Closed

[Proposal] Make the migrations atomic #1970

sobhanatar opened this issue Dec 16, 2019 · 4 comments

Comments

@sobhanatar
Copy link

sobhanatar commented Dec 16, 2019

For now the migrations don't run in atomic way. Means that if you have the following code, first it creates table, then columns and if everything goes fine, then create the index. Now if you have any problem like duplicate index name or anything that prevents the migration from creating the index, on next run - after correcting the problem - you will get error `Table 'xyz' already exists.

Schema::create('xyz', static function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedInteger('user_id')->nullable(false)->index('user_index');
    $table->unsignedInteger('username')->nullable(false)->index('user_index');
});

But if the creation of table and other after creation processes become atomic this problem won't happen.

Do you think it's ok that we have this feature, or atleast have it as an option on command?

@sobhanatar sobhanatar changed the title Make the migrations atomic [Proposal] Make the migrations atomic Dec 16, 2019
@mfn
Copy link

mfn commented Dec 16, 2019

This is already done automatically via withinTransaction iff the database supports it.

For further complication, i.e. regarding MySQL, see e.g. #1645

@prezire
Copy link

prezire commented Dec 16, 2019

For now the migrations don't run in atomic way. Means that if you have the following code, first it creates table, then columns and if everything goes fine, then create the index. Now if you have any problem like duplicate index name or anything that prevents the migration from creating the index, on next run - after correcting the problem - you will get error `Table 'xyz' already exists.

Schema::create('xyz', static function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedInteger('user_id')->nullable(false)->index('user_index');
    $table->unsignedInteger('username')->nullable(false)->index('user_index');
});

But if the creation of table and other after creation processes become atomic this problem won't happen.

Do you think it's ok that we have this feature, or atleast have it as an option on command?

I've always been a victim of this flow. Thanks for proposing.

@sobhanatar
Copy link
Author

This is already done automatically via withinTransaction iff the database supports it.

For further complication, i.e. regarding MySQL, see e.g. #1645

Thanks @mfn
Seems it's controllbale that way.

@dinamic
Copy link

dinamic commented Mar 22, 2021

Changing a database table in some way - adding/modifying/deleting a column is rarely supported inside a transaction. It's definitely not supported with MySQL which I imagine most of us are using.

The way to control this is by having unit migrations - one migration, one change. Need a column with an index attached to it - have two migrations created - one for the column and another one for the index.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants