# Migrations for an Existing DB

**URL:** <https://www.ruby-forum.com/t/migrations-for-an-existing-db/114274>\
**Category:** Rails\
**Created:** [September 10, 2007, 8:59pm UTC](https://www.ruby-forum.com/t/migrations-for-an-existing-db/114274 "2007-09-10T20:59:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sydneyos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sydneyos](https://www.ruby-forum.com/u/sydneyos)\
**Post date:** [September 10, 2007, 8:59pm UTC](https://www.ruby-forum.com/t/migrations-for-an-existing-db/114274/1 "2007-09-10T20:59:25Z")

</div>

I am writing a RoR app against a legacy Oracle database against which  
other applications are running. My app will use some existing tables  
and will add some. My question is how to handle this and still use  
migrations. I obviously never want to run rake db:migrate against the  
production db and have it delete any tables that are being used by  
other apps (though I wouldn’t mind being able to recreate these in  
dev/  
test), so do I, but would like to use migrations to update the  
production db with the new tables.

1. Create a test and development database that are copies of  
production as my Version 0
2. Create migrations only for the new tables

?

Also, can I use db:schema:dump to get a copy of my production schema  
to use for dev/test? If so, I’m not clear what the steps are to do  
this.

Appreciate any help - thanks.

---

<div class="post-metadata">

**Author:** ![sydneyos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sydneyos](https://www.ruby-forum.com/u/sydneyos)\
**Post date:** [September 10, 2007, 9:24pm UTC](https://www.ruby-forum.com/t/migrations-for-an-existing-db/114274/2 "2007-09-10T21:24:29Z")

</div>

On Sep 10, 2007, at 2:58 PM, sydneyos wrote:

> 1. Create a test and development database that are copies of  
> production as my Version 0
> 2. Create migrations only for the new tables
> 
> ?
> 
> Also, can I use db:schema:dump to get a copy of my production schema  
> to use for dev/test? If so, I’m not clear what the steps are to do  
> this.
> 
> Appreciate any help - thanks.

This isn’t a full solution, but some tips based on having to  
reconstruct migrations on a project where the original developers  
didn’t consistently use migrations (since I’ve not had to deal [yet]  
with a true legacy db in Rails).

put the legacy schema (I don’t know if you can just point your  
database.yml at production and db:schema:dump) into your migration 1:  
db/migrate/001\_legacy\_schema.rb

(I think there’s a :force=\>false on the create table that might help,  
too)

Put the down migration as:  
def self.down  
raise IrreversibleMigration, “Can’t erase Legacy schema”  
end

Manually, create the schema\_information table in production and set  
the version column of the only row to be 1.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sydneyos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sydneyos](https://www.ruby-forum.com/u/sydneyos)\
**Post date:** [September 10, 2007, 10:50pm UTC](https://www.ruby-forum.com/t/migrations-for-an-existing-db/114274/3 "2007-09-10T22:50:43Z")

</div>

Thanks Rob - that’s helpful.

---

<div class="post-metadata">

**Author:** ![sydneyos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sydneyos](https://www.ruby-forum.com/u/sydneyos)\
**Post date:** [September 10, 2007, 11:37pm UTC](https://www.ruby-forum.com/t/migrations-for-an-existing-db/114274/4 "2007-09-10T23:37:53Z")

</div>

On Sep 10, 2007, at 5:25 PM, sydneyos wrote:

> So, if I run rake on production with the version = 1 then only  
> migrations numbered higher will be run, is that the idea?

Right, only the migrations numbered from (select version from  
schema\_information)+1 up to the highest prefix in db/migrate/\*.rb  
will be run. So 001\_legacy\_schema.rb would exist more as  
documentation than anything else and the IrreversibleMigration keeps  
the Migrator from ever going from 1 back to 0 (which is usually an  
empty schema (with the possible exception of the schema\_information  
table itself that the migration creates and maintains).

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sydneyos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sydneyos](https://www.ruby-forum.com/u/sydneyos)\
**Post date:** [September 10, 2007, 11:26pm UTC](https://www.ruby-forum.com/t/migrations-for-an-existing-db/114274/5 "2007-09-10T23:26:51Z")

</div>

So, if I run rake on production with the version = 1 then only  
migrations numbered higher will be run, is that the idea?

---

<div class="post-metadata">

**Author:** ![sydneyos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sydneyos](https://www.ruby-forum.com/u/sydneyos)\
**Post date:** [September 11, 2007, 2:57am UTC](https://www.ruby-forum.com/t/migrations-for-an-existing-db/114274/6 "2007-09-11T02:57:08Z")

</div>

Thanks
