基于this article,它描述了如何使用Sprockets编写小型SQL清单文件,我在每个rake db:migrate
上自动重新创建了我的SQL视图和函数。这非常有效,直到最后一次升级到Rails 5.1
突然,清单文件被编译,但是每个*= require
语句都被忽略,我最终得到一个空的清单文件。我已经为DirectiveProcessor
尝试了几种评论样式,有和没有文件扩展名,有和没有相对路径。无论我提供什么,我最终都会得到一个通过数据库执行的空文件。
分贝/功能/ 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
在执行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.
*
*
*/
所以文件被执行但看起来是空的......任何人都有任何想法?
答案 0 :(得分:0)
尝试将以下行添加到Sprockets环境配置块中:
env.register_bundle_processor 'text/sql', Sprockets::Bundle
此外,您可以添加以下行以支持单行SQL注释:
env.register_preprocessor 'text/sql', Sprockets::DirectiveProcessor.new(comments: ['--', ['/*', '*/']])
答案 1 :(得分:-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')]
。