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

Added dropColumnsIfExists and dropColumnIfExists #53305

Open
wants to merge 3 commits into
base: 11.x
Choose a base branch
from

Conversation

eusonlito
Copy link
Contributor

This PR introduces the dropColumnsIfExists method in Builder and dropColumnIfExists 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->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.

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.

3 participants