Using Laravel 5, I've already been through a few DB migrations and realize that a table I created with my first round of migrations is no longer needed. I want to get rid of the table to avoid any sort of confusion in the future, but obviously I also need to prevent the table from being re-created if I reset all migrations.
I'm thinking it would be best to simply delete the migration altogether so if I refresh the table is never created at all. And then maybe just drop the table manually.
I do have data in my DB, so I'd rather not rollback and refresh everything, what is the correct way to drop a table and remove it from being re-created if I refresh my migrations?
You can create a new migration with up function like this
public function up()
{
Schema::table('table_name', function (Blueprint $table) {
$table->drop();
});
}
Off the top of my head I see 2 methods in which you can accomplish this.
Method 1 is long and kind-of annoying if you have 50+ tables. Method 2 is the clean efficient and less invasive way of accomplishing this (I'd chose this one).
Method 1
The following are steps I would take to address the situation:
Export the database tables and data so you have a backup of everything just in case something goes wrong.
Go into your migration directory and find the file specific to that table
Inside of the public function up() insert:
Schema::table('table_name', function (Blueprint $table) {
$table->drop();
});
Go into every table within your migration file and comment out the both the following methods:
public function up()
and
public function down()
Open your console and cd into the directory where your laravel application lives
run the following command:
php artisan migrate
By running the above command the table drop method will activate and all other tables should remain intact (provided you do not have any foreign key constraints that would prevent the database from removing the table).
If you have any Foreign Key constraints active on that table delete them (since you're planning on removing the table anyway).
Method 2
Create a folder within the database\migrations directory and name it something like "passive_rollback"
Simply run the following command:
php artisan migrate:rollback my_table_name_here --path=database/migrations/passive_rollback
The above step will rollback the single table that is located at the path you've specified. This method can also be used for database versioning and organizing the schema by relevancy or sections. Giving you more control over what pieces of the database you wish to work on through laravel!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With