【问题标题】:Rails: migrating production database, need to create new data for every existing userRails:迁移生产数据库,需要为每个现有用户创建新数据
【发布时间】:2011-09-23 18:27:33
【问题描述】:

我的 Ruby on Rails 应用程序中有一个生产数据库,其中包含用户和其他东西。现在,在实现新功能时,我需要为每个现有用户创建一些新数据。有什么聪明的方法吗?可能是 Rake 脚本?

更多细节:

我有一个用户表。现在我添加了书签表。这个想法是每个用户默认有 5 个带有预填充数据的书签。新用户很容易做到,但现有用户呢?在用户登录时创建数据是一种选择,但感觉有点脏。

谢谢!

【问题讨论】:

    标签: ruby-on-rails database database-migration


    【解决方案1】:

    我喜欢将rails runner 用于这样的一次性脚本,它可以让您轻松运行加载了rails env 的脚本。 .

    如果您有很多用户,我建议使用activerecord-import 来加快速度。

    我们可以在脚本目录中创建一个名为 make_bookmarks.rb 的脚本,如下所示:

    require 'activerecord-import'
    
    bookmarks = []
    User.all.each do |user|
        ["value1","value2","value3","value4","value5"].each do |value|
            bookmarks << user.bookmarks.new(:col => value)
        end
    end
    
    # import the bookmarks all with bulk sql = much faster
    Bookmark.import bookmarks
    

    然后运行它rails runner scripts/make_bookmarks.rb

    【讨论】:

      【解决方案2】:

      一个是在您的迁移中包含数据转换,例如(假设您的 has_many 关系使用连接模型 user_bookmark.rb):

      class AddBookmarksTable < ActiveRecord::Migration
        def self.up
          create_table :bookmarks, :force => true do |t|
            t.string  :some_col
            t.timestamps
          end
          create_table :user_bookmarks, :force => true do |t|
            t.integer  :bookmark_id, user_id
            t.timestamps
          end
          # create the 5 bookmarks you want to seed as values for existing users
          Bookmark.create(:some_col => 'value')
          Bookmark.create(:some_col => 'value')
          Bookmark.create(:some_col => 'value')
          Bookmark.create(:some_col => 'value')
          Bookmark.create(:some_col => 'value')
          # add values to the join table for your model
          User.all.each{|u| Bookmark.all.each{|b| UserBookmark.create(:user_id => u.id, :bookmark_id => b.id)}}
        end
      
        def self.down
          drop_table :bookmarks
        end
      end
      

      在你的 user.rb 中你应该有

      has_many :bookmarks
      

      和书签.rb

      belongs_to :user
      

      另一种方式(首选)是只使用您的迁移来创建您的表,因为种子数据并不真正属于迁移,并有一个自定义 rake 任务在 lib/tasks 中为您完成工作.

      lib/tasks/add_bookmarks_to_existing_users.rake

      namespace :db do
        desc "Add bookmarks to existing users"
        task :add_bookmarks_to_existing_users => :environment do
          # create seed book marks (under impression none exist right now and associations have been set up in user and bookmark models)
          Bookmark.create(:some_col => 'value')
          Bookmark.create(:some_col => 'value')
          Bookmark.create(:some_col => 'value')
          Bookmark.create(:some_col => 'value')
          Bookmark.create(:some_col => 'value')
          User.all.each{|u| u.bookmarks << Bookmark.all}
        end
      end
      

      之后你就可以运行了:

      rake db:add_bookmarks_to_existing_users
      

      【讨论】:

      • 有趣的事情,尝试了 rake 任务。它可以工作,但它只为最后一个用户创建数据库条目。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-06
      • 2014-05-03
      相关资源
      最近更新 更多