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
@@ -0,0 +1,69 @@
1
+ require "sequel"
2
+ require "rails"
3
+
4
+ # Database configuration
5
+ require "active_record/connection_handling"
6
+ require "active_record/database_configurations"
7
+
8
+ # Railties
9
+ require "action_controller/railtie"
10
+ require "sequel/rails/railties/log_subscriber"
11
+ require "sequel/rails/railties/controller_runtime"
12
+
13
+ # Load instrumentation (before any database connections are established)
14
+ Sequel.extension :sequel_instrumentation
15
+
16
+ # Monkey patches
17
+ require "sequel/rails/db_console"
18
+
19
+ module Sequel
20
+ module Rails
21
+ class Railtie < ::Rails::Railtie
22
+ # Log subscriber
23
+ ::Sequel::Rails::Railties::LogSubscriber.attach_to :sequel
24
+ ::Sequel::Rails::Railties::LogSubscriber.backtrace_cleaner.add_filter { |line| line.gsub(::Rails.root.to_s + File::SEPARATOR, "") }
25
+
26
+ # Config initialization
27
+ config.app_generators.orm :sequelize_rails, migration: :sequel_migration
28
+ config.sequel = ActiveSupport::OrderedOptions.new
29
+
30
+ initializer "sequel.plugins" do
31
+ ::Sequel::Model.plugin :active_model
32
+ end
33
+
34
+ initializer "sequel.logger" do |app|
35
+ app.config.sequel.logger ||= ::Rails.logger
36
+ app.config.sequel.verbose_query_logs = ::Rails.env.development? if app.config.sequel.verbose_query_logs.nil?
37
+ end
38
+
39
+ # https://api.rubyonrails.org/classes/ActiveModel/Translation.html
40
+ initializer "sequel.i18n_support" do
41
+ ::Sequel::Model.send :extend, ::ActiveModel::Translation
42
+ ::Sequel::Model.send :extend, ::Sequel::Rails::TranslationSupport
43
+ end
44
+
45
+ initializer "sequel.pretty_print" do
46
+ ::Sequel::Model.plugin :pretty_print
47
+ end
48
+
49
+ initializer "sequel.configuration" do
50
+ ::Sequel::Rails.configurations = ActiveRecord::DatabaseConfigurations.new ::Rails.application.config.database_configuration
51
+ end
52
+
53
+ initializer "sequel.connection" do
54
+ in_rake = Rails.const_defined?("Rake") && Rake.application.top_level_tasks.length > 0
55
+ ::Sequel::Rails.connect_to :primary unless in_rake
56
+ end
57
+
58
+ # Expose database runtime to controller for logging.
59
+ initializer "sequel.log_runtime" do |_app|
60
+ require "sequel/rails/railties/controller_runtime"
61
+ ActionController::Base.send :include, ::Sequel::Rails::Railties::ControllerRuntime
62
+ end
63
+
64
+ rake_tasks do
65
+ load File.expand_path("../tasks.rake", __dir__)
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,48 @@
1
+ require "active_support/core_ext/module/attr_internal"
2
+
3
+ module Sequel
4
+ module Rails
5
+ module Railties
6
+ module ControllerRuntime
7
+ extend ActiveSupport::Concern
8
+
9
+ protected
10
+
11
+ attr_internal :db_runtime
12
+
13
+ def process_action(action, *)
14
+ # We also need to reset the runtime before each action
15
+ # because of queries in middleware or in cases we are streaming
16
+ # and it won't be cleaned up by the method below.
17
+ ::Sequel::Rails::Railties::LogSubscriber.reset_runtime
18
+ ::Sequel::Rails::Railties::LogSubscriber.reset_count
19
+ super
20
+ end
21
+
22
+ def cleanup_view_runtime
23
+ db_rt_before_render = ::Sequel::Rails::Railties::LogSubscriber.reset_runtime
24
+ self.db_runtime = (db_runtime || 0) + db_rt_before_render
25
+ runtime = super
26
+ db_rt_after_render = ::Sequel::Rails::Railties::LogSubscriber.reset_runtime
27
+ self.db_runtime += db_rt_after_render
28
+ runtime - db_rt_after_render
29
+ end
30
+
31
+ def append_info_to_payload(payload)
32
+ super
33
+ payload[:db_runtime] = (db_runtime || 0) + ::Sequel::Rails::Railties::LogSubscriber.reset_runtime
34
+ payload[:db_query_count] = ::Sequel::Rails::Railties::LogSubscriber.count
35
+ end
36
+
37
+ module ClassMethods
38
+ def log_process_action(payload)
39
+ messages = super
40
+ messages << format("Database: %.1fms", payload[:db_runtime].to_f) if payload[:db_runtime]
41
+ messages << format("Queries: %d", payload[:db_query_count].to_f) if payload[:db_query_count]
42
+ messages
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,155 @@
1
+ module Sequel
2
+ module Rails
3
+ module Railties
4
+ class LogSubscriber < ActiveSupport::LogSubscriber
5
+ IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]
6
+
7
+ class_attribute :backtrace_cleaner, default: ActiveSupport::BacktraceCleaner.new
8
+
9
+ def self.runtime=(value)
10
+ Thread.current["sequel_sql_runtime"] = value
11
+ end
12
+
13
+ def self.runtime
14
+ Thread.current["sequel_sql_runtime"] ||= 0
15
+ end
16
+
17
+ def self.count=(value)
18
+ Thread.current["sequel_sql_count"] = value
19
+ end
20
+
21
+ def self.count
22
+ Thread.current["sequel_sql_count"] ||= 0
23
+ end
24
+
25
+ def self.reset_runtime
26
+ previous = runtime
27
+ self.runtime = 0
28
+ previous
29
+ end
30
+
31
+ def self.reset_count
32
+ previous = count
33
+ self.count = 0
34
+ previous
35
+ end
36
+
37
+ def sql(event)
38
+ self.class.runtime += event.duration
39
+ self.class.count += 1
40
+ return unless logger.debug?
41
+
42
+ payload = event.payload
43
+
44
+ return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])
45
+
46
+ name = format("%s (%.1fms)", payload[:name], event.duration)
47
+ sql = payload[:sql].squeeze(" ")
48
+ binds = nil
49
+
50
+ if payload[:binds]&.any?
51
+ casted_params = type_casted_binds(payload[:type_casted_binds])
52
+
53
+ binds = []
54
+ payload[:binds].each_with_index do |attr, i|
55
+ attribute_name = if attr.respond_to?(:name)
56
+ attr.name
57
+ elsif attr.respond_to?(:[]) && attr[i].respond_to?(:name)
58
+ attr[i].name
59
+ end
60
+
61
+ filtered_params = filter(attribute_name, casted_params[i])
62
+
63
+ binds << render_bind(attr, filtered_params)
64
+ end
65
+ binds = binds.inspect
66
+ binds.prepend(" ")
67
+ end
68
+
69
+ name = colorize_payload_name(name, payload[:name])
70
+ sql = color(sql, sql_color(sql), true) if colorize_logging
71
+
72
+ debug " #{name} #{sql}#{binds}"
73
+ end
74
+
75
+ private
76
+
77
+ def type_casted_binds(casted_binds)
78
+ casted_binds.respond_to?(:call) ? casted_binds.call : casted_binds
79
+ end
80
+
81
+ def render_bind(attr, value)
82
+ case attr
83
+ when ActiveModel::Attribute
84
+ if attr.type.binary? && attr.value
85
+ value = "<#{attr.value_for_database.to_s.bytesize} bytes of binary data>"
86
+ end
87
+ when Array
88
+ attr = attr.first
89
+ else
90
+ attr = nil
91
+ end
92
+
93
+ [attr&.name, value]
94
+ end
95
+
96
+ def colorize_payload_name(name, payload_name)
97
+ if payload_name.blank? || payload_name == "SQL" # SQL vs Model Load/Exists
98
+ color(name, WHITE, true)
99
+ else
100
+ color(name, CYAN, true)
101
+ end
102
+ end
103
+
104
+ def sql_color(sql)
105
+ case sql
106
+ when /\A\s*rollback/mi
107
+ RED
108
+ when /select .*for update/mi, /\A\s*lock/mi
109
+ CYAN
110
+ when /\A\s*select/i
111
+ CYAN
112
+ when /\A\s*insert/i
113
+ GREEN
114
+ when /\A\s*update/i
115
+ YELLOW
116
+ when /\A\s*delete/i
117
+ RED
118
+ when /transaction\s*\Z/i, /\A\s*begin/i, /\A\s*commit/i
119
+ BLUE
120
+ else
121
+ MAGENTA
122
+ end
123
+ end
124
+
125
+ def logger
126
+ ::Rails.application.config.sequel.logger
127
+ end
128
+
129
+ def debug(progname = nil, &block)
130
+ return unless super
131
+
132
+ if ::Rails.application.config.sequel.verbose_query_logs
133
+ log_query_source
134
+ end
135
+ end
136
+
137
+ def log_query_source
138
+ source = extract_query_source_location(caller)
139
+
140
+ if source
141
+ logger.debug(" ↳ #{source}")
142
+ end
143
+ end
144
+
145
+ def extract_query_source_location(locations)
146
+ backtrace_cleaner.clean(locations.lazy).first
147
+ end
148
+
149
+ def filter(name, value)
150
+ ActiveRecord::Base.inspection_filter.filter_param(name, value)
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,15 @@
1
+ module Sequel
2
+ module Rails
3
+ module TranslationSupport
4
+ def i18n_scope
5
+ :sequel
6
+ end
7
+
8
+ def lookup_ancestors
9
+ # ActiveModel uses the name of ancestors. Exclude unnamed classes, like
10
+ # those returned by Sequel::Model(...).
11
+ super.reject { |x| x.name.nil? }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sequel
4
+ module Rails
5
+ VERSION = "0.4.0"
6
+ end
7
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sequel/rails/version"
4
+ require "sequel/rails/railtie"
5
+ require "sequel/rails/translation_support"
6
+
7
+ # Plugins
8
+ require "sequel/plugins/pretty_print"
9
+
10
+ # Generators
11
+ require "rails/generators"
12
+ require "sequel/rails/generators/migration/migration_generator"
13
+ require "sequel/rails/generators/model/model_generator"
14
+ require "sequel/rails/generators/application_record/application_record_generator"
15
+
16
+ # Minitest support
17
+ begin
18
+ gem "minitest"
19
+ require "sequel/rails/minitest"
20
+ rescue Gem::LoadError
21
+ end
22
+
23
+ module Sequel
24
+ module Rails
25
+ mattr_accessor :configurations
26
+
27
+ # Opens a database connection based on the given configuration name
28
+ def self.connect_to config_name, opts = {}
29
+ config = configurations.resolve(config_name).configuration_hash.dup
30
+ config[:adapter] = "sqlite" if config[:adapter] == "sqlite3"
31
+ config[:max_connections] ||= config.delete(:pool) if config[:pool]
32
+ config[:pool_timeout] ||= config.delete(:timeout) / 1000 if config[:timeout]
33
+ Dir.chdir ::Rails.root do
34
+ ::Sequel.connect config, opts
35
+ end
36
+ end
37
+ end
38
+ end
@@ -23,14 +23,14 @@ db_namespace = namespace ns do
23
23
  task check_protected_environments: :load_config do
