rom-sql 0.9.1 → 1.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -1
  3. data/CHANGELOG.md +32 -0
  4. data/Gemfile +4 -1
  5. data/lib/rom/plugins/relation/sql/auto_wrap.rb +1 -3
  6. data/lib/rom/sql/association.rb +33 -14
  7. data/lib/rom/sql/association/many_to_many.rb +17 -10
  8. data/lib/rom/sql/association/many_to_one.rb +29 -13
  9. data/lib/rom/sql/association/name.rb +12 -4
  10. data/lib/rom/sql/association/one_to_many.rb +21 -10
  11. data/lib/rom/sql/commands/create.rb +0 -1
  12. data/lib/rom/sql/commands/update.rb +1 -49
  13. data/lib/rom/sql/dsl.rb +29 -0
  14. data/lib/rom/sql/expression.rb +26 -0
  15. data/lib/rom/sql/function.rb +23 -0
  16. data/lib/rom/sql/gateway.rb +24 -9
  17. data/lib/rom/sql/migration.rb +6 -7
  18. data/lib/rom/sql/migration/migrator.rb +7 -8
  19. data/lib/rom/sql/order_dsl.rb +20 -0
  20. data/lib/rom/sql/plugin/associates.rb +58 -45
  21. data/lib/rom/sql/plugin/pagination.rb +8 -11
  22. data/lib/rom/sql/plugins.rb +0 -2
  23. data/lib/rom/sql/projection_dsl.rb +41 -0
  24. data/lib/rom/sql/qualified_attribute.rb +2 -2
  25. data/lib/rom/sql/relation.rb +35 -67
  26. data/lib/rom/sql/relation/reading.rb +77 -25
  27. data/lib/rom/sql/restriction_dsl.rb +24 -0
  28. data/lib/rom/sql/schema.rb +73 -7
  29. data/lib/rom/sql/schema/associations_dsl.rb +4 -3
  30. data/lib/rom/sql/schema/dsl.rb +5 -2
  31. data/lib/rom/sql/schema/inferrer.rb +21 -11
  32. data/lib/rom/sql/transaction.rb +19 -0
  33. data/lib/rom/sql/type.rb +76 -0
  34. data/lib/rom/sql/version.rb +1 -1
  35. data/rom-sql.gemspec +3 -4
  36. data/spec/extensions/postgres/inferrer_spec.rb +19 -9
  37. data/spec/integration/association/many_to_many/custom_fks_spec.rb +73 -0
  38. data/spec/integration/association/many_to_many/from_view_spec.rb +81 -0
  39. data/spec/integration/association/many_to_many_spec.rb +2 -2
  40. data/spec/integration/association/many_to_one/custom_fks_spec.rb +59 -0
  41. data/spec/integration/association/many_to_one/from_view_spec.rb +74 -0
  42. data/spec/integration/association/many_to_one/self_ref_spec.rb +51 -0
  43. data/spec/integration/association/many_to_one_spec.rb +4 -2
  44. data/spec/integration/association/one_to_many/custom_fks_spec.rb +48 -0
  45. data/spec/integration/association/one_to_many/from_view_spec.rb +57 -0
  46. data/spec/integration/association/one_to_many/self_ref_spec.rb +52 -0
  47. data/spec/integration/association/one_to_many_spec.rb +1 -1
  48. data/spec/integration/association/one_to_one_spec.rb +1 -1
  49. data/spec/integration/association/one_to_one_through_spec.rb +2 -2
  50. data/spec/integration/commands/create_spec.rb +11 -27
  51. data/spec/integration/commands/update_spec.rb +54 -109
  52. data/spec/integration/gateway_spec.rb +31 -17
  53. data/spec/integration/plugins/associates_spec.rb +27 -0
  54. data/spec/integration/plugins/auto_wrap_spec.rb +8 -8
  55. data/spec/integration/schema/call_spec.rb +24 -0
  56. data/spec/integration/schema/prefix_spec.rb +18 -0
  57. data/spec/integration/schema/qualified_spec.rb +18 -0
  58. data/spec/integration/schema/rename_spec.rb +23 -0
  59. data/spec/integration/schema/view_spec.rb +29 -0
  60. data/spec/integration/schema_inference_spec.rb +31 -14
  61. data/spec/spec_helper.rb +2 -2
  62. data/spec/support/helpers.rb +7 -0
  63. data/spec/unit/gateway_spec.rb +5 -4
  64. data/spec/unit/projection_dsl_spec.rb +54 -0
  65. data/spec/unit/relation/dataset_spec.rb +3 -3
  66. data/spec/unit/relation/distinct_spec.rb +8 -7
  67. data/spec/unit/relation/exclude_spec.rb +2 -4
  68. data/spec/unit/relation/having_spec.rb +6 -4
  69. data/spec/unit/relation/inner_join_spec.rb +47 -2
  70. data/spec/unit/relation/invert_spec.rb +2 -3
  71. data/spec/unit/relation/left_join_spec.rb +44 -3
  72. data/spec/unit/relation/order_spec.rb +40 -0
  73. data/spec/unit/relation/prefix_spec.rb +2 -0
  74. data/spec/unit/relation/project_spec.rb +3 -1
  75. data/spec/unit/relation/qualified_columns_spec.rb +2 -0
  76. data/spec/unit/relation/rename_spec.rb +2 -0
  77. data/spec/unit/relation/right_join_spec.rb +59 -0
  78. data/spec/unit/relation/select_append_spec.rb +21 -0
  79. data/spec/unit/relation/select_spec.rb +41 -0
  80. data/spec/unit/relation/where_spec.rb +28 -0
  81. data/spec/unit/restriction_dsl_spec.rb +34 -0
  82. metadata +62 -40
  83. data/lib/rom/plugins/relation/sql/base_view.rb +0 -31
  84. data/lib/rom/sql/header.rb +0 -61
  85. data/lib/rom/sql/plugin/assoc_macros.rb +0 -133
  86. data/lib/rom/sql/plugin/assoc_macros/class_interface.rb +0 -128
  87. data/spec/integration/read_spec.rb +0 -111
  88. data/spec/unit/association_errors_spec.rb +0 -19
  89. data/spec/unit/plugin/assoc_macros/combined_associations_spec.rb +0 -73
  90. data/spec/unit/plugin/assoc_macros/many_to_many_spec.rb +0 -53
  91. data/spec/unit/plugin/assoc_macros/many_to_one_spec.rb +0 -61
  92. data/spec/unit/plugin/assoc_macros/one_to_many_spec.rb +0 -78
  93. data/spec/unit/plugin/base_view_spec.rb +0 -18
