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

[10.x] Add createIfNotExist Method to Schema Facade #48706

Closed
wants to merge 4 commits into from

Conversation

tgozo19
Copy link

@tgozo19 tgozo19 commented Oct 11, 2023

This pull request introduces a new method, createIfNotExist, to the Schema Facade.

The existing create method in the Schema Facade allows us to create tables using the following syntax:

Schema::create('users', function (Blueprint $table) {});

However, if we attempt to run another migration to create a table that already exists, it results in a failure.

To address this issue, the createIfNotExist method has been added. This method first checks if the table already exists before attempting to run the migration. If the table exists, it does not throw an error. Instead, it informs the user that the table already exists, exits the current migration, and continues to run any subsequent migrations.

This enhancement improves the robustness of database migrations and provides better feedback to the user during the migration process.

@GrahamCampbell GrahamCampbell changed the title Add createIfNotExist Method to Schema Facade [10.x] Add createIfNotExist Method to Schema Facade Oct 11, 2023
@hafezdivandari
Copy link
Contributor

hafezdivandari commented Oct 11, 2023

No need for a new Schema method for this IMO, it can be done much easier with a new Blueprint fluent command, just like we already do with $blueprint->temporary():

$blueprint->temporary ? 'create temporary' : 'create',

Something like this:

Schema::create('users', function (Blueprint $table) {
    $table->ifNotExists();

    $table->id();
    $table->string('name');
});

@tgozo19
Copy link
Author

tgozo19 commented Oct 11, 2023

Thanks for the contribution @hafezdivandari. Does this actually exist?

Schema::create('users', function (Blueprint $table) {
    $table->ifNotExists();

    $table->id();
    $table->string('name');
});

@hafezdivandari
Copy link
Contributor

@tgozo19 MySQL, SQLite and PostgreSQL support this syntax:

CREATE TABLE foo IF NOT EXISTS ...

It's easy to implement this on Laravel.

@tgozo19
Copy link
Author

tgozo19 commented Oct 11, 2023

Now I have added a 3rd parameter to Schema::create(). the parameter is optional and should either be true or false. It's default value is false. If set to true then we will check if the table already exists before attempting to run the migration. If the table exists, it does not throw an error. Instead, it informs the user that the table already exists, exits the current migration, and continues to run any subsequent migrations.

For it to work as intended it should be used like this

Schema::create('posts', function (Blueprint $table) {}, true);

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

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