rom-sql 0.8.0 → 0.9.0

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 (87) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +16 -12
  3. data/CHANGELOG.md +23 -0
  4. data/Gemfile +11 -3
  5. data/README.md +1 -7
  6. data/lib/rom/sql.rb +4 -7
  7. data/lib/rom/sql/association.rb +1 -1
  8. data/lib/rom/sql/association/one_to_many.rb +44 -1
  9. data/lib/rom/sql/association/one_to_one.rb +1 -38
  10. data/lib/rom/sql/commands.rb +0 -3
  11. data/lib/rom/sql/commands/error_wrapper.rb +1 -1
  12. data/lib/rom/sql/errors.rb +4 -1
  13. data/lib/rom/sql/extensions.rb +19 -0
  14. data/lib/rom/sql/{support → extensions}/active_support_notifications.rb +0 -0
  15. data/lib/rom/sql/extensions/postgres.rb +3 -0
  16. data/lib/rom/sql/{commands/postgres.rb → extensions/postgres/commands.rb} +38 -0
  17. data/lib/rom/sql/extensions/postgres/inferrer.rb +64 -0
  18. data/lib/rom/sql/extensions/postgres/types.rb +65 -0
  19. data/lib/rom/sql/{support → extensions}/rails_log_subscriber.rb +0 -0
  20. data/lib/rom/sql/gateway.rb +15 -4
  21. data/lib/rom/sql/relation.rb +6 -2
  22. data/lib/rom/sql/relation/reading.rb +18 -0
  23. data/lib/rom/sql/schema/dsl.rb +7 -4
  24. data/lib/rom/sql/schema/inferrer.rb +44 -31
  25. data/lib/rom/sql/types.rb +5 -1
  26. data/lib/rom/sql/version.rb +1 -1
  27. data/rom-sql.gemspec +14 -13
  28. data/spec/extensions/postgres/inferrer_spec.rb +40 -0
  29. data/spec/extensions/postgres/integration_spec.rb +38 -0
  30. data/spec/extensions/postgres/types_spec.rb +115 -0
  31. data/spec/integration/association/many_to_many_spec.rb +2 -1
  32. data/spec/integration/association/one_to_one_spec.rb +6 -4
  33. data/spec/integration/combine_spec.rb +1 -1
  34. data/spec/integration/commands/create_spec.rb +46 -21
  35. data/spec/integration/commands/delete_spec.rb +13 -38
  36. data/spec/integration/commands/update_spec.rb +19 -41
  37. data/spec/integration/commands/upsert_spec.rb +1 -1
  38. data/spec/integration/gateway_spec.rb +5 -9
  39. data/spec/integration/migration_spec.rb +6 -7
  40. data/spec/integration/read_spec.rb +30 -38
  41. data/spec/integration/schema_inference_spec.rb +211 -49
  42. data/spec/integration/setup_spec.rb +5 -5
  43. data/spec/integration/support/active_support_notifications_spec.rb +4 -3
  44. data/spec/integration/support/rails_log_subscriber_spec.rb +5 -4
  45. data/spec/shared/database_setup.rb +21 -6
  46. data/spec/spec_helper.rb +44 -35
  47. data/spec/unit/association/one_to_many_spec.rb +20 -0
  48. data/spec/unit/association/one_to_one_spec.rb +23 -2
  49. data/spec/unit/association_errors_spec.rb +1 -1
  50. data/spec/unit/gateway_spec.rb +9 -8
  51. data/spec/unit/logger_spec.rb +1 -1
  52. data/spec/unit/migration_tasks_spec.rb +3 -3
  53. data/spec/unit/migrator_spec.rb +3 -2
  54. data/spec/unit/plugin/assoc_macros/combined_associations_spec.rb +1 -1
  55. data/spec/unit/plugin/assoc_macros/many_to_many_spec.rb +1 -1
  56. data/spec/unit/plugin/assoc_macros/many_to_one_spec.rb +1 -1
  57. data/spec/unit/plugin/assoc_macros/one_to_many_spec.rb +1 -1
  58. data/spec/unit/relation/associations_spec.rb +27 -0
  59. data/spec/unit/relation/avg_spec.rb +11 -0
  60. data/spec/unit/relation/by_pk_spec.rb +15 -0
  61. data/spec/unit/relation/dataset_spec.rb +48 -0
  62. data/spec/unit/relation/distinct_spec.rb +14 -0
  63. data/spec/unit/relation/exclude_spec.rb +13 -0
  64. data/spec/unit/relation/fetch_spec.rb +21 -0
  65. data/spec/unit/relation/having_spec.rb +20 -0
  66. data/spec/unit/relation/inner_join_spec.rb +22 -0
  67. data/spec/unit/relation/inspect_spec.rb +11 -0
  68. data/spec/unit/relation/invert_spec.rb +12 -0
  69. data/spec/unit/relation/left_join_spec.rb +16 -0
  70. data/spec/unit/relation/map_spec.rb +16 -0
  71. data/spec/unit/relation/max_spec.rb +11 -0
  72. data/spec/unit/relation/min_spec.rb +11 -0
  73. data/spec/unit/relation/pluck_spec.rb +11 -0
  74. data/spec/unit/relation/prefix_spec.rb +27 -0
  75. data/spec/unit/relation/primary_key_spec.rb +27 -0
  76. data/spec/unit/relation/project_spec.rb +22 -0
  77. data/spec/unit/relation/qualified_columns_spec.rb +27 -0
  78. data/spec/unit/relation/rename_spec.rb +21 -0
  79. data/spec/unit/relation/sum_spec.rb +11 -0
  80. data/spec/unit/relation/union_spec.rb +19 -0
  81. data/spec/unit/relation/unique_predicate_spec.rb +18 -0
  82. data/spec/unit/schema_spec.rb +1 -1
  83. data/spec/unit/types_spec.rb +4 -21
  84. metadata +79 -11
  85. data/lib/rom/sql/commands_ext/postgres.rb +0 -45
  86. data/lib/rom/sql/types/pg.rb +0 -26
  87. data/spec/unit/relation_spec.rb +0 -272
