sequelize-rails 0.2.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/.DS_Store +0 -0
  3. data/CHANGELOG.md +3 -2
  4. data/Gemfile.lock +105 -103
  5. data/README.md +203 -26
  6. data/lib/sequel/rails/generators/application_record/application_record_generator.rb +26 -0
  7. data/lib/sequel/rails/generators/migration/migration_generator.rb +23 -0
  8. data/lib/sequel/rails/generators/model/model_generator.rb +11 -0
  9. data/lib/sequel/rails/minitest.rb +24 -0
  10. data/lib/sequel/rails/railtie.rb +69 -0
  11. data/lib/sequel/rails/railties/controller_runtime.rb +48 -0
  12. data/lib/sequel/rails/railties/log_subscriber.rb +155 -0
  13. data/lib/sequel/rails/translation_support.rb +15 -0
  14. data/lib/sequel/rails/version.rb +7 -0
  15. data/lib/sequel/rails.rb +38 -0
  16. data/lib/{tasks.rake → sequel/tasks.rake} +6 -6
  17. data/lib/sequelize-rails.rb +1 -1
  18. data/lib/sequelize_rails.rb +1 -29
  19. data/sequelize-rails.gemspec +2 -2
  20. data/sig/sequelize_rails.rbs +5 -3
  21. metadata +18 -15
  22. data/lib/sequelize_rails/generators/application_record/application_record_generator.rb +0 -24
  23. data/lib/sequelize_rails/generators/migration/migration_generator.rb +0 -21
  24. data/lib/sequelize_rails/generators/model/model_generator.rb +0 -9
  25. data/lib/sequelize_rails/railtie.rb +0 -66
  26. data/lib/sequelize_rails/railties/controller_runtime.rb +0 -46
  27. data/lib/sequelize_rails/railties/log_subscriber.rb +0 -153
  28. data/lib/sequelize_rails/translation_support.rb +0 -13
  29. data/lib/sequelize_rails/version.rb +0 -5
  30. /data/lib/{sequelize_rails → sequel/rails}/db_console.rb +0 -0
  31. /data/lib/{sequelize_rails → sequel/rails}/generators/application_record/templates/application_record.rb.tt +0 -0
  32. /data/lib/{sequelize_rails → sequel/rails}/generators/migration/templates/migration.rb.erb +0 -0
