rom-sql 3.7.0 → 4.0.0.alpha1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +11 -65
  3. data/LICENSE +1 -1
  4. data/README.md +3 -2
  5. data/lib/rom/plugins/relation/sql/auto_restrictions.rb +10 -12
  6. data/lib/rom/plugins/relation/sql/default_views.rb +75 -0
  7. data/lib/rom/plugins/relation/sql/instrumentation.rb +8 -17
  8. data/lib/rom/plugins/relation/sql/postgres/explain.rb +5 -5
  9. data/lib/rom/plugins/relation/sql/postgres/full_text_search.rb +20 -37
  10. data/lib/rom/plugins/relation/sql/postgres/streaming.rb +17 -14
  11. data/lib/rom/sql/associations/many_to_many.rb +4 -7
  12. data/lib/rom/sql/associations/many_to_one.rb +3 -6
  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 +17 -31
  19. data/lib/rom/sql/attribute_aliasing.rb +4 -6
  20. data/lib/rom/sql/commands/create.rb +5 -5
  21. data/lib/rom/sql/commands/delete.rb +2 -2
  22. data/lib/rom/sql/commands/update.rb +5 -5
  23. data/lib/rom/sql/commands.rb +4 -4
  24. data/lib/rom/sql/dsl.rb +4 -6
  25. data/lib/rom/sql/errors.rb +3 -3
  26. data/lib/rom/sql/extensions/active_support_notifications.rb +3 -3
  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 +19 -19
  35. data/lib/rom/sql/extensions/postgres/types/json.rb +12 -18
  36. data/lib/rom/sql/extensions/postgres/types/ltree.rb +54 -97
  37. data/lib/rom/sql/extensions/postgres/types/network.rb +4 -17
  38. data/lib/rom/sql/extensions/postgres/types/range.rb +30 -30
  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 +6 -21
  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 +19 -42
  47. data/lib/rom/sql/gateway.rb +41 -15
  48. data/lib/rom/sql/group_dsl.rb +3 -8
  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/inline_runner.rb +2 -8
  53. data/lib/rom/sql/migration/migrator.rb +12 -12
  54. data/lib/rom/sql/migration/recorder.rb +4 -10
  55. data/lib/rom/sql/migration/runner.rb +4 -5
  56. data/lib/rom/sql/migration/schema_diff.rb +4 -10
  57. data/lib/rom/sql/migration/writer.rb +7 -7
  58. data/lib/rom/sql/migration.rb +9 -13
  59. data/lib/rom/sql/order_dsl.rb +3 -7
  60. data/lib/rom/sql/plugin/associates.rb +45 -21
  61. data/lib/rom/sql/plugin/pagination.rb +3 -1
  62. data/lib/rom/sql/plugin/schema_indexes.rb +35 -0
  63. data/lib/rom/sql/plugins.rb +9 -6
  64. data/lib/rom/sql/projection_dsl.rb +5 -5
  65. data/lib/rom/sql/rake_task.rb +2 -2
  66. data/lib/rom/sql/relation/reading.rb +78 -83
  67. data/lib/rom/sql/relation/writing.rb +4 -9
  68. data/lib/rom/sql/relation.rb +58 -136
  69. data/lib/rom/sql/restriction_dsl.rb +4 -8
  70. data/lib/rom/sql/schema/attributes_inferrer.rb +2 -2
  71. data/lib/rom/sql/schema/dsl.rb +6 -4
  72. data/lib/rom/sql/schema/index_dsl.rb +6 -7
  73. data/lib/rom/sql/schema/inferrer.rb +22 -24
  74. data/lib/rom/sql/schema/type_builder.rb +4 -20
  75. data/lib/rom/sql/schema.rb +17 -29
  76. data/lib/rom/sql/spec/support.rb +5 -5
  77. data/lib/rom/sql/tasks/migration_tasks.rake +14 -21
  78. data/lib/rom/sql/transaction.rb +4 -3
  79. data/lib/rom/sql/type_dsl.rb +3 -7
  80. data/lib/rom/sql/type_extensions.rb +6 -4
  81. data/lib/rom/sql/type_serializer.rb +9 -9
  82. data/lib/rom/sql/types.rb +6 -6
  83. data/lib/rom/sql/version.rb +1 -1
  84. data/lib/rom/sql/wrap.rb +1 -1
  85. data/lib/rom/sql.rb +13 -12
  86. data/lib/rom/types/values.rb +5 -3
  87. data/lib/rom-sql.rb +1 -1
  88. metadata +17 -12
