# Is it possible to run just one migration

**URL:** <https://www.ruby-forum.com/t/is-it-possible-to-run-just-one-migration/132587>\
**Category:** Rails\
**Created:** [March 21, 2008, 6:02pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-run-just-one-migration/132587 "2008-03-21T18:02:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [March 21, 2008, 6:02pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-run-just-one-migration/132587/1 "2008-03-21T18:02:42Z")

</div>

I have modified an existing migration and I like to update the db with  
that. But the problem is I’ll to run all the migrations in order for  
this change to take affect. That means I’ll also loose all my data.

Is it possible to run just one migration? I know this is a weird case.  
But I am interested in knowing if anybody has come across such a  
situation and solved it.

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [March 21, 2008, 6:07pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-run-just-one-migration/132587/2 "2008-03-21T18:07:25Z")

</div>

On Fri, Mar 21, 2008 at 10:01:47AM -0700, Subbu wrote:

> I have modified an existing migration and I like to update the db with  
> that. But the problem is I’ll to run all the migrations in order for  
> this change to take affect. That means I’ll also loose all my data.
> 
> Is it possible to run just one migration? I know this is a weird case.  
> But I am interested in knowing if anybody has come across such a  
> situation and solved it.

This is what migrations are about. You don’t modify existing ones, you  
create new ones. Whatever you are doing to your schema, do it  
incrementally in a new migration and run that. If it involves modifying  
data to perform the migration, put that in the migration as well.

Migrations are not just about creating database tables. Any schema  
change  
is a migration. Any migration (except those involving unrecoverable data  
loss, which are very rare) should be reversible.

–Greg

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [March 21, 2008, 7:09pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-run-just-one-migration/132587/3 "2008-03-21T19:09:10Z")

</div>

> Migrations are not just about creating database tables. Any schema change  
> is a migration. Any migration (except those involving unrecoverable data  
> loss, which are very rare) should be reversible.

Where do I insert dummy-data in my develop-database? In “Rails Cookbook”  
you can see often migrations, which create a table and insert same  
test-data.  
But if you add an mandatory column to this table the insert will fail  
because the mandatory field only exists in the model and not in the  
migrations-file.

Martin

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [March 21, 2008, 8:30pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-run-just-one-migration/132587/4 "2008-03-21T20:30:05Z")

</div>

> Test data does not belong in migrations. That’s what fixtures are for. I  
> don’t have the Rails Cookbook, so I can’t speak directly to what you’ve  
> found in it, but I’m surprised that it would recommend using migrations to  
> load test data, or even show it as an example.

No, I don’t mean test-data for unit tests.

e.g.: two tables  
users and types

class User \< ActiveRecord::Base  
belongs\_to :type  
end

class Type \< ActiveRecord::Base  
has\_many :users  
end

So I’d like to fill table types with 4 or 5 values so I can add users  
and develop the stylesheets etc for the users\_controller.

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [March 21, 2008, 8:39pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-run-just-one-migration/132587/5 "2008-03-21T20:39:36Z")

</div>

The recommended approach for test data is using fixtures. I’m sure you  
can find info on fixtures in the Rails Cookbook.

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [March 21, 2008, 7:52pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-run-just-one-migration/132587/6 "2008-03-21T19:52:58Z")

</div>

On Fri, Mar 21, 2008 at 07:08:15PM +0100, Martin wrote:

> > Migrations are not just about creating database tables. Any schema  
> > change is a migration. Any migration (except those involving  
> > unrecoverable data loss, which are very rare) should be reversible.
> 
> Where do I insert dummy-data in my develop-database? In “Rails Cookbook”  
> you can see often migrations, which create a table and insert same  
> test-data.

Test data does not belong in migrations. That’s what fixtures are for. I  
don’t have the Rails Cookbook, so I can’t speak directly to what you’ve  
found in it, but I’m surprised that it would recommend using migrations  
to  
load test data, or even show it as an example.

> But if you add an mandatory column to this table the insert will fail  
> because the mandatory field only exists in the model and not in the  
> migrations-file.

Keep your test data up to date with your schema, but not in your  
migrations.

> Martin  
> –Greg

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [March 22, 2008, 10:43pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-run-just-one-migration/132587/7 "2008-03-22T22:43:13Z")

</div>

I’ve run a single migration this way:

$ ruby script/console  
irb\> require ‘db/migration/666\_my\_migration’  
irb\> MyMigration.down

Or MyMigration.up, whatever suits you. I’ve done this, of course, only  
when messing with migrations locally, before commiting them to the  
repository. You should never change a migration that has gone to  
production. And depending on what you’re doing, you might need to  
adjust the schema\_version table manually. Hope that helps.

Cheers,

Helder

---

<div class="post-metadata">

**Author:** ![subbu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@subbu](https://www.ruby-forum.com/u/subbu)\
**Post date:** [March 23, 2008, 7:00am UTC](https://www.ruby-forum.com/t/is-it-possible-to-run-just-one-migration/132587/8 "2008-03-23T07:00:17Z")

</div>

Its just the development database that I want to modify. I have the  
habit of keeping everything to do with one table in a single migration  
file. I find this better organized than scattering db modifications  
across files. I know this can’t be achieved once the migration is  
deployed. But at least till I am in the development mode I try to do  
that.

Using console to load the migrations is a good idea. Worked for me.  
Thanks for the suggestion. I also tried:  
./script/runner “require ‘db/migrate/011\_create\_photos’;  
CreatePhotos.down”  
./script/runner “require ‘db/migrate/011\_create\_photos’;  
CreatePhotos.up”

…even that works.

-subbu