@@ -1,46 +0,0 @@
1
- require "active_support/core_ext/module/attr_internal"
2
-
3
- module SequelizeRails
4
- module Railties
5
- module ControllerRuntime
6
- extend ActiveSupport::Concern
7
-
8
- protected
9
-
10
- attr_internal :db_runtime
11
-
12
- def process_action(action, *)
13
- # We also need to reset the runtime before each action
14
- # because of queries in middleware or in cases we are streaming
15
- # and it won't be cleaned up by the method below.
16
- ::SequelizeRails::Railties::LogSubscriber.reset_runtime
17
- ::SequelizeRails::Railties::LogSubscriber.reset_count
18
- super
19
- end
20
-
21
- def cleanup_view_runtime
22
- db_rt_before_render = ::SequelizeRails::Railties::LogSubscriber.reset_runtime
23
- self.db_runtime = (db_runtime || 0) + db_rt_before_render
24
- runtime = super
25
- db_rt_after_render = ::SequelizeRails::Railties::LogSubscriber.reset_runtime
26
- self.db_runtime += db_rt_after_render
27
- runtime - db_rt_after_render
28
- end
29
-
30
- def append_info_to_payload(payload)
31
- super
32
- payload[:db_runtime] = (db_runtime || 0) + ::SequelizeRails::Railties::LogSubscriber.reset_runtime
33
- payload[:db_query_count] = ::SequelizeRails::Railties::LogSubscriber.count
34
- end
35
-
36
- module ClassMethods
37
- def log_process_action(payload)
38
- messages = super
39
- messages << format("Database: %.1fms", payload[:db_runtime].to_f) if payload[:db_runtime]
40
- messages << format("Queries: %d", payload[:db_query_count].to_f) if payload[:db_query_count]
41
- messages
42
- end
43
- end
44
- end
45
- end
46
- end
@@ -1,153 +0,0 @@
1
- module SequelizeRails
2
- module Railties
3
- class LogSubscriber < ActiveSupport::LogSubscriber
4
- IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]
5
-
6
- class_attribute :backtrace_cleaner, default: ActiveSupport::BacktraceCleaner.new
7
-
8
- def self.runtime=(value)
9
- Thread.current["sequel_sql_runtime"] = value
10
- end
11
-
12
- def self.runtime
13
- Thread.current["sequel_sql_runtime"] ||= 0
14
- end
15
-
16
- def self.count=(value)
17
- Thread.current["sequel_sql_count"] = value
18
- end
19
-
20
- def self.count
21
- Thread.current["sequel_sql_count"] ||= 0
22
- end
23
-
24
- def self.reset_runtime
25
- previous = runtime
26
- self.runtime = 0
27
- previous
28
- end
29
-
30
- def self.reset_count
31
- previous = count
32
- self.count = 0
33
- previous
34
- end
35
-
36
- def sql(event)
37
- self.class.runtime += event.duration
38
- self.class.count += 1
39
- return unless logger.debug?
40
-
41
- payload = event.payload
42
-
43
- return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])
44
-
45
- name = format("%s (%.1fms)", payload[:name], event.duration)
46
- sql = payload[:sql].squeeze(" ")
47
- binds = nil
48
-
49
- if payload[:binds]&.any?
50
- casted_params = type_casted_binds(payload[:type_casted_binds])
51
-
52
- binds = []
53
- payload[:binds].each_with_index do |attr, i|
54
- attribute_name = if attr.respond_to?(:name)
55
- attr.name
56
- elsif attr.respond_to?(:[]) && attr[i].respond_to?(:name)
57
- attr[i].name
58
- end
59
-
60
- filtered_params = filter(attribute_name, casted_params[i])
61
-
62
- binds << render_bind(attr, filtered_params)
63
- end
64
- binds = binds.inspect
65
- binds.prepend(" ")
66
- end
67
-
68
- name = colorize_payload_name(name, payload[:name])
69
- sql = color(sql, sql_color(sql), true) if colorize_logging
70
-
71
- debug " #{name} #{sql}#{binds}"
72
- end
73
-
74
- private
75
-
76
- def type_casted_binds(casted_binds)
77
- casted_binds.respond_to?(:call) ? casted_binds.call : casted_binds
78
- end
79
-
80
- def render_bind(attr, value)
81
- case attr
82
- when ActiveModel::Attribute
83
- if attr.type.binary? && attr.value
84
- value = "<#{attr.value_for_database.to_s.bytesize} bytes of binary data>"
85
- end
86
- when Array
87
- attr = attr.first
88
- else
89
- attr = nil
90
- end
91
-
92
- [attr&.name, value]
93
- end
94
-
95
- def colorize_payload_name(name, payload_name)
96
- if payload_name.blank? || payload_name == "SQL" # SQL vs Model Load/Exists
97
- color(name, WHITE, true)
98
- else
99
- color(name, CYAN, true)
100
- end
101
- end
102
-
103
- def sql_color(sql)
104
- case sql
105
- when /\A\s*rollback/mi
106
- RED
107
- when /select .*for update/mi, /\A\s*lock/mi
108
- CYAN
109
- when /\A\s*select/i
110
- CYAN
111
- when /\A\s*insert/i
112
- GREEN
113
- when /\A\s*update/i
114
- YELLOW
115
- when /\A\s*delete/i
116
- RED
117
- when /transaction\s*\Z/i, /\A\s*begin/i, /\A\s*commit/i
118
- BLUE
119
- else
120
- MAGENTA
121
- end
122
- end
123
-
124
- def logger
125
- ::Rails.application.config.sequel.logger
126
- end
127
-
128
- def debug(progname = nil, &block)
129
- return unless super
130
-
131
- if Rails.application.config.sequel.verbose_query_logs
132
- log_query_source
133
- end
134
- end
135
-
136
- def log_query_source
137
- source = extract_query_source_location(caller)
138
-
139
- if source
140
- logger.debug(" ↳ #{source}")
141
- end
142
- end
143
-
144
- def extract_query_source_location(locations)
145
- backtrace_cleaner.clean(locations.lazy).first
146
- end
147
-
148
- def filter(name, value)
149
- ActiveRecord::Base.inspection_filter.filter_param(name, value)
150
- end
151
- end
152
- end
153
- end
@@ -1,13 +0,0 @@
1
- module SequelizeRails
2
- module TranslationSupport
3
- def i18n_scope
4
- :sequel
5
- end
6
-
7
- def lookup_ancestors
8
- # ActiveModel uses the name of ancestors. Exclude unnamed classes, like
9
- # those returned by Sequel::Model(...).
10
- super.reject { |x| x.name.nil? }
11
- end
12
- end
13
- end
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SequelizeRails
4
- VERSION = "0.2.1"
5
- end