24
24
  # Disconnect any potentially-existing Sequel database connections
25
25
  Sequel::DATABASES.each(&:disconnect)
26
- raise "Error: the #{Rails.env} environment is a protected environment." unless Rails.env.test? || Rails.env.development?
26
+ raise "Error: the #{::Rails.env} environment is a protected environment." unless ::Rails.env.test? || ::Rails.env.development?
27
27
  end
28
28
 
29
29
  task load_config: :environment do
30
30
  if ActiveRecord::Base.configurations.empty?
31
31
  # Changed: the active_record railtie may not be loaded, so we need to load the database configuration directly
32
32
  # ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration
33
- ActiveRecord::Base.configurations = Rails.application.config.database_configuration
33
+ ActiveRecord::Base.configurations = ::Rails.application.config.database_configuration
34
34
  # This line was also added
35
35
  ActiveRecord::Base.establish_connection
36
36
  end
@@ -46,7 +46,7 @@ db_namespace = namespace ns do
46
46
  ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
47
47
  desc "Create #{name} database for current environment"
48
48
  task name => :load_config do
49
- db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: name)
49
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: ::Rails.env, name: name)
50
50
  ActiveRecord::Tasks::DatabaseTasks.create(db_config)
51
51
  end
52
52
  end
@@ -65,7 +65,7 @@ db_namespace = namespace ns do
65
65
  ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
