Schema::dropIfExists() Deleted ALL tables in DB on migrate:rollback - php

I ran php artisan migrate:rollback on command line, so the migration deleted ALL TABLES in db.
There is only one migration created but have others tables in used db.
The laravel documentation says:
To drop an existing table, you may use the drop or dropIfExists methods.
and the function down() on migration code is the default like as bellow:
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('image')->unsigned()->nullable();
$table->string('name', 64);
$table->string('slug', 64)->unique();
$table->integer('price')->unsigned();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
I don't think the Schema::dropIfExists('products') work properlly in this case, this is a bug? or my(and laravel documentation) mistake?

Roll back, rolls back each migration of the last batch.
If your all migration in same batch then it will rollback all the migrations. So your all tables will be deleted from the database.

I know this post is over a year old, but this might help others who are having this issue and are looking for a solution. I'm guessing what happened here is if you run php artisan migrate:rollback it will rollback all migrations from the last batch. In other words, it wil run all down() functions in all the migrations in the same batch. So if you have made a down() function in those migrations where you revert the changes (deleting the created table), those will be ran as well.
To prevent this, you can add the --step parameter to indicate how many migrations should be rolled back, like so: php artisan migrate:rollback --step=1. Then only your last migration will be rolled back.

Related

laravel, update table structure but get error table already exist when use php artisan migrate

I have table and I wanted to update on some columns, or if I wanted to add new column the problem is when I want to use php artisan migrate command gives me error table already exist, also Im using depoly file and the command inside it is php artisan migrate --force so hope this is correct or have to add any more command??
public function up()
{
Schema::create('payment_methods', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id')->unsigned();
$table->string('paymentreference')->unique();
$table->string('payment_token');
$table->string('cardnumber'); //updated
$table->string('cardbin')->nullable();
$table->string('cardlast4');
$table->string('cardtype');
$table->string('expirymonth');
$table->string('expiryyear');
$table->string('cardholdername'); //added
$table->timestamps();
});
}
To added new or update field like profile in payment_methods.
Try
Run command:
php artisan make:migration add_profile_to_payment_methods
And in the up() method of the new migration file generated, use Schema::table() method to add the new columns or modifying the table.
public function up()
{
Schema::table('payment_methods', function (Blueprint $table) {
$table->string('profile')->nullable();
});
}
public function down()
{
Schema::table('payment_methods', function (Blueprint $table) {
$table->dropColumn('profile');
});
}
}
Then run migration to update the table using php artisan migrate
You need a new migration to modify existing table.
Create new migration:
php artisan make:migration modify_payment_methods_table
Then open the migration file and put following code in there:
public function up()
{
Schema::table('payment_methods', function (Blueprint $table) {
$table->string('cardnumber')->change();
$table->string('cardholdername');
});
}
public function down()
{
Schema::table('payment_methods', function (Blueprint $table) {
$table->integer('cardnumber')->change(); // todo: if this was not an integer then fix this to be correct type instead of integer to avoid issue in case if you will have to rollback the migration
$table->dropColumn('cardholdername');
});
}
After this run
php artisan migrate
To do this successfully you may need to install additional dependency doctrine/dbal.
You can install that easily with composer:
composer require doctrine/dbal
You have manipulated or an error has occurred in any of the migrations.
Well, now in the migrations table, there isn't a row that contains create_payment_methods_table in the migrations column.
As it does not exist, but the table to which the migration refers, if it exists in your database, it fails you, since the process is as follows:
When you refresh, Laravel reads the migrations table, and executes
each migration file in order, first executing the down or deletion of
the table.
After executing that step in all migrations, go through the UP. When
the down of that table does not exist, when arriving at its demo
file, the up finds that it already exists. And that's why it fails
you.,
The solution is to delete manually the referenced table and rerun the migration

Laravel 5 - Use migrate:rollback with path

I've inserted a column with name im_useless to my table earlier which I do not need anymore.
This is my schema (filename: 2017_02_27_120313_units.php):
public function up()
{
Schema::create('units', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description');
$table->string('im_useless');
$table->timestamps();
});
}
Now I try to remove it, so I used this code inside the down() function:
public function down()
{
Schema::dropColumn('im_useless');
}
New Schema:
public function up()
{
Schema::create('units', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description');
$table->timestamps();
});
}
Now I have to rollback and then migrate again. I try to rollback only that specific migration file, by executing php artisan help migrate:rollback I found out that there is a --path option.
So I tried to rollback that specific migration like this:
php artisan migrate:rollback --path=2017_02_27_120313_units.php
But I get Nothing to rollback
How can I drop that specific column without having to rollback any other migrations?
UPDATE:
I think I have to change the path like this:
php artisan migrate:rollback --path=database/migrations/2017_02_27_120313_units.php
...since my php shell was opened in the project root folder?
However I still get Nothing to rollback
I also tried php artisan migrate --path=2017_02_27_120313_units.php
and php artisan migrate --path=database/migrate/2017_02_27_120313_units.php
...and get Nothing to migrate
UPDATE 2
I think I have messed up my migrations table, because I removed the code inside the down() function and the table was never deleted.
https://stackoverflow.com/a/26077506/4684797
The rollback function is meant to give you the possibility to revert to the version you had right before you migrated, in case something goes wrong when you deploy. If you want to drop a specific column that you don't need anymore, you should treat that as a new migration and drop the column in the up() method.
If your laravel version is >= 5.3 you could simply add the migrations path like packages does.
https://laravel.com/docs/5.3/packages#migrations
Just put this code in your AppServiceProvider boot method:
$this->loadMigrationsFrom(__DIR__.'/path/to/migrations');
Also if you want to include subfolders recursively you can try with "/path/to/migrations/**/*" but I'm not sure if this will work in older laravel versions.
--path param is like a filter. It will work on the latest migrations that rollback will act. You can use path reference starting from datatabase/migrations. For example:
php artisan migrate:rollback --path=database/migrations/2022_10_03_193316_create_something.php
It will work if in your database, you have something like 2022_10_03_193316_create_something.php on migration column plus greatest batch value.
Tested on Laravel 6
Run composer dump-autoload and try again

