Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting Rails over top of an existing database

Tags:

I have an application written in PHP/MySQL (symfony, to be specific) that I'd (potentially) like to rewrite in Rails. I know how to create scaffolding for tables that don't exist yet, but how do I get Rails to read my existing table structure and create scaffolding based on that?

Update: it turns out I can run the following command to get Rails to generate models for me:

rails generate scaffold Bank --no-migration 

But it doesn't give me forms. I would prefer something that gives me forms.

like image 753
Jason Swett Avatar asked Jan 04 '11 19:01

Jason Swett


People also ask

How do I change a database in Rails?

Notes. To use the Rails 6 db system change command, you simply need to specify the new database adapter with the --to option. Keep in mind, you may need to change the version numbers of the database adapter in your Gemfile after running this command.

What database does Rails use by default?

Rails defaults to using a SQLite database when creating a new project, but you can always change it later.

Does Ruby on Rails work with MySQL?

Ruby on Rails uses SQLite as its database by default, but it also supports the use of MySQL.


2 Answers

The answer is db:schema:dump.

http://guides.rubyonrails.org/migrations.html

like image 55
Jason Swett Avatar answered Oct 11 '22 22:10

Jason Swett


The easiest route is to pretend that you are writing a fresh app with a similar database schema - you can then create your models and migrations with the old schema in mind, but without being restricted by it. At a later stage, you can create a database migration script to copy all the old data into the new schema.

I'm doing this right now. The benefit of this approach is that you can take advantage of all of the rapid development tools and techniques provided by Rails (including scaffolds) without being slowed by trying to retrofit to the exact same schema.

However, if you do decide that you don't like this approach, and you do need to map your new models to existing tables, there are a number of configuration options provided by active record where you can override the convention over configuration naming patterns and map model names to tables names, set oddly named ID fields etc. For example:

class Mammals < ActiveRecord::Base   set_table_name "tbl_Squirrels"   set_primary_key :squirrel_id end 

The above will help Rails attempt to read your existing table, but success will depend upon how well the existing table structures matches Rails conventions. You may have to supply more configuration information to get it to work, and even then it might not work.

Finally, it may be worth considering the use of DataMapper which I believe is more suited to existing brownfield databases than ActiveRecord, because it allows you to map everything, but of course you will need to learn that API if you don't already know it.

like image 27
Scott Avatar answered Oct 11 '22 21:10

Scott