Generate Rails migrations from a schema
NickName:AaronThomson Ask DateTime:2011-04-01T08:17:51

Generate Rails migrations from a schema

I am creating a new Rails application which will work with an existing schema. I have been given the schema SQL but I want to create Rails migrations to populate the database in development. The schema is not overly complicated, with around 20 tables, however I don't want to waste time and risk typos by manually creating the migrations.

Is there a way to generate Rails migrations given a schema's SQL?

Copyright Notice:Content Author:「AaronThomson」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/5507913/generate-rails-migrations-from-a-schema

Answers
François Beausoleil 2011-04-01T00:56:54

Sure, connect your application to your database, then run\n\nrake db:schema:dump\n\n\nThis will give you a db/schema.rb ready with all of your definitions. Now that you have that db/schema.rb, simply copy the contents within the declaration into a new migration. I've done this before, and it works just great.",


More about “Generate Rails migrations from a schema” related questions

Generate Rails migrations from a schema

I am creating a new Rails application which will work with an existing schema. I have been given the schema SQL but I want to create Rails migrations to populate the database in development. The sc...

Show Detail

Any way to make rails not generate a `schema_migrations` table?

I'm using Rails 3 for a project but I am not using migrations (for reasons that aren't relevant to the question). Rather, I'm just editing schema.rb and running rake db:create db:schema:load each t...

Show Detail

Create db schema from migrations with Yii framework

I have an application that is built with Yii framework. It has lots of migrations and now I want to generate a db schema, so it would be possible for other people to install the application without

Show Detail

Rails Migration and Schema (generate/destroy)

I'm playing around with rails migrations and I've ran into a problem which I haven't been able to find a solution to. Basically, I use the rails generators to create and destroy migrations to add ...

Show Detail

Run future Rails migrations after db:schema:load

I feel like this must be a question that has been answered in the past but I could not find anything with Google. According to the Rails guides: There is no need (and it is error prone) to depl...

Show Detail

Rails gem strong_migrations preventing schema from loading

I've recently tried added the strong_migrations gem to my project, but it's preventing me from running any tests because all of the tables in my schema have the force: :cascade option. I get the

Show Detail

While running specs entries are getting delete from schema migrations table

When upgraded to rails 6.1 my specs are failing due to entries are getting delete from schema_migrations table ActiveRecord::SchemaMigration.count (2.1ms) SELECT COUNT(*) FROM "SCHEMA_MIGR...

Show Detail

Rails: PG::InsufficientPrivilege: ERROR: permission denied for relation schema_migrations

I'm trying to create the database in Rails. In Postgres I see the development and test database, however, I'm getting a permissions error. I've tried to follow this link, didn't work for me. Error:...

Show Detail

Ruby on Rails Migrations and Moving Up and Down Basics

I took a couple courses on rails but a few things are unclear to me regarding migrations: 1.) If I generate a migrations and run it, no matter how few operations I perform does rails still create a

Show Detail

Rails migration does not find schema_migrations table if not in main database

I am working with a multi-schema MySQL database. Unfortunately, the main schema (specified in database attribute in database.yml) is NOT the database we can store the schema_migrations table in. ...

Show Detail