2017-09-04 1 views
1

스프 로킷을 사용하여 작은 SQL 매니페스트 파일을 작성하는 방법을 설명하는 this article을 기반으로 모든 SQL 문의보기 및 기능은 모든 rake db:migrate에 자동으로 다시 작성됩니다. 이것은 Rails 5.1의 마지막 업그레이드까지 훌륭하게 작동했습니다Sprockets는 사용자 정의 레이크 작업을 위해 SQL 파일을 요구하지 않습니다.

갑자기 매니페스트 파일이 컴파일되지만 모든 *= require 문은 무시되고 빈 매니페스트 파일로 끝납니다. 필자는 DirectiveProcessor에 대해 파일 확장자의 유무에 관계없이 상대 경로가 있거나없는 몇 가지 주석 스타일을 시도했습니다. 내가 무엇을 제공하든 DB를 통해 실행되는 빈 파일로 끝납니다.

내 설치

는 dB/기능 /보고 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. 
* 


* 

*/ 

그래서 파일이 실행되지만 빈 ... 누구나 어떤 아이디어가 보이는됩니다?

답변

-1

파일 집합을 간단하게 병합하는 작업에 스프로킷이 조금 과잉 반응하지 않습니까? 특정 순서로이해야하는 경우

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')] 대신 배열을 사용 (종속성을 관리 할 수 ​​있습니다). 스프라켓 환경 구성 블록에 다음 줄을 추가

+0

원래 생각은하지 않았다 나에게서 왔어. 나는 단지 그것을 고치려고 노력하고있다. 오버 킬은 다소 복잡 할 수 있습니다. 그러나 현재의 모든 기능을 다시 작성합니다.이 두 가지 경우 이외의 다른 것도 있습니다.이 단계에서는 지나치게 과장됩니다 ... 그러나 다른 접근 방법 덕분에 도움이 될 것입니다. – Vapire

+0

필자는 실제로 서브 파일에 대한 매니페스트가있는 복잡한 종속성 트리가 필요한지를 추측합니다. 그러나 다른 한편으로는 스프 로킷은 의존성 분석을 잘하지 못하고 반복적으로 다른 파일을 요구할 것입니다. – max

0

시도 : 또한

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

, 당신은 한 줄 SQL 주석을 지원하기 위해 다음 줄을 추가 할 수 있습니다

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