@@ -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
@@ -132,7 +132,6 @@ module ROM
132
132
  end
133
133
 
134
134
  class IndexAdded < IndexDiff
135
- # rubocop:disable Metrics/AbcSize
136
135
  def options
137
136
  options = {}
138
137
  options[:name] = index.name unless index.name.nil?
@@ -141,7 +140,6 @@ module ROM
141
140
  options[:where] = index.predicate unless index.predicate.nil?
142
141
  options
143
142
  end
144
- # rubocop:enable Metrics/AbcSize
145
143
  end
146
144
 
147
145
  class IndexRemoved < IndexDiff
@@ -178,7 +176,6 @@ module ROM
178
176
  class ForeignKeyRemoved < ForeignKeyDiff
179
177
  end
180
178
 
181
- # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity, Metrics/MethodLength
182
179
  def call(current, target)
183
180
  if current.empty?
184
181
  TableCreated.new(
@@ -205,13 +202,13 @@ module ROM
205
202
  end
206
203
  end
207
204
  end
208
- # rubocop:enable Metrics/AbcSize, Metrics/PerceivedComplexity, Metrics/MethodLength
209
205
 
210
- # rubocop:disable Style/MultilineBlockChain
211
206
  def compare_attributes(current, target)
212
207
  changed_attributes = target.select { |name, attr|
213
208
  current.key?(name) && !attributes_equal?(current[name], attr)
214
- }.to_h { |name, target_attr| [name, [target_attr, current[name]]] }
209
+ }.map { |name, target_attr|
210
+ [name, [target_attr, current[name]]]
211
+ }.to_h
215
212
  added_attributes = target.reject { |name, _| current.key?(name) }
216
213
  removed_attributes = current.reject { |name, _| target.key?(name) }
217
214
 
@@ -219,9 +216,7 @@ module ROM
219
216
  map_attributes(added_attributes, AttributeAdded) +
220
217
  map_attributes(changed_attributes, AttributeChanged)
221
218
  end
222
- # rubocop:enable Style/MultilineBlockChain
223
219
 
224
- # rubocop:disable Metrics/AbcSize
225
220
  def compare_indexes(current, target)
