rom-sql 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/Gemfile +1 -0
- data/lib/rom/sql/attribute.rb +1 -1
- data/lib/rom/sql/projection_dsl.rb +6 -0
- data/lib/rom/sql/version.rb +1 -1
- data/rom-sql.gemspec +1 -1
- data/spec/integration/association/many_to_many/custom_fks_spec.rb +9 -13
- data/spec/integration/association/many_to_many/from_view_spec.rb +9 -8
- data/spec/integration/association/many_to_many_spec.rb +103 -102
- data/spec/integration/association/many_to_one/custom_fks_spec.rb +6 -7
- data/spec/integration/association/many_to_one/from_view_spec.rb +8 -4
- data/spec/integration/association/many_to_one_spec.rb +61 -54
- data/spec/integration/association/one_to_many/custom_fks_spec.rb +7 -6
- data/spec/integration/association/one_to_many/from_view_spec.rb +7 -10
- data/spec/integration/association/one_to_many/self_ref_spec.rb +6 -6
- data/spec/integration/association/one_to_many_spec.rb +0 -3
- data/spec/integration/association/one_to_one_spec.rb +17 -11
- data/spec/integration/association/one_to_one_through_spec.rb +3 -5
- data/spec/integration/commands/create_spec.rb +33 -22
- data/spec/integration/commands/update_spec.rb +3 -3
- data/spec/integration/commands/upsert_spec.rb +1 -1
- data/spec/integration/gateway_spec.rb +12 -8
- data/spec/integration/migration_spec.rb +4 -3
- data/spec/integration/plugins/associates/many_to_many_spec.rb +2 -2
- data/spec/integration/plugins/associates_spec.rb +1 -1
- data/spec/integration/relation_schema_spec.rb +4 -5
- data/spec/integration/schema/call_spec.rb +1 -1
- data/spec/integration/schema/inferrer/mysql_spec.rb +22 -23
- data/spec/integration/schema/inferrer/postgres_spec.rb +83 -82
- data/spec/integration/schema/inferrer/sqlite_spec.rb +18 -19
- data/spec/integration/schema/inferrer_spec.rb +54 -33
- data/spec/integration/schema/prefix_spec.rb +9 -11
- data/spec/integration/schema/qualified_spec.rb +9 -11
- data/spec/integration/schema/rename_spec.rb +13 -15
- data/spec/integration/schema/view_spec.rb +2 -2
- data/spec/integration/sequel_api_spec.rb +1 -1
- data/spec/integration/setup_spec.rb +5 -5
- data/spec/integration/support/active_support_notifications_spec.rb +2 -2
- data/spec/integration/support/rails_log_subscriber_spec.rb +2 -2
- data/spec/shared/accounts.rb +44 -0
- data/spec/shared/database_setup.rb +42 -81
- data/spec/shared/notes.rb +21 -0
- data/spec/shared/posts.rb +32 -0
- data/spec/shared/puppies.rb +13 -0
- data/spec/shared/relations.rb +1 -1
- data/spec/shared/users.rb +29 -0
- data/spec/shared/users_and_tasks.rb +32 -18
- data/spec/spec_helper.rb +18 -30
- data/spec/support/env_helper.rb +25 -0
- data/spec/support/oracle/create_users.sql +7 -0
- data/spec/support/oracle/set_sys_passwords.sql +2 -0
- data/spec/unit/plugin/pagination_spec.rb +2 -2
- data/spec/unit/plugin/timestamp_spec.rb +1 -1
- data/spec/unit/projection_dsl_spec.rb +8 -0
- data/spec/unit/relation/group_spec.rb +5 -3
- data/spec/unit/relation/max_spec.rb +1 -1
- data/spec/unit/relation/select_spec.rb +7 -0
- metadata +33 -5
- data/spec/shared/users_and_accounts.rb +0 -10
@@ -0,0 +1,21 @@
|
|
1
|
+
RSpec.shared_context 'notes' do
|
2
|
+
include_context 'database setup'
|
3
|
+
|
4
|
+
before do
|
5
|
+
inferrable_relations.concat %i(notes)
|
6
|
+
end
|
7
|
+
|
8
|
+
before do |example|
|
9
|
+
ctx = self
|
10
|
+
|
11
|
+
conn.create_table :notes do
|
12
|
+
primary_key :id
|
13
|
+
String :text, null: false
|
14
|
+
# TODO: Remove Oracle's workarounds once inferer can infer not-null timestamps
|
15
|
+
DateTime :created_at, null: ctx.oracle?(example)
|
16
|
+
DateTime :updated_at, null: ctx.oracle?(example)
|
17
|
+
DateTime :completed_at
|
18
|
+
Date :written
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
RSpec.shared_context 'posts' do
|
2
|
+
before do
|
3
|
+
inferrable_relations.concat %i(posts)
|
4
|
+
end
|
5
|
+
|
6
|
+
before do |example|
|
7
|
+
conn.create_table :posts do
|
8
|
+
primary_key :post_id
|
9
|
+
foreign_key :author_id, :users
|
10
|
+
String :title
|
11
|
+
String :body
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
before do |example|
|
16
|
+
next if example.metadata[:seeds] == false
|
17
|
+
|
18
|
+
conn[:posts].insert(
|
19
|
+
post_id: 1,
|
20
|
+
author_id: 2,
|
21
|
+
title: "Joe's post",
|
22
|
+
body: 'Joe wrote sutin'
|
23
|
+
)
|
24
|
+
|
25
|
+
conn[:posts].insert(
|
26
|
+
post_id: 2,
|
27
|
+
author_id: 1,
|
28
|
+
title: "Jane's post",
|
29
|
+
body: 'Jane wrote sutin'
|
30
|
+
)
|
31
|
+
end
|
32
|
+
end
|
data/spec/shared/relations.rb
CHANGED
@@ -0,0 +1,29 @@
|
|
1
|
+
RSpec.shared_context 'users' do
|
2
|
+
include_context 'database setup'
|
3
|
+
|
4
|
+
before do
|
5
|
+
inferrable_relations.concat %i(users)
|
6
|
+
end
|
7
|
+
|
8
|
+
let(:users) { container.relations[:users] }
|
9
|
+
|
10
|
+
let(:jane_id) { 1 }
|
11
|
+
let(:joe_id) { 2 }
|
12
|
+
|
13
|
+
before do |example|
|
14
|
+
ctx = self
|
15
|
+
|
16
|
+
conn.create_table :users do
|
17
|
+
primary_key :id
|
18
|
+
String :name, null: false
|
19
|
+
check { char_length(name) > 2 } if ctx.postgres?(example)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
before do |example|
|
24
|
+
next if example.metadata[:seeds] == false
|
25
|
+
|
26
|
+
conn[:users].insert name: 'Jane'
|
27
|
+
conn[:users].insert name: 'Joe'
|
28
|
+
end
|
29
|
+
end
|
@@ -1,28 +1,42 @@
|
|
1
|
-
shared_context 'users and tasks' do
|
2
|
-
include_context '
|
1
|
+
RSpec.shared_context 'users and tasks' do
|
2
|
+
include_context 'users'
|
3
|
+
|
4
|
+
let(:tasks) { container.relations[:tasks] }
|
3
5
|
|
4
6
|
before do
|
5
|
-
|
6
|
-
|
7
|
+
inferrable_relations.concat %i(tasks tags task_tags)
|
8
|
+
end
|
9
|
+
|
10
|
+
before do |example|
|
11
|
+
ctx = self
|
12
|
+
|
13
|
+
conn.create_table :tasks do
|
14
|
+
primary_key :id
|
15
|
+
foreign_key :user_id, :users
|
16
|
+
String :title, unique: true
|
17
|
+
constraint(:title_length) { char_length(title) > 1 } if ctx.postgres?(example)
|
18
|
+
constraint(:title_length) { length(title) > 1 } if ctx.sqlite?(example)
|
19
|
+
end
|
20
|
+
|
21
|
+
conn.create_table :tags do
|
22
|
+
primary_key :id
|
23
|
+
String :name
|
24
|
+
end
|
25
|
+
|
26
|
+
conn.create_table :task_tags do
|
27
|
+
primary_key [:tag_id, :task_id]
|
28
|
+
Integer :tag_id
|
29
|
+
Integer :task_id
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
before do |example|
|
34
|
+
next if example.metadata[:seeds] == false
|
7
35
|
|
8
36
|
conn[:tasks].insert id: 1, user_id: 2, title: "Joe's task"
|
9
37
|
conn[:tasks].insert id: 2, user_id: 1, title: "Jane's task"
|
10
38
|
|
11
39
|
conn[:tags].insert id: 1, name: 'important'
|
12
40
|
conn[:task_tags].insert(tag_id: 1, task_id: 1)
|
13
|
-
|
14
|
-
conn[:posts].insert(
|
15
|
-
post_id: 1,
|
16
|
-
author_id: 2,
|
17
|
-
title: "Joe's post",
|
18
|
-
body: 'Joe wrote sutin'
|
19
|
-
)
|
20
|
-
|
21
|
-
conn[:posts].insert(
|
22
|
-
post_id: 2,
|
23
|
-
author_id: 1,
|
24
|
-
title: "Jane's post",
|
25
|
-
body: 'Jane wrote sutin'
|
26
|
-
)
|
27
41
|
end
|
28
42
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -21,22 +21,32 @@ rescue LoadError
|
|
21
21
|
end
|
22
22
|
|
23
23
|
LOGGER = Logger.new(File.open('./log/test.log', 'a'))
|
24
|
+
ENV['TZ'] ||= 'UTC'
|
25
|
+
|
26
|
+
oracle_settings = {
|
27
|
+
db_name: ENV.fetch('ROM_ORACLE_DATABASE', 'xe'),
|
28
|
+
host: ENV.fetch('ROM_ORACLE_HOST', 'localhost'),
|
29
|
+
port: Integer(ENV.fetch('ROM_ORACLE_PORT', '1521'))
|
30
|
+
}
|
24
31
|
|
25
32
|
if defined? JRUBY_VERSION
|
26
33
|
DB_URIS = {
|
27
34
|
sqlite: 'jdbc:sqlite:::memory',
|
28
35
|
postgres: 'jdbc:postgresql://localhost/rom_sql',
|
29
|
-
mysql: 'jdbc:mysql://localhost/rom_sql?user=root'
|
36
|
+
mysql: 'jdbc:mysql://localhost/rom_sql?user=root',
|
37
|
+
oracle: ENV['ROM_USE_ORACLE'] ? fail('Setup Oracle for JRuby!') : nil
|
30
38
|
}
|
31
39
|
else
|
32
40
|
DB_URIS = {
|
33
41
|
sqlite: 'sqlite::memory',
|
34
42
|
postgres: 'postgres://localhost/rom_sql',
|
35
|
-
mysql: 'mysql2://root@localhost/rom_sql'
|
43
|
+
mysql: 'mysql2://root@localhost/rom_sql',
|
44
|
+
oracle: "oracle://#{ oracle_settings[:host] }:#{ oracle_settings[:port] }/" \
|
45
|
+
"#{ oracle_settings[:db_name] }?username=rom_sql&password=rom_sql&autosequence=true"
|
36
46
|
}
|
37
47
|
end
|
38
48
|
|
39
|
-
ADAPTERS = DB_URIS.keys
|
49
|
+
ADAPTERS = ENV['ROM_USE_ORACLE'] ? DB_URIS.keys : DB_URIS.keys - %i(oracle)
|
40
50
|
PG_LTE_95 = ENV.fetch('PG_LTE_95', 'true') == 'true'
|
41
51
|
|
42
52
|
SPEC_ROOT = root = Pathname(__FILE__).dirname
|
@@ -49,9 +59,6 @@ class ROM::SQL::Schema::Inferrer
|
|
49
59
|
end
|
50
60
|
end
|
51
61
|
|
52
|
-
Dir[root.join('shared/**/*')].each { |f| require f }
|
53
|
-
Dir[root.join('support/**/*')].each { |f| require f }
|
54
|
-
|
55
62
|
require 'dry/core/deprecations'
|
56
63
|
Dry::Core::Deprecations.set_logger!(root.join('../log/deprecations.log'))
|
57
64
|
|
@@ -62,30 +69,8 @@ module Types
|
|
62
69
|
include Dry::Types.module
|
63
70
|
end
|
64
71
|
|
65
|
-
module ENVHelper
|
66
|
-
def db?(type, example)
|
67
|
-
example.metadata[type]
|
68
|
-
end
|
69
|
-
|
70
|
-
def postgres?(example)
|
71
|
-
db?(:postgres, example)
|
72
|
-
end
|
73
|
-
|
74
|
-
def mysql?(example)
|
75
|
-
db?(:mysql, example)
|
76
|
-
end
|
77
|
-
|
78
|
-
def sqlite?(example)
|
79
|
-
db?(:sqlite, example)
|
80
|
-
end
|
81
|
-
|
82
|
-
def jruby?
|
83
|
-
defined? JRUBY_VERSION
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
72
|
def with_adapters(*args, &block)
|
88
|
-
reset_adapter = {
|
73
|
+
reset_adapter = Hash[*ADAPTERS.flat_map { |a| [a, false] }]
|
89
74
|
adapters = args.empty? || args[0] == :all ? ADAPTERS : args
|
90
75
|
|
91
76
|
adapters.each do |adapter|
|
@@ -96,7 +81,6 @@ end
|
|
96
81
|
RSpec.configure do |config|
|
97
82
|
config.disable_monkey_patching!
|
98
83
|
|
99
|
-
config.include ENVHelper
|
100
84
|
|
101
85
|
config.before(:suite) do
|
102
86
|
tmp_test_dir = TMP_PATH.join('test')
|
@@ -113,5 +97,9 @@ RSpec.configure do |config|
|
|
113
97
|
Object.send(:remove_const, :Test)
|
114
98
|
end
|
115
99
|
|
100
|
+
Dir[root.join('shared/**/*.rb')].each { |f| require f }
|
101
|
+
Dir[root.join('support/**/*.rb')].each { |f| require f }
|
102
|
+
|
116
103
|
config.include(Helpers, helpers: true)
|
104
|
+
config.include ENVHelper
|
117
105
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module ENVHelper
|
2
|
+
def db?(type, example)
|
3
|
+
example.metadata[type]
|
4
|
+
end
|
5
|
+
|
6
|
+
def postgres?(example)
|
7
|
+
db?(:postgres, example)
|
8
|
+
end
|
9
|
+
|
10
|
+
def mysql?(example)
|
11
|
+
db?(:mysql, example)
|
12
|
+
end
|
13
|
+
|
14
|
+
def sqlite?(example)
|
15
|
+
db?(:sqlite, example)
|
16
|
+
end
|
17
|
+
|
18
|
+
def oracle?(example)
|
19
|
+
db?(:oracle, example)
|
20
|
+
end
|
21
|
+
|
22
|
+
def jruby?
|
23
|
+
defined? JRUBY_VERSION
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
alter database default tablespace USERS;
|
2
|
+
|
3
|
+
CREATE USER rom_sql IDENTIFIED BY rom_sql;
|
4
|
+
|
5
|
+
GRANT unlimited tablespace, create session, create table, create sequence,
|
6
|
+
create procedure, create trigger, create view, create materialized view,
|
7
|
+
create database link, create synonym, create type, ctxapp TO rom_sql;
|
@@ -63,6 +63,14 @@ RSpec.describe ROM::SQL::ProjectionDSL, :postgres, helpers: true do
|
|
63
63
|
|
64
64
|
expect(literals).to eql([%(COUNT("users"."id") AS "count")])
|
65
65
|
end
|
66
|
+
|
67
|
+
it 'supports selecting literal strings' do
|
68
|
+
literals = dsl
|
69
|
+
.call { `'event'`.as(:type) }
|
70
|
+
.map { |attr| attr.sql_literal(ds) }
|
71
|
+
|
72
|
+
expect(literals).to eql([%('event' AS "type")])
|
73
|
+
end
|
66
74
|
end
|
67
75
|
|
68
76
|
describe '#method_missing' do
|
@@ -6,11 +6,13 @@ RSpec.describe ROM::Relation, '#group' do
|
|
6
6
|
include_context 'users and tasks'
|
7
7
|
|
8
8
|
with_adapters do
|
9
|
-
it 'groups by provided attribute name' do
|
9
|
+
it 'groups by provided attribute name' do |example|
|
10
|
+
# Oracle doesn't support concise GROUP BY
|
11
|
+
group_by = oracle?(example) ? %i(id name) : %i(id)
|
10
12
|
grouped = relation.
|
11
13
|
qualified.
|
12
14
|
left_join(:tasks, tasks[:user_id].qualified => relation[:id].qualified).
|
13
|
-
group(
|
15
|
+
group(*group_by)
|
14
16
|
|
15
17
|
expect(grouped.to_a).to eql([{ id: 1, name: 'Jane' }, { id: 2, name: 'Joe'}])
|
16
18
|
end
|
@@ -19,7 +21,7 @@ RSpec.describe ROM::Relation, '#group' do
|
|
19
21
|
grouped = relation.
|
20
22
|
qualified.
|
21
23
|
left_join(:tasks, tasks[:user_id].qualified => relation[:id].qualified).
|
22
|
-
group { id.qualified }
|
24
|
+
group { [id.qualified, name.qualified] }
|
23
25
|
|
24
26
|
expect(grouped.to_a).to eql([{ id: 1, name: 'Jane' }, { id: 2, name: 'Joe'}])
|
25
27
|
end
|
@@ -29,5 +29,12 @@ RSpec.describe ROM::Relation, '#select' do
|
|
29
29
|
it 'supports blocks' do
|
30
30
|
expect(relation.select { [id, title] }.schema.map(&:name)).to eql(%i[id title])
|
31
31
|
end
|
32
|
+
|
33
|
+
it 'supports selecting literal strings' do
|
34
|
+
new_rel = relation.select { `'event'`.as(:type) }
|
35
|
+
|
36
|
+
expect(new_rel.schema[:type].primitive).to be(String)
|
37
|
+
expect(new_rel.first).to eql(type: 'event')
|
38
|
+
end
|
32
39
|
end
|
33
40
|
end
|
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: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Solnica
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-02-
|
11
|
+
date: 2017-02-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sequel
|
@@ -120,6 +120,20 @@ dependencies:
|
|
120
120
|
- - "~>"
|
121
121
|
- !ruby/object:Gem::Version
|
122
122
|
version: '10.0'
|
123
|
+
- !ruby/object:Gem::Dependency
|
124
|
+
name: rspec
|
125
|
+
requirement: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - "~>"
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '3.5'
|
130
|
+
type: :development
|
131
|
+
prerelease: false
|
132
|
+
version_requirements: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - "~>"
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '3.5'
|
123
137
|
description: SQL databases support for ROM
|
124
138
|
email:
|
125
139
|
- piotr.solnica@gmail.com
|
@@ -242,12 +256,19 @@ files:
|
|
242
256
|
- spec/integration/setup_spec.rb
|
243
257
|
- spec/integration/support/active_support_notifications_spec.rb
|
244
258
|
- spec/integration/support/rails_log_subscriber_spec.rb
|
259
|
+
- spec/shared/accounts.rb
|
245
260
|
- spec/shared/database_setup.rb
|
261
|
+
- spec/shared/notes.rb
|
262
|
+
- spec/shared/posts.rb
|
263
|
+
- spec/shared/puppies.rb
|
246
264
|
- spec/shared/relations.rb
|
247
|
-
- spec/shared/
|
265
|
+
- spec/shared/users.rb
|
248
266
|
- spec/shared/users_and_tasks.rb
|
249
267
|
- spec/spec_helper.rb
|
268
|
+
- spec/support/env_helper.rb
|
250
269
|
- spec/support/helpers.rb
|
270
|
+
- spec/support/oracle/create_users.sql
|
271
|
+
- spec/support/oracle/set_sys_passwords.sql
|
251
272
|
- spec/unit/association/many_to_many_spec.rb
|
252
273
|
- spec/unit/association/many_to_one_spec.rb
|
253
274
|
- spec/unit/association/name_spec.rb
|
@@ -320,7 +341,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
320
341
|
version: '0'
|
321
342
|
requirements: []
|
322
343
|
rubyforge_project:
|
323
|
-
rubygems_version: 2.
|
344
|
+
rubygems_version: 2.6.9
|
324
345
|
signing_key:
|
325
346
|
specification_version: 4
|
326
347
|
summary: SQL databases support for ROM
|
@@ -366,12 +387,19 @@ test_files:
|
|
366
387
|
- spec/integration/setup_spec.rb
|
367
388
|
- spec/integration/support/active_support_notifications_spec.rb
|
368
389
|
- spec/integration/support/rails_log_subscriber_spec.rb
|
390
|
+
- spec/shared/accounts.rb
|
369
391
|
- spec/shared/database_setup.rb
|
392
|
+
- spec/shared/notes.rb
|
393
|
+
- spec/shared/posts.rb
|
394
|
+
- spec/shared/puppies.rb
|
370
395
|
- spec/shared/relations.rb
|
371
|
-
- spec/shared/
|
396
|
+
- spec/shared/users.rb
|
372
397
|
- spec/shared/users_and_tasks.rb
|
373
398
|
- spec/spec_helper.rb
|
399
|
+
- spec/support/env_helper.rb
|
374
400
|
- spec/support/helpers.rb
|
401
|
+
- spec/support/oracle/create_users.sql
|
402
|
+
- spec/support/oracle/set_sys_passwords.sql
|
375
403
|
- spec/unit/association/many_to_many_spec.rb
|
376
404
|
- spec/unit/association/many_to_one_spec.rb
|
377
405
|
- spec/unit/association/name_spec.rb
|