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

[11.x] Added dropColumnsIfExists, dropColumnIfExists and dropForeignIfExists #53305

Merged
merged 9 commits into from
Oct 29, 2024

Conversation

eusonlito
Copy link
Contributor

@eusonlito eusonlito commented Oct 25, 2024

This PR introduces the dropColumnsIfExists method in Builder and dropColumnIfExists / dropForeignIfExists in Blueprint.

These methods add a convenient way to safely drop columns from a table if they exist, preventing errors when the specified columns are absent.

While I am not entirely sure how to approach creating tests for this functionality, I have tested it extensively in my code with different migration scenarios. For example, I've tested it with columns that are present and others that are not, and in every case, no errors were thrown.

Here’s how the new methods can be used in migrations:

Schema::table('assignment', function (Blueprint $table) {
    $table->dropColumnIfExists('model');
    $table->dropColumnIfExists('text', 'remote_id');
});

Schema::table('bot', function (Blueprint $table) {
    $table->dropColumnIfExists('icebr');
    $table->dropColumnIfExists('text');
});

Schema::table('grade', function (Blueprint $table) {
    $table->dropColumnIfExists('submission');
    $table->dropForeignIfExists(['tutor_id']);
    $table->dropColumnIfExists('tutor_id');
    $table->dropColumnIfExists('response');
});

Our team uses a shared database across multiple applications, which requires us to frequently modify the database schema. The dropColumnIfExists functionality is essential for our workflow as it ensures that migrations run smoothly without being interrupted by missing columns. This enhancement could also be beneficial to others who work in environments where Laravel does not have complete control over the database schema, providing a more resilient and error-proof migration process.

Any feedback would be welcome.

Thanks!

@lk77
Copy link

lk77 commented Oct 25, 2024

This won't work on foreign keys, you will need to delete the constraint first

@eusonlito
Copy link
Contributor Author

This won't work on foreign keys, you will need to delete the constraint first

Is the same behaviour as dropColumn.

eusonlito and others added 2 commits October 25, 2024 21:42
Thanks!

Co-authored-by: Punyapal Shah  <53343069+MrPunyapal@users.noreply.github.com>
@lk77
Copy link

lk77 commented Oct 28, 2024

yes, except that i can drop the foreign key before the column,
but i cannot drop a foreign key only when it exists, like i would need to, to be able to use your dropColumnIfExists.

@eusonlito
Copy link
Contributor Author

yes, except that i can drop the foreign key before the column, but i cannot drop a foreign key only when it exists, like i would need to, to be able to use your dropColumnIfExists.

Done, also added dropForeignIfExists.

@eusonlito eusonlito changed the title Added dropColumnsIfExists and dropColumnIfExists Added dropColumnsIfExists, dropColumnIfExists and dropForeignIfExists Oct 28, 2024
@MrPunyapal
Copy link
Contributor

Awesome @eusonlito 👌

@eusonlito eusonlito changed the title Added dropColumnsIfExists, dropColumnIfExists and dropForeignIfExists [11.x] Added dropColumnsIfExists, dropColumnIfExists and dropForeignIfExists Oct 28, 2024
@taylorotwell taylorotwell merged commit b6e478d into laravel:11.x Oct 29, 2024
31 checks passed
@eusonlito
Copy link
Contributor Author

❤️

@hafezdivandari
Copy link
Contributor

hafezdivandari commented Oct 29, 2024

Seems I'm a bit late here! You can't call Schema facade directly on Blueprint, ignoring migration's connection and table's prefix. For example, Schema::connection('secondary')->dropColumnsIfExists('table', ['column']) checks if default.table.column exists and drops secondary.table.column.

Solution: use ifExists fluent attribute and move the logic to Schema\Grammar instead.

@eusonlito
Copy link
Contributor Author

@hafezdivandari Thank you for your review. Could you open a PR here to fix this issue?

@hafezdivandari
Copy link
Contributor

@eusonlito sure.

@hafezdivandari
Copy link
Contributor

hafezdivandari commented Oct 29, 2024

You may check #53337

Update: reverted on #53338

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

Successfully merging this pull request may close these issues.

5 participants