有没有更快的方法来编写这个rake命令 - rake db:drop db:create db:migrate db:seed

时间:2013-02-02 16:29:37

标签: ruby-on-rails ruby-on-rails-3 rake

每次我对架构或新迁移文件进行更改时,都会运行以下命令:

rake db:drop db:create db:migrate db:seed

是否有预先建立的等效方法来执行此操作?

我从我读过的内容中想到rake db:reset并没有做同样的事情,但我可能错了。

4 个答案:

答案 0 :(得分:6)

您可以为此创建自定义rake任务 - lib / tasks / db_rebuild_all.rake

namespace :db_tasks do
  desc "Rebuild database"
  task :rebuild, [] => :environment do
    raise "Not allowed to run on production" if Rails.env.production?

    Rake::Task['db:drop'].execute
    Rake::Task['db:create'].execute
    Rake::Task['db:migrate'].execute
    Rake::Task['db:seed'].execute
  end
end

然后运行bundle exec rake db_tasks:rebuild

答案 1 :(得分:3)

  
      
  • 运行rake db:reset && rake db:seed(注意:您必须更新db / schema.rb文件)   
    OR
  •   
  • 运行rake db:migrate:reset && rake db:seed
  •   

答案 2 :(得分:2)

您可以运行rake db:drop然后rake db:setup

db:setup将运行rake db:create db:schema:load and db:seed

但是,每次进行新的迁移时,为什么要删除并重新创建数据库?这就是迁移的目的,对现有数据库进行增量更改。

答案 3 :(得分:0)

首先使用:

创建任务文件(lib / tasks / db.rake)
AverageScore = []
# this is the array where the student' record (name and highest score) is saved
# in a list
count = 0
while count < len(dataList):
    entry = []
# this is whre each student name and score will be temporarily held
    entry.append(dataList[count][0])
# this makes it so the array 'entry' contains the name of every student
    Sum = 0
    frequency = len(dataList[count])
    incount = 1
    while incount < frequency:
        Sum = Sum + int(dataList[count][incount])
        incount = incount + 1 
    average = Sum / (frequency-1)
    entry.append(average)
    AverageScore.append(entry)
# this appends the name and average score of the student to the larger array
# 'AverageScore'
    count= count + 1
# the count is increased so the process is repeated for the next student
AverageSorted = sorted(AverageScore,key=lambda l:l[1], reverse=True)
# http://stackoverflow.com/questions/18563680/sorting-2d-list-python
# this is code i obtained through someone else's post which arranges the array in descending
# order of scores
count2 = 0
while count2 < len(AverageSorted):
    print(AverageSorted[count2][0], ':', AverageSorted[count2][1])
    count2 = count2 + 1
# this formats the array so it prints the elements in a list of each student's
# name and score

然后写下来:

rails g task db reseed