@@ -0,0 +1,11 @@
1
+ RSpec.describe ROM::Relation, '#max' do
2
+ subject(:relation) { container.relations.users }
3
+
4
+ include_context 'users and tasks'
5
+
6
+ with_adapters do
7
+ it 'delegates to dataset and return value' do
8
+ expect(relation.max(:id)).to eql(2)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ RSpec.describe ROM::Relation, '#min' do
2
+ subject(:relation) { container.relations.users }
3
+
4
+ include_context 'users and tasks'
5
+
6
+ with_adapters do
7
+ it 'returns a min' do
8
+ expect(relation.min(:id)).to eql(1)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ RSpec.describe ROM::Relation, '#pluck' do
2
+ subject(:relation) { container.relations.users }
3
+
4
+ include_context 'users and tasks'
5
+
6
+ with_adapters do
7
+ it 'returns a list of values from a specific column' do
8
+ expect(relation.pluck(:id)).to eql([1, 2])
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,27 @@
1
+ RSpec.describe ROM::Relation, '#prefix' do
2
+ subject(:relation) { container.relations.users }
3
+
4
+ include_context 'users and tasks'
5
+
6
+ before do
7
+ conf.relation(:users) do
8
+ def sorted
9
+ order(:id)
10
+ end
11
+ end
12
+ end
13
+
14
+ with_adapters do
15
+ it 'projects the dataset using new column names' do
16
+ prefixed = relation.sorted.prefix(:user)
17
+
18
+ expect(prefixed.first).to eql(user_id: 1, user_name: 'Jane')
19
+ end
20
+
21
+ it 'uses singularized table name as the default prefix' do
22
+ prefixed = relation.sorted.prefix
23
+
24
+ expect(prefixed.first).to eql(user_id: 1, user_name: 'Jane')
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ RSpec.describe ROM::Relation, '#primary_key' do
2
+ subject(:relation) { container.relations.users }
3
+
4
+ include_context 'users and tasks'
5
+
6
+ with_adapters do
7
+ context 'with schema' do
8
+ it 'returns configured primary key from the schema' do
9
+ conf.relation(:users) do
10
+ schema do
11
+ attribute :name, ROM::SQL::Types::String.meta(primary_key: true)
12
+ end
13
+ end
14
+
15
+ expect(relation.primary_key).to be(:name)
16
+ end
17
+ end
18
+
19
+ context 'without schema' do
20
+ it 'returns :id by default' do
21
+ conf.relation(:users)
22
+
23
+ expect(relation.primary_key).to be(:id)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,22 @@
1
+ RSpec.describe ROM::Relation, '#project' do
2
+ subject(:relation) { container.relations.users }
3
+
4
+ include_context 'users and tasks'
5
+
6
+ before do
7
+ conf.relation(:users) do
8
+ def sorted
9
+ order(:id)
10
+ end
11
+ end
12
+ end
13
+
14
+ with_adapters do
15
+ it 'projects the dataset using new column names' do
16
+ projected = relation.sorted.project(:name)
17
+
18
+ expect(projected.header).to match_array([:name])
19
+ expect(projected.first).to eql(name: 'Jane')
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,27 @@
1
+ RSpec.describe ROM::Relation, '#qualified_columns' do
2
+ subject(:relation) { container.relations.users }
3
+
4
+ include_context 'users and tasks'
5
+
6
+ before do
7
+ conf.relation(:users) do
8
+ def sorted
9
+ order(:id)
10
+ end
11
+ end
12
+ end
13
+
14
+ with_adapters do
15
+ it 'returns qualified column names' do
16
+ columns = relation.sorted.prefix(:user).qualified_columns
17
+
18
+ expect(columns).to eql([:users__id___user_id, :users__name___user_name])
19
+ end
20
+
21
+ it 'returns projected qualified column names' do
22
+ columns = relation.sorted.project(:id).prefix(:user).qualified_columns
23
+
24
+ expect(columns).to eql([:users__id___user_id])
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,21 @@
1
+ RSpec.describe ROM::Relation, '#rename' do
2
+ subject(:relation) { container.relations.users }
3
+
4
+ include_context 'users and tasks'
5
+
6
+ before do
7
+ conf.relation(:users) do
8
+ def sorted
9
+ order(:id)
10
+ end
11
+ end
12
+ end
13
+
14
+ with_adapters do
15
+ it 'projects the dataset using new column names' do
16
+ renamed = relation.sorted.rename(id: :user_id, name: :user_name)
17
+
18
+ expect(renamed.first).to eql(user_id: 1, user_name: 'Jane')
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,11 @@
1
+ RSpec.describe ROM::Relation, '#sum' do
2
+ subject(:relation) { container.relations.users }
3
+
4
+ include_context 'users and tasks'
5
+
6
+ with_adapters do
7
+ it 'returns a sum' do
8
+ expect(relation.sum(:id)).to eql(3)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ RSpec.describe ROM::Relation, '#union' do
2
+ subject(:relation) { container.relations.users }
3
+
4
+ include_context 'users and tasks'
5
+
6
+ with_adapters do
7
+ let(:relation1) { relation.where(id: 1).select(:id, :name) }
8
+ let(:relation2) { relation.where(id: 2).select(:id, :name) }
9
+
10
+ it 'unions two relations and returns a new relation' do
11
+ result = relation1.union(relation2)
12
+
13
+ expect(result.to_a).to match_array([
14
+ { id: 1, name: 'Jane' },
15
+ { id: 2, name: 'Joe' }
16
+ ])
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ RSpec.describe ROM::Relation, '#unique?' do
2
+ subject(:relation) { container.relations.tasks }
3
+
4
+ include_context 'users and tasks'
5
+
6
+ with_adapters do
7
+ before { relation.delete }
8
+
9
+ it 'returns true when there is only one tuple matching criteria' do
10
+ expect(relation.unique?(title: 'Task One')).to be(true)
11
+ end
12
+
13
+ it 'returns true when there are more than one tuple matching criteria' do
14
+ relation.insert(title: 'Task One')
15
+ expect(relation.unique?(title: 'Task One')).to be(false)
16
+ end
17
+ end
18
+ end
@@ -1,4 +1,4 @@
1
- RSpec.describe ROM::SQL::Schema do
1
+ RSpec.describe ROM::SQL::Schema, :postgres do
2
2
  describe '#primary_key' do
