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

[Request] migrate:single and migrate:separate #2085

Closed
JohannesMP opened this issue Aug 13, 2013 · 33 comments
Closed

[Request] migrate:single and migrate:separate #2085

JohannesMP opened this issue Aug 13, 2013 · 33 comments

Comments

@JohannesMP
Copy link

To my knowledge, there is currently no way to run migrations as single, or separate batches, such as when testing to see if migrations are working, like when using a set of pre-made migrations in the app/database/migrations folder.

The only way to do so is to manually move migrations out of the migrations folder, and then move each migration in, run php artisan migrate and so on.

This has the potential for user error, since the user might accidentally not move the chronologically oldest migration, which could cause unpredictable results.

Another reason to want to have migrations divided into separate batches is when testing rollbacks, since it lets you rollback just the last migration.

Currently doing so requires the user to manually edit the batch number in the migrations table, which also has the potential for user error and unpredictable results.


Therefore I would like to suggest the addition of two commands:

php artisan migrate:single - migrates the chronologically oldest migration as a new batch. Equivalent to running php artisan migrate with just that single migration in the migrations folder. Note: I am NOT requesting the ability to migrate a single specific migration, since that would go against the principle that they have to be chronological.

php artisan migrate:separate - migrates all available migrations in one command, but does so as individual batches. Equivalent to running php artisan migrate, but the resulting migrations table will show each migration as a separate batch.


Other potential suggestion:

migrate:batch 4 - runs the next 4 available migrations as a single batch. Equivalent to running php artisan migrate with only the 4 chronologically oldest migrations. Since it is equivalent it would not error out if there are less than 4, it just stops after reaching 4 migrations.

@JohannesMP
Copy link
Author

An alternative would be using flags, such as php artisan migrate --separate, which would also allow batching when running the refresh command: php artisan migrate:refresh --separate, though I don't know if that goes with the Laravel command style.

@rmobis
Copy link
Contributor

rmobis commented Aug 16, 2013

I like the idea.

@taylorotwell
Copy link
Member

Just not enough demand or use for a feature like this at the moment.

@yaameen
Copy link

yaameen commented Jan 4, 2015

It would be really handy when it comes to develop module based large applications! 'php artisan migrate --separate'

@shrimpwagon
Copy link

I would appreciate this feature

@ajay-qburst
Copy link

Looking forward to this. This is a much needed feature as seen from http://stackoverflow.com/questions/19102197/running-one-specific-laravel-4-migration-single-file

@benallfree
Copy link

Not sure how much demand is needed, but I would like this too.

@amoseev
Copy link

amoseev commented Feb 3, 2016

Hi!
I like this feature too.
My scenario:
I create table "currencies". I need seeds this table and then update all my previous orders with default currency.
In currenct situation I get the erorr " Key (currency_id)=(1) is not present in table "currencies"." because this two migrations run at once.

@casank25
Copy link

@amoseev +1. I have the same issue here. When doing changes to my app, I needed to do one migration first, then run the seeder and then do the second migration. Yeah, it works well on my local cause i do those steps, but as soon as i pushed to testing environment i came to that problem. Maybe someone knows better workaround.

@camel-chased
Copy link

camel-chased commented Jun 27, 2016

this is good idea 👍
when you working on some project and you don't want to remove all your current data for some reasons and want to add next table there is a problem

there are seeds but sometimes it is not that simple - when your data must be created with other actions as well

@ghost
Copy link

ghost commented Jun 28, 2016

👍 in order to debug a single possibly broken migration out of all the ones that need to run

@YannPl
Copy link

YannPl commented Sep 14, 2016

Same here, it would be very handy to manage multiple migrations in new app deployment with a bunch of migrations

@nachodd
Copy link

nachodd commented Sep 23, 2016

I think that this is a very good idea. I would like to see this feature too.

@randarp
Copy link

randarp commented Oct 25, 2016

If this existed, it would help me with this problem: https://laracasts.com/discuss/channels/laravel/ordering-issues-with-laravel-migrations-and-gitflow. I could write some sort of bash script to look at the "missing" migrations (using php artisan migrate:status) and just run those. I think this a fairly common scenario with any team size greater than 1 that uses GitFlow.

@taylorotwell
Copy link
Member

Can't you use php artisan migrate --step=1

@randarp
Copy link

randarp commented Oct 26, 2016

What happens if there was data created after the migration? This actually happened to us.

Migration 20161026 creates a new table
QA uses a screen to enter in some data into that table
Migration 20161024 gets merged in that is completely independent of 20161026

I am now forced to rollback 20161026, destroying the data QA created just so I can run 20161024. What if the migration is 2 or 3 back? I may have to rollback multiple migrations, destroying any data along the way.

