-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[5.5] Use transaction in migrations using Sql Server #22187
Conversation
|
Schema::table('packinglists', function (Blueprint $table) {
$table->integer('user_id')->nullable();
$table->foreign('user_id')->references('id')
->on('wrongUserTable')->onDelete('no action');
});
|
Is this supported on older SQL Server versions? Does this apply to all DDL statements that the migration system can execute, or only selective to some of them? |
|
Why would you want to wrap your migration in a transaction? If it fails (in development) - you just use |
@laurencei not speaking for everyone, but I have nothing against making my life as developer easier :) Sometimes you don't what to eradicate whole database only because minor migration failed. |
Because it:
Why not? The PostgresGrammar works at this way. For me it does not make sense every time a migration fails to have to go into the database and drop a table manually. |
@laurencei A refresh will not help if the problem is a badly written migration that crashes. A scenario would be when developing new migrations and you've messed up a column name somewhere and the migrations fails when trying it out. I do not see any downsides with this change; a failing migration will leave the database in the original state instead of a half-way processed migration that you have to clean-up by hand. |
I said migrate:fresh, not migrate:refresh. This is *exactly* why
migrate:fresh exists, and solves this problem.
Anyway - for everyone who supports it (I don’t); I don’t understand why
this PR is on for SQL Server then. It should be for all drivers, or none.
Having transactions on one driver only seems strange...
|
Both This change does technically introduce the transactions capability to another driver, so we now have two drivers with transactional support. ;) Regarding implementing other drivers;
Source: https://dev.mysql.com/doc/refman/5.7/en/implicit-commit.html I've not looked into what sqlite supports. I usually just have to look at the mysql implementation to find something weird enough for my arguments. ;) |
Good catch, I also don't see any downsides with this - in fact, would be very useful. 👍 SQL Server foreign keys are being created in a separated Unfortunately although transactions are supported in MySQL, those DDL statements use implicit atomic commits as @sisve pointed out - so it would be useless to use in this schema grammar. Same for MariaDB. It seems to also be useful when using SQLite - although SQLite foreign keys are created inline, it could be used when creating tables with indexes or changing multiple columns. 😉 |
I know this this has already been merged and closed, but I just wanted to add my 2 cents to the above discussion to let you know about some alternate use cases.
Not everyone is building microservices and fresh, small apps. Unfortunately for us, sometimes we have to do things in migrations that we cannot do inside a transaction, so the above solution wouldn't work for us (though we don't use SQL server so this PR does not directly affect us). Instead, we modified the migration stub to the following to give ourselves flexibility: /**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::transaction(function () {
// Code goes here
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::transaction(function () {
// Code goes here
});
} This has worked great for us for years. If an exception is thrown (or a Edit: Thought of another use case for migrations where a (re)fresh won't work: Production. If I write a migration that works on my local but for some reason breaks on production, I would prefer that the migration be cleanly rolled back so that it is all-or-nothing. I don't want a half-run migration to have to sort out. It's easy enough to roll back the other migrations in the batch and reset the code until the migration can be debugged. |
No description provided.