3
3
  it 'returns primary key attributes' do
4
4
  schema = Class.new(ROM::Relation[:sql]).schema do
@@ -1,8 +1,7 @@
1
1
  require 'rom/sql/types'
2
- require 'rom/sql/types/pg'
3
2
 
4
- RSpec.describe ROM::SQL::Types do
5
- describe ROM::SQL::Types::Serial do
3
+ RSpec.describe 'ROM::SQL::Types', :postgres do
4
+ describe 'ROM::SQL::Types::Serial' do
6
5
  it 'accepts ints > 0' do
7
6
  expect(ROM::SQL::Types::Serial[1]).to be(1)
8
7
  end
@@ -12,24 +11,8 @@ RSpec.describe ROM::SQL::Types do
12
11
  end
13
12
  end
14
13
 
15
- describe ROM::SQL::Types::PG::JSON do
16
- it 'coerces to pg json hash' do
17
- input = { foo: 'bar' }
18
-
19
- expect(ROM::SQL::Types::PG::JSON[input]).to eql(Sequel.pg_json(input))
20
- end
21
-
22
- it 'coerces to pg json array' do
23
- input = [1, 2, 3]
24
- output = ROM::SQL::Types::PG::JSON[input]
25
-
26
- expect(output).to be_instance_of(Sequel::Postgres::JSONArray)
27
- expect(output.to_a).to eql(input)
28
- end
29
- end
30
-
31
- describe ROM::SQL::Types::PG::Bytea do
32
- it 'coerses strings to Sequel::SQL::Blob' do
14
+ describe ROM::SQL::Types::Blob do
15
+ it 'coerces strings to Sequel::SQL::Blob' do
33
16
  input = 'sutin'
