# Automatically generate migration from db/SQL?

**URL:** <https://www.ruby-forum.com/t/automatically-generate-migration-from-db-sql/50555>\
**Category:** Rails\
**Created:** [December 18, 2005, 2:29am UTC](https://www.ruby-forum.com/t/automatically-generate-migration-from-db-sql/50555 "2005-12-18T02:29:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@Jason\_E](https://www.ruby-forum.com/u/Jason_E)\
**Post date:** [December 18, 2005, 2:29am UTC](https://www.ruby-forum.com/t/automatically-generate-migration-from-db-sql/50555/1 "2005-12-18T02:29:23Z")

</div>

Hi,

I’d like to use migrations to build my initial database. Is there a gem  
or plugin that will generate the migration code for initial db if I  
point it at a live database or sql file?

If yes, then can you automatically generate the incremental migrations  
as well?

Thanks,  
Jason

---

<div class="post-metadata">

**Author:** ![Jason\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@Jason\_E](https://www.ruby-forum.com/u/Jason_E)\
**Post date:** [December 18, 2005, 2:32am UTC](https://www.ruby-forum.com/t/automatically-generate-migration-from-db-sql/50555/2 "2005-12-18T02:32:23Z")

</div>

Hi Jason,

On 12/17/05, Jason E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I’d like to use migrations to build my initial database. Is there a gem  
> or plugin that will generate the migration code for initial db if I  
> point it at a live database or sql file?

It’s built right in. Uncomment the line in config/environment.rb that  
says:

config.active\_record.schema\_format = :ruby

Then run rake db\_schema\_dump and you’ll have the schema in  
db/schema.rb. This can be loaded with rake db\_schema\_import, or you  
can copy the create\_table statements into a new migration.

> If yes, then can you automatically generate the incremental migrations  
> as well?

No, but if you run your tests db/schema.rb will always be up-to-date.

–  
sam

---

<div class="post-metadata">

**Author:** ![Jason\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@Jason\_E](https://www.ruby-forum.com/u/Jason_E)\
**Post date:** [December 18, 2005, 2:38am UTC](https://www.ruby-forum.com/t/automatically-generate-migration-from-db-sql/50555/3 "2005-12-18T02:38:24Z")

</div>

On Dec 17, 2005, at 8:26 PM, Jason E. wrote:

> Hi,
> 
> I’d like to use migrations to build my initial database. Is there a  
> gem or plugin that will generate the migration code for initial db if  
> I point it at a live database or sql file?
> 
> If yes, then can you automatically generate the incremental migrations  
> as well?

Point rails to your database and do ‘rake db\_schema\_dump’, you could  
then paste the guts of the schema.rb it produces into your initial  
migration.

It could not generate incremental migrations from an existing schema,  
there is no way to know how the schema was put together, only that it  
is what it is.

–  
Scott B.  
Lunchbox Software  
[http://lunchboxsoftware.com](http://lunchboxsoftware.com)  
[http://lunchroom.lunchboxsoftware.com](http://lunchroom.lunchboxsoftware.com)  
[http://rubyi.st](http://rubyi.st)

---

<div class="post-metadata">

**Author:** ![Jason\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@Jason\_E](https://www.ruby-forum.com/u/Jason_E)\
**Post date:** [December 18, 2005, 2:42am UTC](https://www.ruby-forum.com/t/automatically-generate-migration-from-db-sql/50555/4 "2005-12-18T02:42:53Z")

</div>

Also, check out SQL Translator (Google). It’s amazing at migrations -  
just sqlt-diff.  
All we need is for someone to write ActiveRecord.migration bindings to  
it.

Sam S. wrote:

> Hi Jason,
> 
> On 12/17/05, Jason E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi,
> > 
> > I’d like to use migrations to build my initial database. Is there a gem  
> > or plugin that will generate the migration code for initial db if I  
> > point it at a live database or sql file?
> 
> It’s built right in. Uncomment the line in config/environment.rb that  
> says:
> 
> config.active\_record.schema\_format = :ruby
> 
> Then run rake db\_schema\_dump and you’ll have the schema in  
> db/schema.rb. This can be loaded with rake db\_schema\_import, or you  
> can copy the create\_table statements into a new migration.
> 
> > If yes, then can you automatically generate the incremental migrations  
> > as well?
> 
> No, but if you run your tests db/schema.rb will always be up-to-date.
> 
> –  
> sam
