rom-sql 3.6.2 → 4.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -18
  3. data/LICENSE +1 -1
  4. data/README.md +1 -1
  5. data/lib/rom/plugins/relation/sql/auto_restrictions.rb +11 -10
  6. data/lib/rom/plugins/relation/sql/default_views.rb +75 -0
  7. data/lib/rom/plugins/relation/sql/instrumentation.rb +11 -10
  8. data/lib/rom/plugins/relation/sql/postgres/explain.rb +4 -4
  9. data/lib/rom/plugins/relation/sql/postgres/full_text_search.rb +18 -16
  10. data/lib/rom/plugins/relation/sql/postgres/streaming.rb +12 -9
  11. data/lib/rom/sql/associations/many_to_many.rb +7 -7
  12. data/lib/rom/sql/associations/many_to_one.rb +3 -3
  13. data/lib/rom/sql/associations/one_to_many.rb +3 -3
  14. data/lib/rom/sql/associations/one_to_one.rb +1 -1
  15. data/lib/rom/sql/associations/one_to_one_through.rb +1 -1
  16. data/lib/rom/sql/associations/self_ref.rb +1 -1
  17. data/lib/rom/sql/associations.rb +5 -5
  18. data/lib/rom/sql/attribute.rb +9 -9
  19. data/lib/rom/sql/attribute_aliasing.rb +2 -3
  20. data/lib/rom/sql/commands/create.rb +5 -4
  21. data/lib/rom/sql/commands/delete.rb +2 -2
  22. data/lib/rom/sql/commands/update.rb +5 -4
  23. data/lib/rom/sql/commands.rb +4 -4
  24. data/lib/rom/sql/dsl.rb +3 -3
  25. data/lib/rom/sql/errors.rb +3 -3
  26. data/lib/rom/sql/extensions/active_support_notifications.rb +4 -4
  27. data/lib/rom/sql/extensions/mysql/type_builder.rb +5 -5
  28. data/lib/rom/sql/extensions/mysql.rb +1 -1
  29. data/lib/rom/sql/extensions/postgres/commands.rb +13 -31
  30. data/lib/rom/sql/extensions/postgres/type_builder.rb +28 -31
  31. data/lib/rom/sql/extensions/postgres/type_serializer.rb +24 -25
  32. data/lib/rom/sql/extensions/postgres/types/array.rb +4 -4
  33. data/lib/rom/sql/extensions/postgres/types/array_types.rb +1 -1
  34. data/lib/rom/sql/extensions/postgres/types/geometric.rb +27 -27
  35. data/lib/rom/sql/extensions/postgres/types/json.rb +9 -9
  36. data/lib/rom/sql/extensions/postgres/types/ltree.rb +61 -34
  37. data/lib/rom/sql/extensions/postgres/types/network.rb +4 -17
  38. data/lib/rom/sql/extensions/postgres/types/range.rb +25 -25
  39. data/lib/rom/sql/extensions/postgres/types.rb +14 -14
  40. data/lib/rom/sql/extensions/postgres.rb +6 -6
  41. data/lib/rom/sql/extensions/rails_log_subscriber.rb +3 -3
  42. data/lib/rom/sql/extensions/sqlite/types.rb +1 -1
  43. data/lib/rom/sql/extensions/sqlite.rb +2 -2
  44. data/lib/rom/sql/extensions.rb +6 -6
  45. data/lib/rom/sql/foreign_key.rb +3 -1
  46. data/lib/rom/sql/function.rb +18 -22
  47. data/lib/rom/sql/gateway.rb +44 -17
  48. data/lib/rom/sql/group_dsl.rb +1 -1
  49. data/lib/rom/sql/index.rb +2 -0
  50. data/lib/rom/sql/join_dsl.rb +1 -1
  51. data/lib/rom/sql/mapper_compiler.rb +2 -2
  52. data/lib/rom/sql/migration/migrator.rb +11 -11
  53. data/lib/rom/sql/migration/runner.rb +3 -3
  54. data/lib/rom/sql/migration/schema_diff.rb +7 -8
  55. data/lib/rom/sql/migration/writer.rb +12 -12
  56. data/lib/rom/sql/migration.rb +4 -8
  57. data/lib/rom/sql/order_dsl.rb +1 -1
  58. data/lib/rom/sql/plugin/associates.rb +49 -11
  59. data/lib/rom/sql/plugin/pagination.rb +5 -3
  60. data/lib/rom/sql/plugin/schema_indexes.rb +35 -0
  61. data/lib/rom/sql/plugins.rb +9 -6
  62. data/lib/rom/sql/projection_dsl.rb +2 -2
  63. data/lib/rom/sql/rake_task.rb +2 -2
  64. data/lib/rom/sql/relation/reading.rb +34 -27
  65. data/lib/rom/sql/relation/writing.rb +11 -10
  66. data/lib/rom/sql/relation.rb +57 -89
  67. data/lib/rom/sql/restriction_dsl.rb +1 -1
  68. data/lib/rom/sql/schema/attributes_inferrer.rb +3 -3
  69. data/lib/rom/sql/schema/dsl.rb +4 -2
  70. data/lib/rom/sql/schema/index_dsl.rb +3 -5
  71. data/lib/rom/sql/schema/inferrer.rb +25 -23
  72. data/lib/rom/sql/schema/type_builder.rb +2 -2
  73. data/lib/rom/sql/schema.rb +9 -21
  74. data/lib/rom/sql/spec/support.rb +5 -5
  75. data/lib/rom/sql/tasks/migration_tasks.rake +13 -21
  76. data/lib/rom/sql/transaction.rb +4 -2
  77. data/lib/rom/sql/type_extensions.rb +3 -1
  78. data/lib/rom/sql/type_serializer.rb +10 -10
  79. data/lib/rom/sql/types.rb +4 -4
  80. data/lib/rom/sql/version.rb +1 -1
  81. data/lib/rom/sql/wrap.rb +1 -1
  82. data/lib/rom/sql.rb +13 -12
  83. data/lib/rom/types/values.rb +2 -2
  84. data/lib/rom-sql.rb +1 -1
  85. metadata +13 -17
