Question

I am creating a new Rails 3.1 application. I would like this new application to reuses an existing database (which was created by a previous rails 2 application).

I created the new application defining models that reuses some of the existing data in the database.

In the development and test phase everything works fine since it runs on a clean sheet database, but when trying to deploy to production I get messages such as:

PGError: ERROR:  column "email" of relation "users" already exists
*** [err :: localhost] : ALTER TABLE "users" ADD COLUMN "email" character varying(255) DEFAULT '' NOT NULL

however I have in my migration thinks like

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    change_table(:users) do |t|
      t.database_authenticatable :null => false
      t.recoverable
      t.rememberable
      t.trackable
      t.timestamps
    end
end

How can I make db:migrate ignore what already exist and only change the new things and/or new types?

I saw similar questions on stackoverflow, but none answering this question. Thanks for your answers.

Was it helpful?

Solution

If you are using an existing database then you shouldn't try and override it through migrations, you should replicate the existing database in schema.rb and then migrate forwards from there, only adding the fields that have changed.

OTHER TIPS

One approach I've taken is to create a new model (assuming you don't have any migrations yet - carefully delete them if you do), say, "rails generate model user". Among other things, the generator creates the db migration for that model. When you run the db migrate rails, rails creates the users table and creates a schema.rb based on the current state of the existing database. From then on subsequent migrations will be based on the schema.rb and any new changes made.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top