66
66
  desc "Drop #{name} database for current environment"
67
67
  task name => [:load_config, :check_protected_environments] do
68
- db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: name)
68
+ db_config = ActiveRecord::Base.configurations.configs_for(env_name: ::Rails.env, name: name)
69
69
  ActiveRecord::Tasks::DatabaseTasks.drop(db_config)
70
70
  end
71
71
  end
@@ -112,14 +112,14 @@ db_namespace = namespace ns do
112
112
  task connection: [:environment] do
113
113
  require "sequel/core"
114
114
  Sequel.extension :migration
115
- SequelizeRails.connect_to :primary
115
+ Sequel::Rails.connect_to :primary
116
116
  end
117
117
 
118
118
  # desc "Backs up the database from DATABASE_URL or config/database.yml for the current RAILS_ENV"
119
119
  # task dump: [] do
120
120
  # end
121
121
 
122
- desc "Migrate the database"
122
+ desc "Runs database migrations"
123
123
  task migrate: [:connection] do
124
124
  migrator.run
125
125
  end
@@ -1 +1 @@
1
- require "sequelize_rails"
1
+ require "sequel/rails"
@@ -1,29 +1 @@
1
- # frozen_string_literal: true
2
-
3
- require "sequelize_rails/version"
4
- require "sequelize_rails/railtie"
5
- require "sequelize_rails/translation_support"
6
-
7
- # Plugins
8
- require "sequel/plugins/pretty_print"
9
-
10
- # Generators
11
- require "rails/generators"
12
- require "sequelize_rails/generators/migration/migration_generator"
13
- require "sequelize_rails/generators/model/model_generator"
14
- require "sequelize_rails/generators/application_record/application_record_generator"
15
-
16
- module SequelizeRails
17
- mattr_accessor :configurations
18
-
19
- # Opens a database connection based on the given configuration name
20
- def self.connect_to config_name, opts = {}
21
- config = SequelizeRails.configurations.resolve(config_name).configuration_hash.dup
22
- config[:adapter] = "sqlite" if config[:adapter] == "sqlite3"
23
- config[:max_connections] ||= config.delete(:pool) if config[:pool]
24
- config[:pool_timeout] ||= config.delete(:timeout) / 1000 if config[:timeout]
25
- Dir.chdir Rails.root do
26
- Sequel.connect config, opts
27
- end
28
- end
29
- end
1
+ require "sequel/rails"
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "lib/sequelize_rails/version"
3
+ require_relative "lib/sequel/rails/version"
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "sequelize-rails"
7
- spec.version = ::SequelizeRails::VERSION
7
+ spec.version = ::Sequel::Rails::VERSION
8
8
  spec.authors = ["Kenaniah Cerny"]
9
9
  spec.email = ["kenaniah@gmail.com"]
10
10
 
@@ -1,4 +1,6 @@
1
- module SequelizeRails
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
1
+ module Sequel
2
+ module Rails
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
4
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequelize-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenaniah Cerny
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-06-18 00:00:00.000000000 Z
11
+ date: 2023-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -144,6 +144,7 @@ executables: []
144
144
  extensions: []
145
145
  extra_rdoc_files: []
146
146
  files:
147
+ - ".DS_Store"
147
148
  - ".standard.yml"
148
149
  - Appraisals
149
150
  - CHANGELOG.md
@@ -160,20 +161,22 @@ files:
160
161
  - gemfiles/rails_7.gemfile
161
162
  - lib/sequel/extensions/sequel_instrumentation.rb
162
163
  - lib/sequel/plugins/pretty_print.rb
164
+ - lib/sequel/rails.rb
165
+ - lib/sequel/rails/db_console.rb
166
+ - lib/sequel/rails/generators/application_record/application_record_generator.rb
167
+ - lib/sequel/rails/generators/application_record/templates/application_record.rb.tt
168
+ - lib/sequel/rails/generators/migration/migration_generator.rb
169
+ - lib/sequel/rails/generators/migration/templates/migration.rb.erb
170
+ - lib/sequel/rails/generators/model/model_generator.rb
171
+ - lib/sequel/rails/minitest.rb
172
+ - lib/sequel/rails/railtie.rb
173
+ - lib/sequel/rails/railties/controller_runtime.rb
174
+ - lib/sequel/rails/railties/log_subscriber.rb
175
+ - lib/sequel/rails/translation_support.rb
176
+ - lib/sequel/rails/version.rb
177
+ - lib/sequel/tasks.rake
163
178
  - lib/sequelize-rails.rb
164
179
  - lib/sequelize_rails.rb
165
- - lib/sequelize_rails/db_console.rb
166
- - lib/sequelize_rails/generators/application_record/application_record_generator.rb
167
- - lib/sequelize_rails/generators/application_record/templates/application_record.rb.tt
168
- - lib/sequelize_rails/generators/migration/migration_generator.rb
169
- - lib/sequelize_rails/generators/migration/templates/migration.rb.erb
170
- - lib/sequelize_rails/generators/model/model_generator.rb
171
- - lib/sequelize_rails/railtie.rb
172
- - lib/sequelize_rails/railties/controller_runtime.rb
173
- - lib/sequelize_rails/railties/log_subscriber.rb
174
- - lib/sequelize_rails/translation_support.rb
175
- - lib/sequelize_rails/version.rb
176
- - lib/tasks.rake
177
180
  - sequelize-rails.gemspec
178
181
  - sig/sequelize_rails.rbs