@@ -1,15 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'logger'
4
- require 'sequel/core'
3
+ require "logger"
4
+ require "sequel/core"
5
5
 
6
- require 'dry/core/constants'
6
+ require "dry/core/constants"
7
7
 
8
- require 'rom/types'
9
- require 'rom/gateway'
10
- require 'rom/sql/migration'
11
- require 'rom/sql/commands'
12
- require 'rom/sql/transaction'
8
+ require "rom/setup"
9
+ require "rom/types"
10
+ require "rom/gateway"
11
+ require "rom/sql/migration"
12
+ require "rom/sql/commands"
13
+ require "rom/sql/transaction"
14
+ require "rom/support/notifications"
13
15
 
14
16
  module ROM
15
17
  module SQL
@@ -17,6 +19,8 @@ module ROM
17
19
  #
18
20
  # @api public
19
21
  class Gateway < ROM::Gateway
22
+ extend Notifications
23
+
20
24
  include Dry::Core::Constants
21
25
  include Migration
22
26
 
@@ -81,13 +85,18 @@ module ROM
81
85
  def initialize(uri, options = EMPTY_HASH)
82
86
  @connection = connect(uri, options)
83
87
  load_extensions(Array(options[:extensions]))
84
- Notifications.trigger("configuration.gateway.connected", connection: @connection)
88
+ notifications.trigger("sql.gateway.connected", connection: @connection)
85
89
 
86
90
  @options = options
87
91
 
88
92
  super
89
93
  end
90
94
 
95
+ # @api private
96
+ def notifications
97
+ @notifications ||= Notifications.event_bus(:sql)
98
+ end
99
+
91
100
  # Disconnect from the gateway's database
92
101
  #
93
102
  # @api public
@@ -152,15 +161,15 @@ module ROM
152
161
  # Create a table using the configured connection
153
162
  #
154
163
  # @api public
155
- def create_table(*args, &block)
156
- connection.create_table(*args, &block)
164
+ def create_table(...)
165
+ connection.create_table(...)
157
166
  end
158
167
 
159
168
  # Drops a table
160
169
  #
161
170
  # @api public
162
- def drop_table(*args, &block)
163
- connection.drop_table(*args, &block)
171
+ def drop_table(...)
172
+ connection.drop_table(...)
164
173
  end
165
174
 
166
175
  # Returns a list of datasets inferred from table names
@@ -205,6 +214,26 @@ module ROM
205
214
  connection.run(statement)
206
215
  end
207
216
 
217
+ # Build an SQL-specific command
218
+ #
219
+ # @return [Command]
220
+ #
221
+ # @api public
222
+ def command(klass, relation:, **opts)
223
+ return super unless relation.dataset.db.database_type == :postgres
224
+
225
+ ext =
226
+ if klass < Commands::Create
227
+ Postgres::Commands::Create
228
+ elsif klass < Commands::Update
229
+ Postgres::Commands::Update
230
+ end
231
+
232
+ klass.include(ext) if ext
233
+
234
+ super
235
+ end
236
+
208
237
  private
