Advertisement
Guest User

Untitled

a guest
Aug 13th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. # FABRICATION_SEQUENCE
  2. # /Users/larry1mbp/.rvm/gems/ruby-2.3.3@headmaster/gems/fabrication-2.15.2/lib/fabrication/generator/base.rb:100:in `persist'
  3. # /Users/larry1mbp/.rvm/gems/ruby-2.3.3@headmaster/gems/fabrication-2.15.2/lib/fabrication/generator/base.rb:25:in `create'
  4. # /Users/larry1mbp/.rvm/gems/ruby-2.3.3@headmaster/gems/fabrication-2.15.2/lib/fabrication/schematic/definition.rb:78:in `block in fabricate'
  5. # /Users/larry1mbp/.rvm/gems/ruby-2.3.3@headmaster/gems/fabrication-2.15.2/lib/fabrication/schematic/definition.rb:77:in `instance_eval'
  6. # /Users/larry1mbp/.rvm/gems/ruby-2.3.3@headmaster/gems/fabrication-2.15.2/lib/fabrication/schematic/definition.rb:77:in `fabricate'
  7. # /Users/larry1mbp/.rvm/gems/ruby-2.3.3@headmaster/gems/fabrication-2.15.2/lib/fabricate.rb:33:in `create'
  8. # /Users/larry1mbp/.rvm/gems/ruby-2.3.3@headmaster/gems/fabrication-2.15.2/lib/fabrication.rb:65:in `Fabricate'
  9.  
  10. # DATABASE_CLEANER_SEQUENCE
  11. # /Users/larry1mbp/.rvm/gems/ruby-2.3.3@headmaster/gems/database_cleaner-1.5.3/lib/database_cleaner/active_record/transaction.rb:12:in `start'
  12. # /Users/larry1mbp/.rvm/gems/ruby-2.3.3@headmaster/gems/database_cleaner-1.5.3/lib/database_cleaner/base.rb:88:in `start'
  13. # /Users/larry1mbp/.rvm/gems/ruby-2.3.3@headmaster/gems/database_cleaner-1.5.3/lib/database_cleaner/configuration.rb:75:in `block in start'
  14. # /Users/larry1mbp/.rvm/gems/ruby-2.3.3@headmaster/gems/database_cleaner-1.5.3/lib/database_cleaner/configuration.rb:75:in `each'
  15. # /Users/larry1mbp/.rvm/gems/ruby-2.3.3@headmaster/gems/database_cleaner-1.5.3/lib/database_cleaner/configuration.rb:75:in `start'
  16. # /Users/larry1mbp/605/headmaster-164/spec/support/database_cleaner_configuration.rb:22:in `block (2 levels) in <top (required)>'
  17.  
  18. if ENV['DEBUG_QUERIES']
  19.  
  20. BLACKLIST = %w(
  21. gems/actionpack
  22. gems/activemodel
  23. gems/activerecord
  24. gems/activesupport
  25. gems/capybara
  26. gems/delayed_job
  27. gems/grant
  28. gems/rack
  29. gems/rack-test
  30. gems/railties
  31. gems/rspec-rails
  32. gems/strongbolt
  33. headmaster/bin/
  34. headmaster/gems/rspec-core)
  35.  
  36. @sequences = {
  37. fabrication: {
  38. text: 'FABRICATION SEQUENCE',
  39. lines: [
  40. 'fabrication/generator/base.rb:100',
  41. 'fabrication/generator/base.rb:25',
  42. 'fabrication/schematic/definition.rb:78',
  43. "fabrication/schematic/definition.rb:77:in `instance_eval'",
  44. "fabrication/schematic/definition.rb:77:in `fabricate'",
  45. 'fabricate.rb:33',
  46. 'fabrication.rb:65'
  47. ]
  48. },
  49. database_cleaner: {
  50. text: 'DATABASE CLEANER SEQUENCE',
  51. lines: [ 'database_cleaner/active_record/transaction.rb:12',
  52. 'database_cleaner/base.rb:88',
  53. "database_cleaner/configuration.rb:75:in `block in start'",
  54. "database_cleaner/configuration.rb:75:in `each'",
  55. "database_cleaner/configuration.rb:75:in `start'",
  56. 'support/database_cleaner_configuration.rb:22'
  57. ]
  58. }
  59. }
  60.  
  61. @blacklist_re = []
  62. BLACKLIST.each do |token|
  63. @blacklist_re << Regexp.new(token)
  64. end
  65.  
  66. # seq is @sequences[:some_symbol] so it is a hash
  67. # @seq_flag is :fabrication or :database_cleaner or nil
  68. @seq_flag = nil
  69. seq = {}
  70. line_just_rec = 99
  71. printed_suppression_indicator = false
  72.  
  73. query_regex = /#{ENV['QUERY_TO_DEBUG']}/
  74.  
  75. ActiveSupport::Notifications.subscribe('sql.active_record') do |_, _, _, _, details|
  76. # details[:sql] is the SQL statement
  77. # details[:binds] are the values substituted in
  78. # caller is the backtrace
  79. if details[:sql] =~ query_regex
  80. puts '*' * 50
  81. puts details[:sql]
  82. puts details[:binds].map {|bind| bind[1]}.join(', ')
  83. caller.each do |line|
  84. unless not_wanted(line)
  85. printed_suppression_indicator = false
  86. if @seq_flag # if we are in a sequence
  87. if this_is_next_line(seq, line_just_rec, line)
  88. line_just_rec += 1
  89. if this_is_last_line(seq, line_just_rec)
  90. puts sequence_text(seq)
  91. set_seq_flag(seq, false)
  92. end
  93. else # not next line
  94. flush_lines(seq, line_just_rec)
  95. puts "#{line}\n" # puts line as is (we thought sequence but no)
  96. end
  97. else # not in sequence
  98. # detect if a sequence is beginning
  99. seq_sym = first_line_of_sequence(line)
  100. if seq_sym
  101. seq = @sequences[seq_sym]
  102. set_seq_flag(seq, true)
  103. line_just_rec = 0
  104. else
  105. puts "#{line}\n" # 'normal' line
  106. end
  107. end
  108. else # not wanted
  109. unless printed_suppression_indicator
  110. puts "...."
  111. printed_suppression_indicator = true
  112. end
  113. end
  114. end
  115. end
  116. end
  117.  
  118. def not_wanted(string)
  119. @blacklist_re.each do |re|
  120. if string =~ re
  121. return true
  122. end
  123. end
  124. false
  125. end
  126.  
  127. def set_seq_flag(seq, tf)
  128. if tf
  129. @seq_flag = seq.keys.first
  130. else
  131. @seq_flag = nil
  132. end
  133. end
  134.  
  135. def this_is_next_line(seq, line_just_rec, line)
  136. return line.include? seq[:lines][line_just_rec + 1]
  137. end
  138.  
  139. def this_is_last_line(seq, line_just_rec)
  140. return (seq[:lines].length == line_just_rec + 1)
  141. end
  142.  
  143. def flush_lines(seq, line_just_rec)
  144. puts "FLUSHING LINES"
  145. seq[:lines][0..line_just_rec].each do |line|
  146. puts "#{line}\n"
  147. end
  148. end
  149.  
  150. def sequence_text(seq)
  151. seq[:text]
  152. end
  153.  
  154. # returns which sequence it is the first line of
  155. # or false if it isn't one
  156. def first_line_of_sequence(line)
  157. @sequences.each do |k, v|
  158. if line.include? v[:lines][0]
  159. return k
  160. end
  161. end
  162. return false
  163. end
  164.  
  165. end # if ENV DEBUG_QUERIES
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement