# Generate a migration from an existing MySQL Database (like Symfony)

**URL:** <https://www.ruby-forum.com/t/generate-a-migration-from-an-existing-mysql-database-like-symfony/122823>\
**Category:** Rails\
**Created:** [December 12, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/generate-a-migration-from-an-existing-mysql-database-like-symfony/122823 "2007-12-12T16:30:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RaSca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cc9497/32.png) [@RaSca](https://www.ruby-forum.com/u/RaSca)\
**Post date:** [December 12, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/generate-a-migration-from-an-existing-mysql-database-like-symfony/122823/1 "2007-12-12T16:30:54Z")

</div>

Hi everybody,  
i’m a rails newbie and i’m trying to convert an existing application  
to rails.  
What i cannot find is a way to “convert” an existing mysql database  
into a rails migration script. I don’t wanto to rewrite all the  
migration scripts and i wonder if there’s a way to automate this  
process.  
For example, in Symfony once you have configured an access to a mysql  
DB in the config file (config/database.yml) you can launch

# symfony propel-build-schema xml

to generate an the XML file (config/schema.xml) containing the db  
structure and after that with

# symfony propel-build-model

and  
#symfony propel-generate-crud frontend tablename Tablename  
you have the complete editable scaffold for the table access  
configured.

Is there a similar way to do these kind of things in rails ? Or the  
only way is to write by hand all the migration scripts ?

Thanks,

–  
RaSca  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![RaSca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cc9497/32.png) [@RaSca](https://www.ruby-forum.com/u/RaSca)\
**Post date:** [December 12, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/generate-a-migration-from-an-existing-mysql-database-like-symfony/122823/2 "2007-12-12T18:40:51Z")

</div>

On Dec 12, 3:22 am, RaSca [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi everybody,  
> i’m a rails newbie and i’m trying to convert an existing application  
> to rails.  
> What i cannot find is a way to “convert” an existing mysql database  
> into a rails migration script.

This can be done in three simple steps:

1. write config/database.yml to reference your database.
2. Run “rake db:schema:dump” to generate db/schema.rb. Here’s the  
documentation:

$ rake -T db:schema:dump  
…  
rake db:schema:dump # Create a db/schema.rb file that can be  
portably used against any DB supported by AR

1. Convert schema.rb into db/migrate/001\_create\_database.rb:

class CreateMigration \< ActiveRecord::Migration  
def self.up  
# insert schema.rb here  
end

def self.down  
# drop all the tables if you really need  
# to support migration back to version 0  
end  
end

---

<div class="post-metadata">

**Author:** ![RaSca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cc9497/32.png) [@RaSca](https://www.ruby-forum.com/u/RaSca)\
**Post date:** [December 12, 2007, 6:43pm UTC](https://www.ruby-forum.com/t/generate-a-migration-from-an-existing-mysql-database-like-symfony/122823/3 "2007-12-12T18:43:16Z")

</div>

On 12 Dic, 17:51, kevin cline [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Dec 12, 3:22 am, RaSca [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> […]  
> This can be done in three simple steps:  
> […]

Great!

Thank you so much.

Bye,

–  
RaSca  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![RaSca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/cc9497/32.png) [@RaSca](https://www.ruby-forum.com/u/RaSca)\
**Post date:** [February 7, 2013, 12:22am UTC](https://www.ruby-forum.com/t/generate-a-migration-from-an-existing-mysql-database-like-symfony/122823/4 "2013-02-07T00:22:51Z")

</div>

This was a big help. Thanks!

One thing though: I couldn’t get the newly created migration file to  
work initially. I kept getting an error that said “uninitialized  
constant CreateDatabase.” I had to rename the migration class  
“CreateDataBase”.

So the first line of the migration file is now:  
class CreateDatabase \< ActiveRecord::Migration

Everything looks good now.

Thanks again.

kevin cline wrote in post #600414:

> On Dec 12, 3:22 am, RaSca [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi everybody,  
> > i’m a rails newbie and i’m trying to convert an existing application  
> > to rails.  
> > What i cannot find is a way to “convert” an existing mysql database  
> > into a rails migration script.
> 
> This can be done in three simple steps:
> 
> 1. write config/database.yml to reference your database.
> 2. Run “rake db:schema:dump” to generate db/schema.rb. Here’s the  
> documentation:
> 
> $ rake -T db:schema:dump  
> …  
> rake db:schema:dump # Create a db/schema.rb file that can be  
> portably used against any DB supported by AR
> 
> 1. Convert schema.rb into db/migrate/001\_create\_database.rb:
> 
> class CreateMigration \< ActiveRecord::Migration  
> def self.up  
> # insert schema.rb here  
> end
> 
> def self.down  
> # drop all the tables if you really need  
> # to support migration back to version 0  
> end  
> end