artisan migrate:rollback error in laravel 5.3

I'm trying to build a simple migration table and then trying to add a column in the table so following is my migration file:
class AddFlagToEmiTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('epins', function (Blueprint $table) {
$table->boolean('flag');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('epins', function (Blueprint $table) {
//
});
}
}
In this I forgot to write the drop value which is:
$table->dropColumn('flag');
Now while adding this and trying to rollback and again the trying to do php artisan migrate it is showing error, and also it is showing output nothing to migrate respectively even I've added drop values in the migration.
I've already tried following:
php artisan optimize
php artisan clear-compiled
composer dump-autoload
It ain't helping me out please see the screenshot:
Help me out. Thanks!
its because artisan cannot find flag column in epins table
There is workaround for this,
add flagcolumn explicitly from phpmyadmin(if you are using mysql) and then try roll back
Hope this method works, ask if any doubt
Please try deleting the tables from the database, modify the migration files and add drop to the down function and migrate again.

How to add column in a table using laravel 5 migration without losing its data?

I have an existing database table and I want to add column on it. However, as I run the php artisan migrate command, it says nothing to migrate. But I already add a Schema for adding table columns. I have read some articles and links that I should run the php artisan migrate:refresh first before the new columns to be added.The problem is, it will erase my existing data in my table. Is there any way I could perform the migration and successfully add columns in my table without deleting my data? Please help me with this. Thanks a lot. Here is my migration code.
public function up()
{
//
Schema::create('purchase_orders', function(Blueprint $table){
$table->increments('id');
$table->string('po_code');
$table->text('purchase_orders');
$table->float('freight_charge');
$table->float('overall_total');
$table->timestamps();
});
Schema::table('purchase_orders', function(Blueprint $table){
$table->string('shipped_via');
$table->string('terms');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
Schema::drop('purchase_orders');
}
I want to add column shipped_via and terms in my purchase_orders table.
Use below command to modify the existing table
php artisan make:migration add_shipped_via_and_terms_colums_to_purchase_orders_table --table=purchase_orders
use --create for creating the new table and --table for modifying the existing table.
Now a new migration file will be created. Inside the up() function in this file add these line
Schema::table('purchase_orders', function(Blueprint $table){
$table->string('shipped_via');
$table->string('terms');
});
And then run php artisan migrate
Laravel has a table in your database where it keeps track of all the migrations that are already executed. So by only changing the migration file Laravel will not automatically rerun that migration for you. Cause the migration is already executed by Laravel.
So the best thing to do is to just create a new migration and put the piece of code in it you already have (you were on the right track!).
public function up()
{
//
Schema::table('purchase_orders', function(Blueprint $table){
$table->string('shipped_via');
$table->string('terms');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
You don't need to populate the down function case the table will be dropped by your current purchase_orders migration.
To migrate the new migration just run:
php artisan migrate

How to create two tables migration using php artisan command for Laravel Project?

I am using composer for migrating two tables like this:
php artisan make:migration create_two_tables --create="projects","tasks"
Its creating the file in database->migration folder.
but while migrating using
php artisan migrate
its creating only table in database like this:
'projects,tasks'
as one table.
I want only one file and only one command like what I do in the top for migrating two tables in db.
Is there any possibility to get this?
Note: My superior ordered me to not change the database migration file at any cost...
Can anyone help me out here...
The make:migration command is just to create a new migration file from a template. To actually define the migration you normally have to edit that file. So in your case you would do this:
php artisan make:migration create_two_tables --create="projects"
Then open the ***_create_two_tables.php migration file and add the second table:
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('projects', function(Blueprint $table)
{
});
Schema::create('tasks', function(Blueprint $table)
{
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('projects');
Schema::drop('tasks');
}
Usually you also want to add actual columns to your tables. And that you do inside the Schema::create closure:
Schema::create('projects', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
// and so on
});
Read more about creating tables with the Schema Builder here
If you want to just create two tables through artisan and not add any columns to those tables you can create two migrations. Create one for each table. When you run
php artisan make:migration create_projects_table --create=projects
php artisan make:migration create_tasks_table --create=tasks
php artisan migrate
it will execute both migrations and create both tables for you.

Categories