Capistrano 3.0 Rails 5.0.0 数据库自定义任务

Capistrano 3.0 Rails 5.0.0 database custom task

我已经使用 Capistrano 在 Ubuntu 服务器上部署了一个 Rails 应用程序。但是,我正在尝试 运行 我创建并在 'lib/tasks' 中的自定义任务。我试图通过执行

让它工作

生产上限db:views

好像是自定义任务,但显然没有成功

cap aborted! Don't know how to build task 'db:views'

文件为sql_views.rake,此任务用于在数据库中创建视图

namespace :db do
  desc "Update and create SQL views"
  task :views => :environment do
    Dir["#{Rails.root}/db/sql_views/*.sql"].each do |file_name|
      STDERR.puts "Applying the SQL view at #{file_name}"
      source_file = File.new(file_name, 'r')

      if source_file and (sql_content = source_file.read)
        ActiveRecord::Base.transaction do
          # Each statement ends with a semicolon followed by a newline.
          sql_lines = sql_content.split(/;[ \t]*$/)
          if sql_lines.respond_to?(:each)
            sql_lines.each do |line|
              ActiveRecord::Base.connection.execute "#{line};"
            end
          end
        end # transaction
      end

    end # Dir.each
  end # task
end

如果您使用 cap install 生成 Capfile,则应存在以下行:

# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }

所以它正在 lib/capistrano/tasks 中寻找自定义任务。

lib/tasks/sql_views.rake移动到lib/capistrano/tasks/sql_views.rake

或者只导入每个 rake 任务:

import 'lib/tasks/sql_views.rake'