209
238
 
210
239
  # Connect to database or reuse established connection instance
@@ -236,18 +265,16 @@ module ROM
236
265
 
237
266
  # this will be default in Sequel 5.0.0 and since we don't rely
238
267
  # on dataset mutation it is safe to enable it already
239
- connection.extension(:freeze_datasets) unless RUBY_ENGINE == 'rbx'
268
+ connection.extension(:freeze_datasets) unless RUBY_ENGINE == "rbx"
240
269
 
241
270
  # for ROM::SQL::Relation#nullify
242
271
  connection.extension(:null_dataset)
243
272
  end
244
273
 
245
274
  # @api private
246
- def transaction_runner(_)
275
+ def transaction_runner(**)
247
276
  ROM::SQL::Transaction.new(connection)
248
277
  end
249
278
  end
250
279
  end
251
-
252
- Configuration.register_event("configuration.gateway.connected")
253
280
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rom/sql/dsl'
3
+ require "rom/sql/dsl"
4
4
 
5
5
  module ROM
6
6
  module SQL
data/lib/rom/sql/index.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "dry/core/equalizer"
4
+
3
5
  module ROM
4
6
  module SQL
5
7
  # @api private
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rom/sql/restriction_dsl'
3
+ require "rom/sql/restriction_dsl"
4
4
 
5
5
  module ROM
6
6
  module SQL
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rom/mapper_compiler'
3
+ require "rom/mapper_compiler"
4
4
 
5
5
  module ROM
6
6
  module SQL
@@ -9,7 +9,7 @@ module ROM
9
9
  name, _, meta_options = node
10
10
 
11
11
  if meta_options[:wrapped]
12
- [extract_wrapped_name(node), from: meta_options[:alias]]
12
+ [extract_wrapped_name(node), {from: meta_options[:alias]}]
13
13
  else
14
14
  [name]
15
15
  end
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'pathname'
3
+ require "pathname"
4
4
 
5
- require 'rom/types'
6
- require 'rom/initializer'
5
+ require "rom/types"
6
+ require "rom/initializer"
7
7
 
8
- require 'rom/sql/migration/runner'
9
- require 'rom/sql/migration/inline_runner'
10
- require 'rom/sql/migration/writer'
8
+ require "rom/sql/migration/runner"
9
+ require "rom/sql/migration/inline_runner"
10
+ require "rom/sql/migration/writer"
11
11
 
12
12
  module ROM
13
13
  module SQL
@@ -16,8 +16,8 @@ module ROM
16
16
  class Migrator
17
17
  extend Initializer
18
18
 
19
- DEFAULT_PATH = 'db/migrate'.freeze
20
- VERSION_FORMAT = '%Y%m%d%H%M%S'.freeze
19
+ DEFAULT_PATH = "db/migrate"
20
+ VERSION_FORMAT = "%Y%m%d%H%M%S"
21
21
  DEFAULT_INFERRER = Schema::Inferrer.new.suppress_errors.freeze
22
22
 
23
23
  param :connection
@@ -43,8 +43,8 @@ module ROM
43
43
 
44
44
  # @api private
45
45
  def create_file(name, version = generate_version, **options)
46
- sequence = options[:sequence] ? '%03d' % options[:sequence] : nil
47
- filename = "#{ version }#{ sequence }_#{ name }.rb"
46
+ sequence = options[:sequence] ? "%03d" % options[:sequence] : nil
47
+ filename = "#{version}#{sequence}_#{name}.rb"
48
48
  content = options[:content] || migration_file_content
49
49
  path = options[:path] || self.path
50
50
  dirname = Pathname(path)
@@ -63,7 +63,7 @@ module ROM
63
63
 
64
64
  # @api private
65
65
  def migration_file_content
66
- File.read(Pathname(__FILE__).dirname.join('template.rb').realpath)
66
+ File.read(Pathname(__FILE__).dirname.join("template.rb").realpath)
67
67
  end
68
68
 
69
69
  # @api private
@@ -54,6 +54,7 @@ module ROM
54
54
  end
55
55
  end
56
56
 
57
+ # rubocop:disable Metrics/AbcSize
57
58
  def alter_table(diff)
58
59
  return if diff.meta?
59
60
 
@@ -68,9 +69,7 @@ module ROM
68
69
  when SchemaDiff::AttributeChanged
