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.
A Rails migration is a tool for changing an application's database schema. Instead of managing SQL scripts, you define database changes in a domain-specific language (DSL). The code is database-independent, so you can easily move your app to a new platform.
Go to db/migrate subdirectory of your application and edit each file one by one using any simple text editor. The ID column will be created automatically, so don't do it here as well. The method self. up is used when migrating to a new version, self.
Run the database migrations first, before you deploy the new code. This means the before code must work with both database schemas, but the after code can assume that the tables have already been added.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With