@@ -1,19 +0,0 @@
1
- RSpec.describe 'Association errors', :postgres do
2
- include_context 'users and tasks'
3
-
4
- describe 'accessing an undefined association' do
5
- specify do
6
- conf.relation(:users) do
7
- use :assoc_macros
8
-
9
- def with_undefined
10
- association_join(:undefined)
11
- end
12
- end
13
-
14
- expect {
15
- container.relations.users.with_undefined
16
- }.to raise_error ROM::SQL::NoAssociationError, 'Association :undefined has not been defined for relation :users'
17
- end
18
- end
19
- end
@@ -1,73 +0,0 @@
1
- RSpec.describe 'Defining multiple associations', :postgres do
2
- include_context 'users and tasks'
3
-
4
- it 'extends relation with association methods' do
5
- conf.relation(:users) { use :assoc_macros }
6
-
7
- conf.relation(:tags) { use :assoc_macros }
8
-
9
- conf.relation(:tasks) do
10
- use :assoc_macros
11
-
12
- many_to_one :users, key: :user_id
13
-
14
- many_to_many :tags,
15
- join_table: :task_tags,
16
- left_key: :task_id,
17
- right_key: :tag_id
18
-
19
- def with_user_and_tags
20
- all.with_user.with_tags
21
- end
22
-
23
- def all
24
- select(:id, :title)
25
- end
26
-
27
- def by_tag(name)
28
- where(tags__name: name)
29
- end
30
-
31
- def by_title(title)
32
- where(tasks__title: title)
33
- end
34
-
35
- def with_tags
36
- association_left_join(:tags, select: [:name])
37
- end
38
-
39
- def with_user
40
- association_join(:users, select: [:name])
41
- end
42
-
43
- def sorted_by_tags_name
44
- order(Sequel.desc(:tags__name))
45
- end
46
- end
47
-
48
- tasks = container.relations.tasks
49
-
50
- expect(tasks.with_user_and_tags.to_a).to eql([
51
- { id: 1, title: "Joe's task", name: 'Joe', tags_name: 'important' },
52
- { id: 2, title: "Jane's task", name: 'Jane', tags_name: nil }
53
- ])
54
-
55
- expect(tasks.with_user_and_tags.sorted_by_tags_name.to_a).to eql([
56
- { id: 2, title: "Jane's task", name: 'Jane', tags_name: nil },
57
- { id: 1, title: "Joe's task", name: 'Joe', tags_name: 'important' }
58
- ])
59
-
60
- expect(tasks.with_user_and_tags.by_tag('important').to_a).to eql([
61
- { id: 1, title: "Joe's task", name: 'Joe', tags_name: 'important' }
62
- ])
63
-
64
- expect(tasks.all.with_user.to_a).to eql([
65
- { id: 1, title: "Joe's task", name: 'Joe' },
66
- { id: 2, title: "Jane's task", name: 'Jane' }
67
- ])
68
-
69
- expect(tasks.by_title("Jane's task").to_a).to eql(
70
- [{ id: 2, user_id: 1, title: "Jane's task" }]
71
- )
72
- end
73
- end
@@ -1,53 +0,0 @@
1
- RSpec.describe 'Defining many-to-one association', :postgres do
2
- include_context 'users and tasks'
3
-
4
- it 'extends relation with association methods' do
5
- conf.relation(:tags) { use :assoc_macros }
6
- conf.relation(:task_tags) { use :assoc_macros }
7
-
8
- conf.relation(:tasks) do
9
- use :assoc_macros
10
-
11
- many_to_many :tags,
12
- join_table: :task_tags,
13
- left_key: :task_id,
14
- right_key: :tag_id
15
-
16
- def with_tags
17
- association_left_join(:tags, select: [:name])
18
- end
19
-
20
- def with_tags_and_tag_id
21
- association_left_join(:tags, select: {
22
- tags: [:name], task_tags: [:tag_id]
23
- })
24
- end
25
-
26
- def by_tag(name)
27
- with_tags.where(name: name)
28
- end
29
-
30
- def all
31
- select(:id, :title)
32
- end
33
- end
34
-
35
- tasks = container.relations.tasks
36
-
37
- expect(tasks.all.with_tags.to_a).to eql([
38
- { id: 1, title: "Joe's task", name: 'important' },
39
- { id: 2, title: "Jane's task", name: nil }
40
- ])
41
-
42
- expect(tasks.all.with_tags_and_tag_id.to_a).to eql([
43
- { id: 1, title: "Joe's task", tag_id: 1, name: 'important' },
44
- { id: 2, title: "Jane's task", tag_id: nil, name: nil }
45
- ])
46
-
47
- expect(tasks.all.by_tag("important").to_a).to eql([
48
- { id: 1, title: "Joe's task", name: 'important' }
49
- ])
50
-
51
- expect(tasks.by_tag("not-here").to_a).to be_empty
52
- end
53
- end
@@ -1,61 +0,0 @@
1
- RSpec.describe 'Defining many-to-one association', :postgres do
2
- include_context 'users and tasks'
3
-
4
- before do
5
- conf.relation(:users) { use :assoc_macros }
6
- end
7
-
8
- it 'extends relation with association methods' do
9
- conf.relation(:tasks) do
10
- use :assoc_macros
11
-
12
- many_to_one :users, key: :user_id, on: { name: 'Jane' }
13
-
14
- def all
15
- select(:id, :title)
16
- end
17
-
18
- def with_user
19
- association_join(:users, select: [:name])
20
- end
21
- end
22
-
23
- conf.mappers do
24
- define(:tasks)
25
-
26
- define(:with_user, parent: :tasks) do
27
- wrap :user do
28
- attribute :name
29
- end
30
- end
31
- end
32
-
33
- tasks = container.relations.tasks
34
-
35
- expect(tasks.all.with_user.to_a).to eql(
36
- [{ id: 2, name: 'Jane', title: "Jane's task" }]
37
- )
38
-
39
- expect(container.relation(:tasks).map_with(:with_user).all.with_user.to_a).to eql(
40
- [{ id: 2, title: "Jane's task", user: { name: 'Jane' } }]
41
- )
42
- end
43
-
44
- it "joins on specified key" do
45
- conf.relation(:task_tags) do
46
- use :assoc_macros
47
-
48
- many_to_one :tags, key: :tag_id
49
-
50
- def with_tags
51
- association_left_join(:tags)
52
- end
53
- end
54
-
55
- conf.relation(:tags) { use :assoc_macros }
56
-
57
- expect(container.relation(:task_tags).with_tags.to_a).to eq(
58
- [{ tag_id: 1, task_id: 1, id: 1, name: "important" }]
59
- )
60
- end
61
- end
@@ -1,78 +0,0 @@
1
- RSpec.describe 'Defining one-to-many association', :postgres do
2
- include_context 'users and tasks'
3
-
4
- before do
5
- conf.mappers do
6
- define(:users)
7
-
8
- define(:with_tasks, parent: :users) do
9
- group tasks: [:tasks_id, :title]
10
- end
11
- end
12
-
13
- conf.relation(:tasks) { use :assoc_macros }
14
- end
15
-
16
- it 'extends relation with association methods' do
17
- conf.relation(:users) do
18
- use :assoc_macros
19
-
20
- one_to_many :tasks, key: :user_id, on: { title: "Jane's task" }
21
-
22
- def by_name(name)
23
- where(name: name)
24
- end
25
-
26
- def with_tasks
27
- association_left_join(:tasks, select: [:id, :title])
28
- end
29
-
30
- def all
31
- select(:id, :name)
32
- end
33
- end
34
-
35
- users = container.relations.users
36
-
37
- expect(users.with_tasks.by_name("Jane").to_a).to eql(
38
- [{ id: 1, name: 'Jane', tasks_id: 2, title: "Jane's task" }]
39
- )
40
-
41
- result = container.relation(:users).map_with(:with_tasks)
42
- .all.with_tasks.by_name("Jane").to_a
43
-
44
- expect(result).to eql(
45
- [{ id: 1, name: 'Jane', tasks: [{ tasks_id: 2, title: "Jane's task" }] }]
46
- )
47
- end
48
-
49
- it 'allows setting :conditions' do
50
- conf.relation(:users) do
51
- use :assoc_macros
52
-
53
- one_to_many :janes_tasks, relation: :tasks, key: :user_id,
54
- conditions: { name: 'Jane' }
55
-
56
- def with_janes_tasks
57
- association_left_join(:janes_tasks, select: [:id, :title])
58
- end
59
-
60
- def all
61
- select(:id, :name)
62
- end
63
- end
64
-
65
- users = container.relations.users
66
-
67
- expect(users.with_janes_tasks.to_a).to eql(
68
- [{ id: 1, name: 'Jane', tasks_id: 2, title: "Jane's task" }]
69
- )
70
-
71
- result = container.relation(:users).map_with(:with_tasks)
72
- .all.with_janes_tasks.to_a
73
-
74
- expect(result).to eql(
75
- [{ id: 1, name: 'Jane', tasks: [{ tasks_id: 2, title: "Jane's task" }] }]
76
- )
77
- end
78
- end
@@ -1,18 +0,0 @@
1
- RSpec.describe 'Plugin / Base View' do
2
- include_context 'database setup'
3
-
4
- with_adapters do
5
- it 'defines base view' do
6
- module Test
7
- class Users < ROM::Relation[:sql]
8
- dataset :users
9
- register_as :users
10
- end
11
- end
12
-
13
- conf.register_relation(Test::Users)
14
-
15
- expect(container.relation(:users).base.header).to match_array([:id, :name])
16
- end
17
- end
18
- end