69
70
  if attribute.type_changed?
70
71
  from, to = attribute.current.unwrap, attribute.target.unwrap
71
- raise UnsupportedConversion.new(
72
- "Don't know how to convert #{from.inspect} to #{to.inspect}"
73
- )
72
+ raise UnsupportedConversion, "Don't know how to convert #{from.inspect} to #{to.inspect}"
74
73
  end
75
74
 
76
75
  if attribute.nullability_changed?
@@ -94,6 +93,7 @@ module ROM
94
93
  end
95
94
  end
96
95
  end
96
+ # rubocop:enable Metrics/AbcSize
97
97
 
98
98
  def alter_foreign_keys(diff, foreign_key_changes)
99
99
  return if foreign_key_changes.empty?
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rom/sql/type_serializer'
3
+ require "rom/sql/type_serializer"
4
4
 
5
5
  module ROM
6
6
  module SQL
@@ -46,7 +46,6 @@ module ROM
46
46
  end
47
47
 
48
48
  class TableAltered < TableDiff
49
-
50
49
  option :attribute_changes, default: -> { EMPTY_ARRAY }
51
50
 
52
51
  option :index_changes, default: -> { EMPTY_ARRAY }
@@ -135,10 +134,10 @@ module ROM
135
134
  class IndexAdded < IndexDiff
136
135
  def options
137
136
  options = {}
138
- options[:name] = index.name if !index.name.nil?
137
+ options[:name] = index.name unless index.name.nil?
139
138
  options[:unique] = true if index.unique?
140
- options[:type] = index.type if !index.type.nil?
141
- options[:where] = index.predicate if !index.predicate.nil?
139
+ options[:type] = index.type unless index.type.nil?
140
+ options[:where] = index.predicate unless index.predicate.nil?
142
141
  options
143
142
  end
144
143
  end
@@ -146,7 +145,7 @@ module ROM
146
145
  class IndexRemoved < IndexDiff
147
146
  def options
148
147
  options = {}
149
- options[:name] = index.name if !index.name.nil?
148
+ options[:name] = index.name unless index.name.nil?
150
149
  options
151
150
  end
152
151
  end
@@ -210,8 +209,8 @@ module ROM
210
209
  }.map { |name, target_attr|
211
210
  [name, [target_attr, current[name]]]
212
211
  }.to_h
213
- added_attributes = target.select { |name, _| !current.key?(name) }
214
- removed_attributes = current.select { |name, _| !target.key?(name) }
212
+ added_attributes = target.reject { |name, _| current.key?(name) }
213
+ removed_attributes = current.reject { |name, _| target.key?(name) }
215
214
 
216
215
  map_attributes(removed_attributes, AttributeRemoved) +
217
216
  map_attributes(added_attributes, AttributeAdded) +
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rom/sql/migration/recorder'
3
+ require "rom/sql/migration/recorder"
4
4
 
5
5
  module ROM
6
6
  module SQL
7
7
  module Migration
8
8
  # @api private
9
9
  class Writer
10
- MIGRATION_BEGIN = "ROM::SQL.migration do\n change do".freeze
11
- MIGRATION_END = "\n end\nend\n".freeze
10
+ MIGRATION_BEGIN = "ROM::SQL.migration do\n change do"
11
+ MIGRATION_END = "\n end\nend\n"
12
12
 
13
13
  attr_reader :yield_migration
14
14
 
@@ -33,14 +33,14 @@ module ROM
33
33
  def write(operations, buffer, indent)
34
34
  operations.each do |operation|
35
35
  op, args, nested = operation
36
- buffer << indent << op.to_s << ' '
36
+ buffer << indent << op.to_s << " "
37
37
  write_arguments(buffer, args)
38
38
 
39
- if !nested.empty?
40
- buffer << ' do'
41
- write(nested, buffer, indent + ' ')
42
- buffer << indent << 'end'
43
- end
39
+ next if nested.empty?
40
+
41
+ buffer << " do"
42
+ write(nested, buffer, indent + " ")
43
+ buffer << indent << "end"
44
44
  end
45
45
  end
46
46
 
@@ -51,9 +51,9 @@ module ROM
51
51
  options = EMPTY_HASH
52
52
  end
53
53
 
54
- buffer << args.map(&:inspect).join(', ')
54
+ buffer << args.map(&:inspect).join(", ")
55
55
  options.each do |key, value|
56
- buffer << ', ' << key.to_s << ': ' << value.inspect
56
+ buffer << ", " << key.to_s << ": " << value.inspect
57
57
  end
