Sprockets不需要SQL文件来进行自定义rake任务

问题描述 投票:1回答:2

基于this article,它描述了如何使用Sprockets编写小型SQL清单文件,我在每个rake db:migrate上自动重新创建了我的SQL视图和函数。这非常有效,直到最后一次升级到Rails 5.1

突然,清单文件被编译,但是每个*= require语句都被忽略,我最终得到一个空的清单文件。我已经为DirectiveProcessor尝试了几种评论样式,有和没有文件扩展名,有和没有相对路径。无论我提供什么,我最终得到一个通过数据库执行的空文件。

my setup

分贝/功能/ application.sql

/*
 * This is a manifest file that'll be compiled into application.sql, which will include all the files
 * from db/functions listed below.
 *
 *= require kill_all_connections.sql
 *= require invalidate_emails.sql
 *
 *= require days_until_birthday.sql
*/

LIB /任务/ db_functions.rake

namespace :db do
  desc 'creates DB functions listed in db/functions.sql'
  task :functions => :environment do
    sprocket_env = Sprockets::Environment.new do |env|
      env.register_mime_type('text/sql', '.sql')
      env.register_processor('text/sql', Sprockets::DirectiveProcessor)
      env.append_path 'db/functions'
    end

    ActiveRecord::Base.connection.execute(sprocket_env['application.sql'].to_s)
  end
end

my result

当我执行rails db:functions时查看控制台,我看到以下内容:

(69.2ms)  /*
* This is a manifest file that'll be compiled into application.sql, which will include all the files
* from db/functions listed below.
*


*

*/

所以文件被执行但看起来很空......任何人都有任何想法?

sql ruby-on-rails rake-task rails-sprockets
2个回答
0
投票

尝试将以下行添加到Sprockets环境配置块:

env.register_bundle_processor 'text/sql', Sprockets::Bundle

此外,您可以添加以下行以支持单行SQL注释:

env.register_preprocessor 'text/sql', Sprockets::DirectiveProcessor.new(comments: ['--', ['/*', '*/']])

-1
投票

简单地合并一堆文件的任务是不是Sprockets有点矫枉过正?

namespace :db do
  desc 'creates DB functions listed in db/functions.sql'
  task :functions => :environment do
    File.open(Rails.root.join('db','functions.sql'), 'w') do |dest|
      Dir[Rails.root.join('db', 'functions', '*.sql')].each do |f|
        File.copy_stream(f, dest)
      end
    end
    # ...
  end
end

如果您需要按特定顺序(管理依赖项),只需使用数组而不是Dir[Rails.root.join('db', 'functions', '*.sql')]

© www.soinside.com 2019 - 2024. All rights reserved.