【问题标题】:Capistrano 3.0 Rails 5.0.0 database custom taskCapistrano 3.0 Rails 5.0.0 数据库自定义任务
【发布时间】:2017-01-01 15:05:17
【问题描述】:

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

上限生产数据库:视图

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

上限中止! 不知道如何构建任务 '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

【问题讨论】:

    标签: ruby-on-rails ruby capistrano


    【解决方案1】:

    如果您使用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'
    

    【讨论】:

      猜你喜欢
      • 2014-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 2014-07-12
      • 2014-01-20
      相关资源
      最近更新 更多