226
221
  added_indexes = target.indexes.reject { |idx|
227
222
  current.indexes.any? { |curr_idx| curr_idx.attributes == idx.attributes }
@@ -233,7 +228,6 @@ module ROM
233
228
  removed_indexes.map { |idx| IndexRemoved.new(idx) } +
234
229
  added_indexes.map { |idx| IndexAdded.new(idx) }
235
230
  end
236
- # rubocop:enable Metrics/AbcSize
237
231
 
238
232
  def compare_foreign_key_constraints(current, target)
239
233
  target_fks = target.foreign_keys
@@ -1,6 +1,6 @@
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
@@ -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
39
  next if nested.empty?
40
40
 
41
- buffer << ' do'
42
- write(nested, buffer, "#{indent} ")
43
- buffer << indent << 'end'
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
 
@@ -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
@@ -54,12 +54,12 @@ module ROM
54
54
  # @param [Symbol] gateway The gateway name, :default by default
55
55
  #
56
56
  # @api public
57
- def migration(*args, &)
57
+ def migration(*args, &block)
58
58
  if args.any?
59
59
  container, gateway, * = args
60
- with_gateway(container.gateways[gateway || :default]) { migration(&) }
60
+ with_gateway(container.gateways[gateway || :default]) { migration(&block) }
61
61
  else
62
- current_gateway.migration(&)
62
+ current_gateway.migration(&block)
63
63
  end
64
64
  end
65
65
 
@@ -121,8 +121,8 @@ module ROM
121
121
  # @see ROM::SQL.migration
122
122
  #
123
123
  # @api public
124
- def migration(&)
125
- migrator.migration(&)
124
+ def migration(&block)
125
+ migrator.migration(&block)
126
126
  end
127
127
 
128
128
  # Run migrations
@@ -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
@@ -8,16 +8,12 @@ module ROM
8
8
  class OrderDSL < DSL
9
9
  private
10
10
 
11
- def respond_to_missing?(_meth, _include_private = false)
12
- true
13
- end
14
-
15
11
  # @api private
16
- def method_missing(meth, ...)
12
+ def method_missing(meth, *args, &block)
17
13
  if schema.key?(meth)
18
14
  schema[meth]
19
15
  else
20
- ::Sequel::VIRTUAL_ROW.__send__(meth.to_s.upcase, ...)
16
+ ::Sequel::VIRTUAL_ROW.__send__(meth.to_s.upcase, *args, &block)
21
17
  end
22
18
  end
23
19
  end
@@ -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
@@ -10,17 +10,13 @@ module ROM
10
10
  # @api private
11
11
  module Associates
12
12
  class AssociateOptions
13
- attr_reader :name
14
-
15
- attr_reader :assoc
16
-
17
- attr_reader :opts
13
+ attr_reader :name, :assoc, :opts
18
14
 
19
15
  # @api private
20
16
  def initialize(name, relation, opts)
21
17
  @name = name
22
18
  @assoc = relation.associations[name]
23
- @opts = { assoc: assoc, keys: assoc.join_keys }
19
+ @opts = {assoc: assoc, keys: assoc.join_keys}
24
20
  @opts.update(parent: opts[:parent]) if opts[:parent]
25
21
  end
26
22
 
@@ -29,7 +25,7 @@ module ROM
29
25
  end
30
26
 
31
27
  def to_hash
32
- { associate: opts }
28
+ {associate: opts}
33
29
  end
34
30
  end
35
31
 
@@ -39,11 +35,10 @@ module ROM
39
35
  extend ClassMethods
40
36
  include InstanceMethods
41
37
 
42
- defines :associations
43
-
44
- associations({})
38
+ setting :associations, default: {}, reader: true
45
39
 
46
40
  option :associations, default: -> { self.class.associations }
41
+
47
42
  option :configured_associations, default: -> { EMPTY_ARRAY }
48
43
  end
49
44
  super
@@ -51,6 +46,39 @@ module ROM
51
46
 
52
47
  # @api public
53
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
+
54
82
  # @see ROM::Command::ClassInterface.build
55
83
  #
56
84
  # @api public
@@ -101,7 +129,9 @@ module ROM
101
129
  "#{name} association is already defined for #{self.class}"
102
130
  end
103
131
 
104
- associations(associations.merge(name => options))
132
+ associations[name] = options
133
+
134
+ self
105
135
  end
106
136
  end
107
137
 
@@ -115,14 +145,8 @@ module ROM
115
145
  #
116
146
  # @api public
117
147
  #
118
- # rubocop:disable Metrics/PerceivedComplexity, Metrics/AbcSize
119
- def associate(
120
- tuples,
121
- curried_parent = nil,
122
- assoc:,
123
- keys:, # rubocop:disable Lint/UnusedMethodArgument
124
- parent: curried_parent
125
- )
148
+ # rubocop:disable Lint/UnusedMethodArgument
149
+ def associate(tuples, curried_parent = nil, assoc:, keys:, parent: curried_parent)
126
150
  result_type = result
127
151
 
128
152
  output_tuples =
@@ -150,7 +174,7 @@ module ROM
150
174
 
151
175
  result_type == :one ? output_tuples[0] : output_tuples
152
176
  end
153
- # rubocop:enable Metrics/PerceivedComplexity, Metrics/AbcSize
177
+ # rubocop:enable Lint/UnusedMethodArgument
154
178
 
155
179
  # Return a new command with the provided association
156
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
@@ -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
@@ -37,7 +37,7 @@ module ROM
37
37
  #
38
38
  # @api public
39
39
  def function(name, *attrs)
40
- ::ROM::SQL::Function.new(::ROM::Types::Any).meta(schema: schema).public_send(name, *attrs)
40
+ ::ROM::SQL::Function.new(::ROM::Types::Any, schema: schema).public_send(name, *attrs)
41
41
  end
42
42
  alias_method :f, :function
43
43
 
@@ -49,7 +49,7 @@ module ROM
49
49
  private
50
50
 
51
51
  # @api private
52
- def method_missing(meth, *args, &)
52
+ def method_missing(meth, *args, &block)
53
53
  if schema.key?(meth)
54
54
  schema[meth]
55
55
  else
@@ -57,7 +57,7 @@ module ROM
57
57
 
58
58
  if type
59
59
  if args.empty?
60
- ::ROM::SQL::Function.new(type).meta(schema: schema)
60
+ ::ROM::SQL::Function.new(type, schema: schema)
61
61
  else
62
62
  ::ROM::SQL::Attribute[type].value(args[0])
63
63
  end
@@ -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"