The problem is with processes like GitFlow, there is no guarantee that migrations are merged in order. It would seems much simpler to just allow you to run any migrations, that have not been run. I wouldn't do this in production, but I would sure use it in QA.

@digitlimit
Copy link

I think a quick fix is to protected existing tables when you run php artisan migrate

public function up()
{
        if(Schema::hasTable('address')) return;
        Schema::create('address', function(Blueprint $table) 
        {
        });
}

@brian-lamb-software-engineer
Copy link

brian-lamb-software-engineer commented May 18, 2017

I would like to see a php artisan migrate:refresh --table=some_table so that during development when i want to alter a table, before its populated, i dont keep losing my other dependent data, which i havent made a seed file for, including the user which I have to continually readd, among other support data. Without this option, i am forced to build seed files for other tables. One option can save this. Lots of potential saved development time. This would be the only need for those seed files in my case.

So in effect, without this option, im having to develop many seed files.

@bsadnu
Copy link

bsadnu commented Sep 8, 2017

I am +1 who needs such feature

@bovavrik
Copy link

+1 php artisan migrate:refresh --table=some_table

@rohitkhatri
Copy link

You can achieve this with this gist

https://gist.github.com/rohitkhatri/a3b1237925b609bf7070f924d37b9b92

@brian-lamb-software-engineer

I also saw how you can edit the batch column in the migrate table, if you change the batch number on the row that you want to change, to the highest number, only that table will be rolled back when you do a rollback, then you can migrate it again, effectively refreshing only that table.

@Angelo8828
Copy link

+1

@daraul
Copy link

daraul commented Feb 21, 2018

I copied @rohitkhatri's gist a little create migrate:batch:

https://gist.github.com/daraul/6902e56b39d49bbd762f80525f399b30

You can just run the command to run the next migration in it's own batch. You can pass a batch size, and can also migrate in steps. I tried allowing it to also rollback migrations, but I was too lazy. It can refresh the database, though.

@fridzema
Copy link
Contributor

Any idea to add this to laravel in 2018/2019 as a lot of people want this?

@sahilsharma011
Copy link

sahilsharma011 commented Sep 26, 2018

Can't you use php artisan migrate --step=1

There is no such command @taylorotwell

@jbhasan
Copy link

jbhasan commented Oct 14, 2018

This is very easy to use.

https://packagist.org/packages/sayeed/custom-migrate

@pedzed
Copy link

pedzed commented Oct 28, 2018

I would like to see a php artisan migrate:refresh --table=some_table

I would find this very useful for my foreign keys. I have one separate migrations file for all my foreign keys. It would be nice if somebody could tell me how I could reverse the migrations and rerun them.

@asolopovas
Copy link

Would be great if at least there would be a way to perform 1 migration at a time. In my case I have to change database structure after it has been populated with orders data. Doing it manually is possible, but can have unexpected results. It would be so much easier to write done all changes step by step. Execute 1 migration, then execute couple queries to update data, than execute following migration for example to add a foreign key to a table.

@raraworks
Copy link

@taylorotwell Will this ever be implemented? This kind of feature is quite popular amongst other PHP frameworks.

@buzzyburrows
Copy link

I'm going to add my +1 to this in 2021 :-)

Quick and dirty solution is to use a custom command:

Command line:
php artisan make:command migrate_set_batches_to_singles

Edit the created file:

import DB at the top:
use Illuminate\Support\Facades\DB;

create signature:
$signature = 'migrate:singles'

the good stuff:

handle(){
        DB::update(DB::raw("UPDATE migrations SET batch = id WHERE 1"));
}

Run all of your migrations, then run the command we created 'php artisan migrate:singles'. Check it with 'migrate:status' and roll back individually to your hearts content. If you migrate a new batch all at once, just re-run to make then separate again :-)

Rolling back even works where there are jumps in the batch numbers!

@guluguru
Copy link

In Laravel Framework 8.68.1, migrate now support on single step
for example:
php artisan status
image
php artisan migrate --step then php artisan migrate:status
image
Thought, the --step option of migrate doen not support any arguments, so the only thing you can do is migrate them all or migrate them one by one.

But if you really need migrate something, there is a workaround, don't do that if you need those table's data.

  1. php artisan migrate --step
  2. php artisan migrate:rollback --step N, N migration will be rollback, not N batch
  3. php artisan migrate
  4. php artisan migrate:rollback
  5. php artisan migrate

then, part 3 - part4 is batch 3, and part 5 is batch 4
image

@l-alexandrov
Copy link

I want this feature too.

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

No branches or pull requests