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

[6.x] Fix laravel migrations when migrating to sql server (dropColumn with default value) #31229

Merged
merged 8 commits into from Jan 28, 2020

Conversation

@joelharkes
Copy link
Contributor

joelharkes commented Jan 24, 2020

Currently when running the following script would work in mysql (and mariadb) but would fail in sqlserver.

Schema::create('foo', function($table)
{
     $table->boolean('bar')->default(false);
});
Schema::table('foo', function($table)
{
    $table->dropColumn('bar'); // Don't works!
});

this would throw error: [Illuminate\Database\QueryException] SQLSTATE[42000]: [Microsoft][SQL Server Native Client 11.0][SQL Server]The object 'DF__foo__bar__322242A7' is dependent on column 'bar'. (SQL: alter table "foo" drop column "bar")

The problem: on dropColumn() your fluent code doesn't know if the column had a default constraint. This code added would retrieve wether a default constraint exists on dropped columns and remove them before dropping the column as well, and thus not triggering a is dependent on column constraint QueryException.

This solution

Creates a script before drop column is called to get the Default Constraints and drop these before the column is dropped.

In this case it would run script:

DECLARE @sql NVARCHAR(MAX) = '';
SELECT @sql += 'ALTER TABLE [dbo].[foo] DROP CONSTRAINT ' + OBJECT_NAME([default_object_id]) + ';' "
FROM SYS.COLUMNS
WHERE [object_id] = OBJECT_ID('[dbo].[foo') AND [name] in ('bar');
EXEC(@sql)

Which creates the following sql and executes it:

ALTER TABLE [dbo].[foo] DROP CONSTRAINT DF__foo__bar__322242A7;

Before running sql

alter table foo drop column bar

End result

migrating from mysql to sqlserver becomes almost seamless, no need to specify dropping default constraints manually like proposed in other issues like: #4402

Tests

I couldn't create e2e tests because this laravel travis setup has no sql server?
But locally this fix has worked great for me.

joelharkes added 8 commits Jan 20, 2020
…elharkes/framework into 5.8-fix-sqlserver-drop-column
@yaroslavmo

This comment has been minimized.

Copy link
Contributor

yaroslavmo commented Jan 27, 2020

I faced the same issue today. Really hope they will merge your fix!

@GrahamCampbell GrahamCampbell changed the title [6.0.] Fix laravel migrations when migrating to sql server (dropColumn with default value) [6.0] Fix laravel migrations when migrating to sql server (dropColumn with default value) Jan 27, 2020
@taylorotwell taylorotwell merged commit 812ab55 into laravel:6.x Jan 28, 2020
2 checks passed
2 checks passed
continuous-integration/styleci/pr The analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@GrahamCampbell GrahamCampbell changed the title [6.0] Fix laravel migrations when migrating to sql server (dropColumn with default value) [6.x] Fix laravel migrations when migrating to sql server (dropColumn with default value) Jan 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.