34
17
  output = described_class[input]
35
18
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rom-sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Solnica
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-27 00:00:00.000000000 Z
11
+ date: 2016-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -44,14 +44,28 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0.8'
47
+ version: '0.9'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0.8'
54
+ version: '0.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: dry-core
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.2'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rom
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -141,12 +155,17 @@ files:
141
155
  - lib/rom/sql/commands/create.rb
142
156
  - lib/rom/sql/commands/delete.rb
143
157
  - lib/rom/sql/commands/error_wrapper.rb
144
- - lib/rom/sql/commands/postgres.rb
145
158
  - lib/rom/sql/commands/transaction.rb
146
159
  - lib/rom/sql/commands/update.rb
147
- - lib/rom/sql/commands_ext/postgres.rb
148
160
  - lib/rom/sql/error.rb
149
161
  - lib/rom/sql/errors.rb
162
+ - lib/rom/sql/extensions.rb
163
+ - lib/rom/sql/extensions/active_support_notifications.rb
164
+ - lib/rom/sql/extensions/postgres.rb
165
+ - lib/rom/sql/extensions/postgres/commands.rb
166
+ - lib/rom/sql/extensions/postgres/inferrer.rb
167
+ - lib/rom/sql/extensions/postgres/types.rb
168
+ - lib/rom/sql/extensions/rails_log_subscriber.rb
150
169
  - lib/rom/sql/gateway.rb
151
170
  - lib/rom/sql/header.rb
152
171
  - lib/rom/sql/migration.rb
@@ -167,14 +186,14 @@ files:
167
186
  - lib/rom/sql/schema/dsl.rb
168
187
  - lib/rom/sql/schema/inferrer.rb
169
188
  - lib/rom/sql/spec/support.rb
170
- - lib/rom/sql/support/active_support_notifications.rb
171
- - lib/rom/sql/support/rails_log_subscriber.rb
172
189
  - lib/rom/sql/tasks/migration_tasks.rake
173
190
  - lib/rom/sql/types.rb
174
- - lib/rom/sql/types/pg.rb
175
191
  - lib/rom/sql/version.rb
176
192
  - log/.gitkeep
177
193
  - rom-sql.gemspec
194
+ - spec/extensions/postgres/inferrer_spec.rb
195
+ - spec/extensions/postgres/integration_spec.rb
196
+ - spec/extensions/postgres/types_spec.rb
178
197
  - spec/fixtures/migrations/20150403090603_create_carrots.rb
179
198
  - spec/integration/association/many_to_many_spec.rb
180
199
  - spec/integration/association/many_to_one_spec.rb
@@ -219,7 +238,30 @@ files:
219
238
  - spec/unit/plugin/assoc_macros/one_to_many_spec.rb
220
239
  - spec/unit/plugin/base_view_spec.rb
221
240
  - spec/unit/plugin/pagination_spec.rb
222
- - spec/unit/relation_spec.rb
241
+ - spec/unit/relation/associations_spec.rb
242
+ - spec/unit/relation/avg_spec.rb
243
+ - spec/unit/relation/by_pk_spec.rb
244
+ - spec/unit/relation/dataset_spec.rb
245
+ - spec/unit/relation/distinct_spec.rb
246
+ - spec/unit/relation/exclude_spec.rb
247
+ - spec/unit/relation/fetch_spec.rb
248
+ - spec/unit/relation/having_spec.rb
249
+ - spec/unit/relation/inner_join_spec.rb
250
+ - spec/unit/relation/inspect_spec.rb
251
+ - spec/unit/relation/invert_spec.rb
252
+ - spec/unit/relation/left_join_spec.rb
253
+ - spec/unit/relation/map_spec.rb
254
+ - spec/unit/relation/max_spec.rb
255
+ - spec/unit/relation/min_spec.rb
256
+ - spec/unit/relation/pluck_spec.rb
257
+ - spec/unit/relation/prefix_spec.rb
258
+ - spec/unit/relation/primary_key_spec.rb
259
+ - spec/unit/relation/project_spec.rb
260
+ - spec/unit/relation/qualified_columns_spec.rb
261
+ - spec/unit/relation/rename_spec.rb
262
+ - spec/unit/relation/sum_spec.rb
263
+ - spec/unit/relation/union_spec.rb
264
+ - spec/unit/relation/unique_predicate_spec.rb
223
265
  - spec/unit/schema_spec.rb