58
58
  end
59
59
 
@@ -61,7 +61,7 @@ module ROM
61
61
  create_or_alter, args = op
62
62
  table_name = args[0]
63
63
 
64
- "#{create_or_alter.to_s.sub('_table', '')}_#{table_name}"
64
+ "#{create_or_alter.to_s.sub("_table", "")}_#{table_name}"
65
65
  end
66
66
  end
67
67
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rom/sql/migration/migrator'
4
- require 'rom/sql/migration/schema_diff'
3
+ require "rom/sql/migration/migrator"
4
+ require "rom/sql/migration/schema_diff"
5
5
 
6
6
  module ROM
7
7
  module SQL
@@ -141,12 +141,8 @@ module ROM
141
141
  end
142
142
 
143
143
  # @api public
144
- def auto_migrate!(conf, options = EMPTY_HASH)
145
- schemas = conf.relation_classes(self).map do |klass|
146
- klass.schema_proc.call.finalize_attributes!(gateway: self)
147
- end
148
-
149
- migrator.auto_migrate!(self, schemas, options)
144
+ def auto_migrate!(setup, options = EMPTY_HASH)
145
+ migrator.auto_migrate!(self, setup.registry.relations.map(&:schema), options)
150
146
  end
151
147
 
152
148
  private
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rom/sql/dsl'
3
+ require "rom/sql/dsl"
4
4
 
5
5
  module ROM
6
6
  module SQL
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rom/sql/associations'
3
+ require "rom/sql/associations"
4
4
 
5
5
  module ROM
6
6
  module SQL
@@ -16,7 +16,7 @@ module ROM
16
16
  def initialize(name, relation, opts)
17
17
  @name = name
18
18
  @assoc = relation.associations[name]
19
- @opts = { assoc: assoc, keys: assoc.join_keys }
19
+ @opts = {assoc: assoc, keys: assoc.join_keys}
20
20
  @opts.update(parent: opts[:parent]) if opts[:parent]
21
21
  end
22
22
 
@@ -25,7 +25,7 @@ module ROM
25
25
  end
26
26
 
27
27
  def to_hash
28
- { associate: opts }
28
+ {associate: opts}
29
29
  end
30
30
  end
31
31
 
@@ -35,11 +35,10 @@ module ROM
35
35
  extend ClassMethods
36
36
  include InstanceMethods
37
37
 
38
- defines :associations
39
-
40
- associations Hash.new
38
+ setting :associations, default: {}, reader: true
41
39
 
42
40
  option :associations, default: -> { self.class.associations }
41
+
43
42
  option :configured_associations, default: -> { EMPTY_ARRAY }
44
43
  end
45
44
  super
@@ -47,6 +46,39 @@ module ROM
47
46
 
48
47
  # @api public
49
48
  module ClassMethods
49
+ # @api private
50
+ def create_class(relation:, rel_meta: {}, parent_relation: nil, **, &block)
51
+ klass = super
52
+
53
+ if relation && rel_meta[:combine_type]
54
+ setup_associates(klass, relation, parent_relation)
55
+ end
56
+
57
+ klass
58
+ end
59
+
60
+ # Sets up `associates` plugin for a given command class and relation
61
+ #
62
+ # @param [Class] klass The command class
63
+ # @param [Relation] relation The relation for the command
64
+ #
65
+ # @api private
66
+ def setup_associates(klass, relation, parent_relation)
67
+ assoc_name =
68
+ if relation.associations.key?(parent_relation)
69
+ parent_relation
70
+ else
71
+ singular_name = relation.inflector.singularize(parent_relation).to_sym
72
+ singular_name if relation.associations.key?(singular_name)
73
+ end
74
+
75
+ if assoc_name
76
+ klass.associates(assoc_name)
77
+ else
78
+ klass.associates(parent_relation)
79
+ end
80
+ end
81
+
50
82
  # @see ROM::Command::ClassInterface.build
51
83
  #
52
84
  # @api public
@@ -57,16 +89,17 @@ module ROM
57
89
 
58
90
  associate_options = command.associations.map { |(name, opts)|
59
91
  next if configured_assocs.include?(name)
92
+
60
93
  AssociateOptions.new(name, relation, opts)
61
94
  }.compact
62
95
 
63
96
  before_hooks = associate_options.reject(&:after?).map(&:to_hash)
64
97
  after_hooks = associate_options.select(&:after?).map(&:to_hash)
65
98
 
