Rails: Best way to make changes to a production database

后端 未结 4 1535
滥情空心
滥情空心 2021-02-01 10:10

I need to make changes to an in-use production database. Just adding a few columns. I\'ve made the changes to the dev database with migrations. What is the best way to update

4条回答
  •  天涯浪人
    2021-02-01 10:21

    It sounds like you're in a state where the production db schema doesn't exactly match what you're using in dev (although it's not totally clear). I would draw a line in the sand, and get that prod db in a better state. Essentially what you want to do is make sure that the prod db has a "schema_info" table that lists any migrations that you >don't< ever want to run in production. Then you can add migrations to your hearts content and they'll work against the production db.

    Once you've done that you can write migrations that add schema changes or add data, but one thing you need to be really careful about is that if you add data using a migration, you must define the model within the migration itself, like this:

    class AddSomeColumnsToUserTable < ActiveRecord::Migration
      class User < ActiveRecord::Base; end
      def self.up
        add_column :users, :super_cool, :boolean, :default => :false
        u = User.find_by_login('cameron')
        u.super_cool = true
        u.save
      end
    
      def self.down
        remove_column :users, :super_cool
      end
    end
    

    The reason for this is that in the future, you might remove the model altogether, during some refactoring or other. If you don't define the user class on line "User.find_by_login..." the migration will throw an exception which is a big pain.

提交回复
热议问题