179
182
  homepage: https://github.com/kenaniah/sequelize-rails
@@ -199,7 +202,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
199
202
  - !ruby/object:Gem::Version
200
203
  version: '0'
201
204
  requirements: []
202
- rubygems_version: 3.2.33
205
+ rubygems_version: 3.4.1
203
206
  signing_key:
204
207
  specification_version: 4
205
208
  summary: A gem for using Sequel with Rails natively.
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SequelizeRails
4
- module Generators
5
- class ApplicationRecordGenerator < ::Rails::Generators::Base
6
- source_root File.expand_path("templates", __dir__)
7
-
8
- def create_application_record
9
- template "application_record.rb", application_record_file_name
10
- end
11
-
12
- private
13
-
14
- def application_record_file_name
15
- @application_record_file_name ||=
16
- if namespaced?
17
- "app/models/#{namespaced_path}/application_record.rb"
18
- else
19
- "app/models/application_record.rb"
20
- end
21
- end
22
- end
23
- end
24
- end
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SequelizeRails
4
- module Generators
5
- class MigrationGenerator < ::Rails::Generators::NamedBase
6
- include Rails::Generators::Migration
7
-
8
- source_root File.expand_path("templates", __dir__)
9
-
10
- argument :attributes, type: :array, default: [], banner: "field[:type][:index] field[:type][:index]"
11
-
12
- def self.next_migration_number _dirname
13
- Time.now.strftime "%Y%m%d%H%M%S"
14
- end
15
-
16
- def create_migration_file
17
- migration_template "migration.rb.erb", "db/migrate/#{file_name}.rb"
18
- end
19
- end
20
- end
21
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SequelizeRails
4
- module Generators
5
- class ModelGenerator < ::Rails::Generators::NamedBase
6
- source_root File.expand_path("templates", __dir__)
7
- end
8
- end
9
- end
@@ -1,66 +0,0 @@
1
- require "sequel"
2
- require "rails"
3
-
4
- # Database configuration
5
- require "active_record/connection_handling"
6
- require "active_record/database_configurations"
7
-
8
- # Railties
9
- require "action_controller/railtie"
10
- require "sequelize_rails/railties/log_subscriber"
11
- require "sequelize_rails/railties/controller_runtime"
12
-
13
- # Load instrumentation (before any database connections are established)
14
- Sequel.extension :sequel_instrumentation
15
-
16
- # Monkey patches
17
- require "sequelize_rails/db_console"
18
-
19
- module SequelizeRails
20
- class Railtie < Rails::Railtie
21
- # Log subscriber
22
- ::SequelizeRails::Railties::LogSubscriber.attach_to :sequel
23
- ::SequelizeRails::Railties::LogSubscriber.backtrace_cleaner.add_filter { |line| line.gsub(Rails.root.to_s + File::SEPARATOR, "") }
24
-
25
- # Config initialization
26
- config.app_generators.orm :sequelize_rails, migration: :sequel_migration
27
- config.sequel = ActiveSupport::OrderedOptions.new
28
-
29
- initializer "sequel.plugins" do
30
- ::Sequel::Model.plugin :active_model
31
- end
32
-
33
- initializer "sequel.logger" do |app|
34
- app.config.sequel.logger ||= ::Rails.logger
35
- app.config.sequel.verbose_query_logs = ::Rails.env.development? if app.config.sequel.verbose_query_logs.nil?
36
- end
37
-
38
- # https://api.rubyonrails.org/classes/ActiveModel/Translation.html
39
- initializer "sequel.i18n_support" do
40
- ::Sequel::Model.send :extend, ::ActiveModel::Translation
41
- ::Sequel::Model.send :extend, ::SequelizeRails::TranslationSupport
42
- end
43
-
44
- initializer "sequel.pretty_print" do
45
- ::Sequel::Model.plugin :pretty_print
46
- end
47
-
48
- initializer "sequel.configuration" do
49
- SequelizeRails.configurations = ActiveRecord::DatabaseConfigurations.new Rails.application.config.database_configuration
50
- end
51
-
52
- initializer "sequel.connection" do
53
- SequelizeRails.connect_to :primary unless ARGV.any? { |c| c.starts_with? "db:" }
54
- end
55
-
56
- # Expose database runtime to controller for logging.
57
- initializer "sequel.log_runtime" do |_app|
58
- require "sequelize_rails/railties/controller_runtime"
59
- ActionController::Base.send :include, SequelizeRails::Railties::ControllerRuntime
60
- end
61
-
62
- rake_tasks do
63
- load File.expand_path("../tasks.rake", __dir__)
64
- end
65
- end
66
- end