66
- command.
67
- with(configured_associations: configured_assocs + associate_options.map(&:name)).
68
- before(*before_hooks).
69
- after(*after_hooks)
99
+ command
100
+ .with(configured_associations: configured_assocs + associate_options.map(&:name))
101
+ .before(*before_hooks)
102
+ .after(*after_hooks)
70
103
  end
71
104
 
72
105
  # Set command to associate tuples with a parent tuple using provided keys
@@ -96,7 +129,9 @@ module ROM
96
129
  "#{name} association is already defined for #{self.class}"
97
130
  end
98
131
 
99
- associations(associations.merge(name => options))
132
+ associations[name] = options
133
+
134
+ self
100
135
  end
101
136
  end
102
137
 
@@ -109,6 +144,8 @@ module ROM
109
144
  # @return [Array<Hash>]
110
145
  #
111
146
  # @api public
147
+ #
148
+ # rubocop:disable Lint/UnusedMethodArgument
112
149
  def associate(tuples, curried_parent = nil, assoc:, keys:, parent: curried_parent)
113
150
  result_type = result
114
151
 
@@ -137,6 +174,7 @@ module ROM
137
174
 
138
175
  result_type == :one ? output_tuples[0] : output_tuples
139
176
  end
177
+ # rubocop:enable Lint/UnusedMethodArgument
140
178
 
141
179
  # Return a new command with the provided association
142
180
  #
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rom/initializer'
3
+ require "dry/core/equalizer"
4
+
5
+ require "rom/initializer"
4
6
 
5
7
  module ROM
6
8
  module SQL
@@ -53,7 +55,7 @@ module ROM
53
55
  # @api public
54
56
  def prev_page
55
57
  num = current_page - 1
56
- num if num > 0
58
+ num if num.positive?
57
59
  end
58
60
 
59
61
  # Return total number of tuples
@@ -100,7 +102,7 @@ module ROM
100
102
  per_page = per_page.to_i
101
103
 
102
104
  self.class.new(
103
- dataset.offset((current_page-1)*per_page).limit(per_page),
105
+ dataset.offset((current_page - 1) * per_page).limit(per_page),
104
106
  current_page: current_page, per_page: per_page
105
107
  )
106
108
  end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/effects"
4
+ require "rom/sql/schema/index_dsl"
5
+
6
+ module ROM
7
+ module SQL
8
+ module Plugin
9
+ module SchemaIndexes
10
+ # @api public
11
+ module DSL
12
+ # Define indexes within a block
13
+ #
14
+ # @api public
15
+ def indexes(&block)
16
+ index_dsl.instance_eval(&block)
17
+ end
18
+
19
+ # @api private
20
+ def configure
21
+ super
22
+ config.options.update(indexes: index_dsl.(config.id, attributes.values))
23
+ end
24
+
25
+ private
26
+
27
+ # @api private
28
+ def index_dsl
29
+ @index_dsl ||= Schema::IndexDSL.new(attr_class: config.attr_class)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,16 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rom/plugins/relation/sql/instrumentation'
4
- require 'rom/plugins/relation/sql/auto_restrictions'
3
+ require "rom/plugins/relation/sql/instrumentation"
4
+ require "rom/plugins/relation/sql/default_views"
5
+ require "rom/plugins/relation/sql/auto_restrictions"
5
6
 
6
- require 'rom/sql/plugin/associates'
7
- require 'rom/sql/plugin/nullify'
8
- require 'rom/sql/plugin/pagination'
7
+ require "rom/sql/plugin/associates"
8
+ require "rom/sql/plugin/nullify"
9
+ require "rom/sql/plugin/pagination"
10
+ require "rom/sql/plugin/schema_indexes"
9
11
 
10
12
  ROM.plugins do
11
- adapter :sql do
13
+ adapter(:sql) do
12
14
  register :nullify, ROM::SQL::Plugin::Nullify, type: :relation
13
15
  register :pagination, ROM::SQL::Plugin::Pagination, type: :relation
14
16
  register :associates, ROM::SQL::Plugin::Associates, type: :command
17
+ register :indexes, ROM::SQL::Plugin::SchemaIndexes, type: :schema
15
18
  end
16
19
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rom/sql/dsl'
4
- require 'rom/sql/function'
3
+ require "rom/sql/dsl"
4
+ require "rom/sql/function"
5
5
 
6
6
  module ROM
7
7
  module SQL
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rake'
4
- load 'rom/sql/tasks/migration_tasks.rake'
3
+ require "rake"
4
+ load "rom/sql/tasks/migration_tasks.rake"