224
266
  - spec/unit/types_spec.rb
225
267
  homepage: http://rom-rb.org
@@ -247,6 +289,9 @@ signing_key:
247
289
  specification_version: 4
248
290
  summary: SQL databases support for ROM
249
291
  test_files:
292
+ - spec/extensions/postgres/inferrer_spec.rb
293
+ - spec/extensions/postgres/integration_spec.rb
294
+ - spec/extensions/postgres/types_spec.rb
250
295
  - spec/fixtures/migrations/20150403090603_create_carrots.rb
251
296
  - spec/integration/association/many_to_many_spec.rb
252
297
  - spec/integration/association/many_to_one_spec.rb
@@ -291,6 +336,29 @@ test_files:
291
336
  - spec/unit/plugin/assoc_macros/one_to_many_spec.rb
292
337
  - spec/unit/plugin/base_view_spec.rb
293
338
  - spec/unit/plugin/pagination_spec.rb
294
- - spec/unit/relation_spec.rb
339
+ - spec/unit/relation/associations_spec.rb
340
+ - spec/unit/relation/avg_spec.rb
341
+ - spec/unit/relation/by_pk_spec.rb
342
+ - spec/unit/relation/dataset_spec.rb
343
+ - spec/unit/relation/distinct_spec.rb
344
+ - spec/unit/relation/exclude_spec.rb
345
+ - spec/unit/relation/fetch_spec.rb
346
+ - spec/unit/relation/having_spec.rb
347
+ - spec/unit/relation/inner_join_spec.rb
348
+ - spec/unit/relation/inspect_spec.rb
349
+ - spec/unit/relation/invert_spec.rb
350
+ - spec/unit/relation/left_join_spec.rb
351
+ - spec/unit/relation/map_spec.rb
352
+ - spec/unit/relation/max_spec.rb
353
+ - spec/unit/relation/min_spec.rb
354
+ - spec/unit/relation/pluck_spec.rb
355
+ - spec/unit/relation/prefix_spec.rb
356
+ - spec/unit/relation/primary_key_spec.rb
357
+ - spec/unit/relation/project_spec.rb
358
+ - spec/unit/relation/qualified_columns_spec.rb
359
+ - spec/unit/relation/rename_spec.rb
360
+ - spec/unit/relation/sum_spec.rb
361
+ - spec/unit/relation/union_spec.rb
362
+ - spec/unit/relation/unique_predicate_spec.rb
295
363
  - spec/unit/schema_spec.rb
296
364
  - spec/unit/types_spec.rb
@@ -1,45 +0,0 @@
1
- require 'rom/sql/commands/create'
2
- require 'rom/sql/commands/update'
3
-
4
- module ROM
5
- module SQL
6
- module Commands
7
- module Postgres
8
- module Create
9
- # Executes insert statement and returns inserted tuples
10
- #
11
- # @api private
12
- def insert(tuples)
13
- tuples.map do |tuple|
14
- relation.dataset.returning(*relation.columns).insert(tuple)
15
- end.flatten
16
- end
17
-
18
- # Executes multi_insert statement and returns inserted tuples
19
- #
20
- # @api private
21
- def multi_insert(tuples)
22
- relation.dataset.returning(*relation.columns).multi_insert(tuples)
23
- end
24
-
25
- # Executes upsert statement (INSERT with ON CONFLICT clause)
26
- # and returns inserted/updated tuples
27
- #
28
- # @api private
29
- def upsert(tuple, opts = EMPTY_HASH)
30
- relation.dataset.returning(*relation.columns).insert_conflict(opts).insert(tuple)
31
- end
32
- end
33
-
34
- module Update
35
- # Executes update statement and returns updated tuples
36
- #
37
- # @api private
38
- def update(tuple)
39
- relation.dataset.returning(*relation.columns).update(tuple)
40
- end
41
- end
42
- end
43
- end
44
- end
45
- end