rom-sql 2.0.0.beta2 → 2.0.0.beta3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +66 -0
- data/lib/rom/plugins/relation/sql/postgres/explain.rb +54 -0
- data/lib/rom/sql.rb +1 -1
- data/lib/rom/sql/attribute.rb +17 -18
- data/lib/rom/sql/errors.rb +3 -0
- data/lib/rom/sql/extensions/mysql.rb +1 -1
- data/lib/rom/sql/extensions/mysql/type_builder.rb +28 -0
- data/lib/rom/sql/extensions/postgres.rb +3 -1
- data/lib/rom/sql/extensions/postgres/commands.rb +30 -13
- data/lib/rom/sql/extensions/postgres/{attributes_inferrer.rb → type_builder.rb} +24 -28
- data/lib/rom/sql/extensions/postgres/type_serializer.rb +39 -0
- data/lib/rom/sql/extensions/postgres/types.rb +24 -477
- data/lib/rom/sql/extensions/postgres/types/array.rb +163 -0
- data/lib/rom/sql/extensions/postgres/types/geometric.rb +135 -0
- data/lib/rom/sql/extensions/postgres/types/json.rb +235 -0
- data/lib/rom/sql/extensions/postgres/types/network.rb +15 -0
- data/lib/rom/sql/extensions/sqlite.rb +1 -1
- data/lib/rom/sql/extensions/sqlite/{attributes_inferrer.rb → type_builder.rb} +5 -5
- data/lib/rom/sql/extensions/sqlite/types.rb +8 -3
- data/lib/rom/sql/foreign_key.rb +17 -0
- data/lib/rom/sql/function.rb +86 -8
- data/lib/rom/sql/gateway.rb +26 -26
- data/lib/rom/sql/index.rb +4 -0
- data/lib/rom/sql/migration.rb +3 -3
- data/lib/rom/sql/migration/inline_runner.rb +9 -83
- data/lib/rom/sql/migration/migrator.rb +35 -12
- data/lib/rom/sql/migration/recorder.rb +21 -0
- data/lib/rom/sql/migration/runner.rb +115 -0
- data/lib/rom/sql/migration/schema_diff.rb +108 -53
- data/lib/rom/sql/migration/writer.rb +61 -0
- data/lib/rom/sql/relation.rb +2 -1
- data/lib/rom/sql/relation/reading.rb +63 -3
- data/lib/rom/sql/relation/writing.rb +38 -0
- data/lib/rom/sql/schema.rb +9 -3
- data/lib/rom/sql/schema/attributes_inferrer.rb +3 -119
- data/lib/rom/sql/schema/inferrer.rb +99 -18
- data/lib/rom/sql/schema/type_builder.rb +94 -0
- data/lib/rom/sql/type_dsl.rb +30 -0
- data/lib/rom/sql/type_extensions.rb +11 -6
- data/lib/rom/sql/type_serializer.rb +46 -0
- data/lib/rom/sql/types.rb +12 -0
- data/lib/rom/sql/version.rb +1 -1
- metadata +26 -244
- data/.codeclimate.yml +0 -15
- data/.gitignore +0 -17
- data/.rspec +0 -3
- data/.travis.yml +0 -39
- data/.yardopts +0 -2
- data/Gemfile +0 -33
- data/Guardfile +0 -24
- data/LICENSE.txt +0 -22
- data/Rakefile +0 -19
- data/circle.yml +0 -10
- data/lib/rom/sql/extensions/mysql/attributes_inferrer.rb +0 -10
- data/lib/rom/sql/relation/sequel_api.rb +0 -133
- data/log/.gitkeep +0 -0
- data/rom-sql.gemspec +0 -29
- data/spec/extensions/postgres/attribute_spec.rb +0 -217
- data/spec/extensions/postgres/integration_spec.rb +0 -59
- data/spec/extensions/postgres/types_spec.rb +0 -252
- data/spec/extensions/sqlite/types_spec.rb +0 -11
- data/spec/fixtures/migrations/20150403090603_create_carrots.rb +0 -8
- data/spec/integration/associations/many_to_many/custom_fks_spec.rb +0 -76
- data/spec/integration/associations/many_to_many/from_view_spec.rb +0 -88
- data/spec/integration/associations/many_to_many_spec.rb +0 -162
- data/spec/integration/associations/many_to_one/custom_fks_spec.rb +0 -64
- data/spec/integration/associations/many_to_one/from_view_spec.rb +0 -84
- data/spec/integration/associations/many_to_one/self_ref_spec.rb +0 -53
- data/spec/integration/associations/many_to_one_spec.rb +0 -117
- data/spec/integration/associations/one_to_many/custom_fks_spec.rb +0 -54
- data/spec/integration/associations/one_to_many/from_view_spec.rb +0 -57
- data/spec/integration/associations/one_to_many/self_ref_spec.rb +0 -54
- data/spec/integration/associations/one_to_many_spec.rb +0 -86
- data/spec/integration/associations/one_to_one_spec.rb +0 -69
- data/spec/integration/associations/one_to_one_through_spec.rb +0 -92
- data/spec/integration/auto_migrations/errors_spec.rb +0 -31
- data/spec/integration/auto_migrations/indexes_spec.rb +0 -253
- data/spec/integration/auto_migrations/managing_columns_spec.rb +0 -156
- data/spec/integration/auto_migrations/postgres/column_types_spec.rb +0 -63
- data/spec/integration/combine_with_spec.rb +0 -43
- data/spec/integration/commands/create_spec.rb +0 -304
- data/spec/integration/commands/delete_spec.rb +0 -84
- data/spec/integration/commands/update_spec.rb +0 -90
- data/spec/integration/commands/upsert_spec.rb +0 -83
- data/spec/integration/gateway_spec.rb +0 -107
- data/spec/integration/migration_spec.rb +0 -55
- data/spec/integration/plugins/associates/many_to_many_spec.rb +0 -69
- data/spec/integration/plugins/associates_spec.rb +0 -250
- data/spec/integration/plugins/auto_restrictions_spec.rb +0 -74
- data/spec/integration/relation_schema_spec.rb +0 -271
- data/spec/integration/schema/call_spec.rb +0 -24
- data/spec/integration/schema/inferrer/mysql_spec.rb +0 -45
- data/spec/integration/schema/inferrer/postgres_spec.rb +0 -203
- data/spec/integration/schema/inferrer/sqlite_spec.rb +0 -37
- data/spec/integration/schema/inferrer_spec.rb +0 -390
- data/spec/integration/schema/prefix_spec.rb +0 -16
- data/spec/integration/schema/qualified_spec.rb +0 -16
- data/spec/integration/schema/rename_spec.rb +0 -21
- data/spec/integration/schema/view_spec.rb +0 -29
- data/spec/integration/sequel_api_spec.rb +0 -36
- data/spec/integration/setup_spec.rb +0 -26
- data/spec/integration/support/active_support_notifications_spec.rb +0 -24
- data/spec/integration/support/rails_log_subscriber_spec.rb +0 -30
- data/spec/integration/wrap_spec.rb +0 -91
- data/spec/shared/accounts.rb +0 -48
- data/spec/shared/database_setup.rb +0 -70
- data/spec/shared/notes.rb +0 -23
- data/spec/shared/posts.rb +0 -34
- data/spec/shared/puppies.rb +0 -15
- data/spec/shared/relations.rb +0 -8
- data/spec/shared/users.rb +0 -32
- data/spec/shared/users_and_tasks.rb +0 -50
- data/spec/spec_helper.rb +0 -122
- data/spec/support/env_helper.rb +0 -25
- data/spec/support/helpers.rb +0 -24
- data/spec/support/oracle/create_users.sql +0 -7
- data/spec/support/oracle/set_sys_passwords.sql +0 -2
- data/spec/support/test_configuration.rb +0 -16
- data/spec/unit/attribute_spec.rb +0 -104
- data/spec/unit/function_spec.rb +0 -48
- data/spec/unit/gateway_spec.rb +0 -70
- data/spec/unit/logger_spec.rb +0 -14
- data/spec/unit/migration_tasks_spec.rb +0 -111
- data/spec/unit/migrator_spec.rb +0 -25
- data/spec/unit/order_dsl_spec.rb +0 -43
- data/spec/unit/plugin/associates_spec.rb +0 -94
- data/spec/unit/plugin/pagination_spec.rb +0 -91
- data/spec/unit/plugin/timestamp_spec.rb +0 -117
- data/spec/unit/projection_dsl_spec.rb +0 -110
- data/spec/unit/relation/assoc_spec.rb +0 -87
- data/spec/unit/relation/associations_spec.rb +0 -27
- data/spec/unit/relation/avg_spec.rb +0 -11
- data/spec/unit/relation/by_pk_spec.rb +0 -62
- data/spec/unit/relation/dataset_spec.rb +0 -50
- data/spec/unit/relation/distinct_spec.rb +0 -15
- data/spec/unit/relation/exclude_spec.rb +0 -11
- data/spec/unit/relation/exist_predicate_spec.rb +0 -25
- data/spec/unit/relation/exists_spec.rb +0 -18
- data/spec/unit/relation/fetch_spec.rb +0 -21
- data/spec/unit/relation/group_spec.rb +0 -61
- data/spec/unit/relation/having_spec.rb +0 -22
- data/spec/unit/relation/inner_join_spec.rb +0 -158
- data/spec/unit/relation/inspect_spec.rb +0 -11
- data/spec/unit/relation/instrument_spec.rb +0 -45
- data/spec/unit/relation/invert_spec.rb +0 -11
- data/spec/unit/relation/left_join_spec.rb +0 -55
- data/spec/unit/relation/lock_spec.rb +0 -93
- data/spec/unit/relation/map_spec.rb +0 -16
- data/spec/unit/relation/max_spec.rb +0 -11
- data/spec/unit/relation/min_spec.rb +0 -11
- data/spec/unit/relation/order_spec.rb +0 -51
- data/spec/unit/relation/pluck_spec.rb +0 -11
- data/spec/unit/relation/prefix_spec.rb +0 -29
- data/spec/unit/relation/primary_key_spec.rb +0 -27
- data/spec/unit/relation/project_spec.rb +0 -24
- data/spec/unit/relation/qualified_columns_spec.rb +0 -30
- data/spec/unit/relation/qualified_spec.rb +0 -25
- data/spec/unit/relation/read_spec.rb +0 -25
- data/spec/unit/relation/rename_spec.rb +0 -23
- data/spec/unit/relation/right_join_spec.rb +0 -57
- data/spec/unit/relation/select_append_spec.rb +0 -21
- data/spec/unit/relation/select_spec.rb +0 -40
- data/spec/unit/relation/sum_spec.rb +0 -11
- data/spec/unit/relation/union_spec.rb +0 -19
- data/spec/unit/relation/unique_predicate_spec.rb +0 -18
- data/spec/unit/relation/where_spec.rb +0 -133
- data/spec/unit/restriction_dsl_spec.rb +0 -34
- data/spec/unit/schema_spec.rb +0 -25
- data/spec/unit/types_spec.rb +0 -65
@@ -1,74 +0,0 @@
|
|
1
|
-
RSpec.describe 'Plugins / :auto_restrictions', seeds: true do
|
2
|
-
include_context 'users and tasks'
|
3
|
-
|
4
|
-
with_adapters do
|
5
|
-
before do
|
6
|
-
conn.add_index :tasks, :title, unique: true
|
7
|
-
end
|
8
|
-
|
9
|
-
shared_context 'auto-generated restriction view' do
|
10
|
-
it 'defines restriction views for all indexed attributes' do
|
11
|
-
expect(tasks.select(:id).by_title("Jane's task").one).to eql(id: 2)
|
12
|
-
end
|
13
|
-
|
14
|
-
it 'defines curried methods' do
|
15
|
-
expect(tasks.by_title.("Jane's task").first).to eql(id: 2, user_id: 1, title: "Jane's task")
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
context 'with an inferred schema' do
|
20
|
-
before do
|
21
|
-
conf.plugin(:sql, relations: :auto_restrictions)
|
22
|
-
end
|
23
|
-
|
24
|
-
include_context 'auto-generated restriction view'
|
25
|
-
end
|
26
|
-
|
27
|
-
context 'with explicit schema' do
|
28
|
-
before do
|
29
|
-
conf.relation(:tasks) do
|
30
|
-
schema do
|
31
|
-
attribute :id, ROM::SQL::Types::Serial
|
32
|
-
attribute :user_id, ROM::SQL::Types::Int
|
33
|
-
attribute :title, ROM::SQL::Types::String.meta(index: true)
|
34
|
-
|
35
|
-
indexes do
|
36
|
-
index :user_id, :title
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
use :auto_restrictions
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
include_context 'auto-generated restriction view'
|
45
|
-
|
46
|
-
it 'generates restrictrions by a composite index' do
|
47
|
-
expect(tasks.by_user_id_and_title(1, "Jane's task").first).to eql(id: 2, user_id: 1, title: "Jane's task")
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
if metadata[:postgres]
|
52
|
-
# An auto-generated restriction should include the prediate from the index definition
|
53
|
-
# but it seems to be too much from my POV, better leave it to the user
|
54
|
-
# Note that this can be enabled later
|
55
|
-
it 'skips partial indexes' do
|
56
|
-
conf.relation(:tasks) do
|
57
|
-
schema do
|
58
|
-
attribute :id, ROM::SQL::Types::Serial
|
59
|
-
attribute :user_id, ROM::SQL::Types::Int
|
60
|
-
attribute :title, ROM::SQL::Types::String
|
61
|
-
|
62
|
-
indexes do
|
63
|
-
index :title, predicate: 'title is not null'
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
use :auto_restrictions
|
68
|
-
end
|
69
|
-
|
70
|
-
expect(tasks).not_to respond_to(:by_title)
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
@@ -1,271 +0,0 @@
|
|
1
|
-
RSpec.describe 'Inferring schema from database' do
|
2
|
-
include_context 'users'
|
3
|
-
include_context 'posts'
|
4
|
-
|
5
|
-
with_adapters do
|
6
|
-
context "when database schema exists" do
|
7
|
-
it "infers the schema from the database relations" do
|
8
|
-
conf.relation(:users)
|
9
|
-
|
10
|
-
expect(container.relations.users.to_a)
|
11
|
-
.to eql(container.gateways[:default][:users].to_a)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
context "for empty database schemas" do
|
16
|
-
it "returns an empty schema" do
|
17
|
-
expect { container.users }.to raise_error(NoMethodError)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
context 'defining associations', seeds: false do
|
22
|
-
let(:config) { TestConfiguration.new(:sql, conn) }
|
23
|
-
let(:container) { ROM.container(config) }
|
24
|
-
|
25
|
-
let(:user_associations) do
|
26
|
-
config.relation(:accounts) { schema(infer: true) }
|
27
|
-
config.relation(:cards) { schema(infer: true) }
|
28
|
-
config.register_relation(Test::Users)
|
29
|
-
container.relations[:users].associations
|
30
|
-
end
|
31
|
-
|
32
|
-
let(:post_associations) do
|
33
|
-
config.relation(:tags) { schema(infer: true) }
|
34
|
-
config.relation(:posts_tags) { schema(infer: true) }
|
35
|
-
config.register_relation(Test::Posts)
|
36
|
-
container.relations[:posts].associations
|
37
|
-
end
|
38
|
-
|
39
|
-
let(:tag_associations) do
|
40
|
-
config.relation(:posts) { schema(infer: true) }
|
41
|
-
config.relation(:posts_tags) { schema(infer: true) }
|
42
|
-
config.register_relation(Test::Tags)
|
43
|
-
container.relations[:tags].associations
|
44
|
-
end
|
45
|
-
|
46
|
-
it "allows defining a one-to-many" do
|
47
|
-
class Test::Posts < ROM::Relation[:sql]
|
48
|
-
schema(:posts) do
|
49
|
-
associations do
|
50
|
-
one_to_many :tags
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
assoc = ROM::Associations::Definitions::OneToMany.new(:posts, :tags)
|
56
|
-
|
57
|
-
expect(post_associations[:tags].definition).to eql(assoc)
|
58
|
-
end
|
59
|
-
|
60
|
-
it "allows defining a one-to-many using has_many shortcut" do
|
61
|
-
class Test::Posts < ROM::Relation[:sql]
|
62
|
-
schema(:posts) do
|
63
|
-
associations do
|
64
|
-
has_many :tags
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
assoc = ROM::Associations::Definitions::OneToMany.new(:posts, :tags)
|
70
|
-
|
71
|
-
expect(post_associations[:tags].definition).to eql(assoc)
|
72
|
-
end
|
73
|
-
|
74
|
-
it "allows defining a one-to-one" do
|
75
|
-
class Test::Users < ROM::Relation[:sql]
|
76
|
-
schema(:users) do
|
77
|
-
associations do
|
78
|
-
one_to_one :accounts
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
assoc = ROM::Associations::Definitions::OneToOne.new(:users, :accounts)
|
84
|
-
|
85
|
-
expect(user_associations[:accounts].definition).to eql(assoc)
|
86
|
-
end
|
87
|
-
|
88
|
-
it "allows defining a one-to-one using has_one shortcut" do
|
89
|
-
class Test::Users < ROM::Relation[:sql]
|
90
|
-
schema(:users) do
|
91
|
-
associations do
|
92
|
-
has_one :account
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
assoc = ROM::Associations::Definitions::OneToOne.new(:users, :accounts, as: :account)
|
98
|
-
|
99
|
-
expect(user_associations[:account].definition).to eql(assoc)
|
100
|
-
expect(user_associations[:account].definition.target).to be_aliased
|
101
|
-
end
|
102
|
-
|
103
|
-
it "allows defining a one-to-one using has_one shortcut with an alias" do
|
104
|
-
class Test::Users < ROM::Relation[:sql]
|
105
|
-
schema(:users) do
|
106
|
-
associations do
|
107
|
-
has_one :account, as: :user_account
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
assoc = ROM::Associations::Definitions::OneToOne.new(:users, :accounts, as: :user_account)
|
113
|
-
|
114
|
-
expect(user_associations[:user_account].definition).to eql(assoc)
|
115
|
-
expect(user_associations[:user_account].definition.target).to be_aliased
|
116
|
-
end
|
117
|
-
|
118
|
-
it "allows defining a one-to-one-through" do
|
119
|
-
class Test::Users < ROM::Relation[:sql]
|
120
|
-
schema(:users) do
|
121
|
-
associations do
|
122
|
-
one_to_one :cards, through: :accounts
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
assoc = ROM::Associations::Definitions::OneToOneThrough.new(:users, :cards, through: :accounts)
|
128
|
-
|
129
|
-
expect(user_associations[:cards].definition).to eql(assoc)
|
130
|
-
end
|
131
|
-
|
132
|
-
it "allows defining a many-to-one" do
|
133
|
-
class Test::Tags < ROM::Relation[:sql]
|
134
|
-
schema(:tags) do
|
135
|
-
associations do
|
136
|
-
many_to_one :posts
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
assoc = ROM::Associations::Definitions::ManyToOne.new(:tags, :posts)
|
142
|
-
|
143
|
-
expect(tag_associations[:posts].definition).to eql(assoc)
|
144
|
-
end
|
145
|
-
|
146
|
-
it "allows defining a many-to-one using belongs_to shortcut" do
|
147
|
-
class Test::Tags < ROM::Relation[:sql]
|
148
|
-
schema(:tags) do
|
149
|
-
associations do
|
150
|
-
belongs_to :post
|
151
|
-
end
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
|
-
assoc = ROM::Associations::Definitions::ManyToOne.new(:tags, :posts, as: :post)
|
156
|
-
|
157
|
-
expect(tag_associations[:post].definition).to eql(assoc)
|
158
|
-
end
|
159
|
-
|
160
|
-
it "allows defining a many-to-one using belongs_to shortcut" do
|
161
|
-
class Test::Tags < ROM::Relation[:sql]
|
162
|
-
schema(:tags) do
|
163
|
-
associations do
|
164
|
-
belongs_to :post, as: :post_tag
|
165
|
-
end
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
assoc = ROM::Associations::Definitions::ManyToOne.new(:tags, :posts, as: :post_tag)
|
170
|
-
|
171
|
-
expect(tag_associations[:post_tag].definition).to eql(assoc)
|
172
|
-
end
|
173
|
-
|
174
|
-
it "allows defining a many-to-many" do
|
175
|
-
class Test::Posts < ROM::Relation[:sql]
|
176
|
-
schema(:posts) do
|
177
|
-
associations do
|
178
|
-
one_to_many :tags, through: :posts_tags
|
179
|
-
end
|
180
|
-
end
|
181
|
-
end
|
182
|
-
|
183
|
-
assoc = ROM::Associations::Definitions::ManyToMany.new(:posts, :tags, through: :posts_tags)
|
184
|
-
|
185
|
-
expect(post_associations[:tags].definition).to eql(assoc)
|
186
|
-
end
|
187
|
-
|
188
|
-
it "allows defining a many-to-one with a custom name" do
|
189
|
-
class Test::Tags < ROM::Relation[:sql]
|
190
|
-
schema(:tags) do
|
191
|
-
associations do
|
192
|
-
many_to_one :posts, as: :published_posts
|
193
|
-
end
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
assoc = ROM::Associations::Definitions::ManyToOne.new(:tags, :posts, as: :published_posts)
|
198
|
-
|
199
|
-
expect(tag_associations[:published_posts].definition).to eql(assoc)
|
200
|
-
end
|
201
|
-
end
|
202
|
-
|
203
|
-
context 'defining indexes', :helpers do |ctx|
|
204
|
-
it 'allows defining indexes' do
|
205
|
-
class Test::Tags < ROM::Relation[:sql]
|
206
|
-
schema(:tags) do
|
207
|
-
attribute :id, Types::Serial
|
208
|
-
attribute :name, Types::String
|
209
|
-
attribute :created_at, Types::Time
|
210
|
-
attribute :updated_at, Types::Time
|
211
|
-
|
212
|
-
indexes do
|
213
|
-
index :name
|
214
|
-
index :created_at, :name
|
215
|
-
index :updated_at, name: :recently_idx
|
216
|
-
index :created_at, name: :unique_date, unique: true
|
217
|
-
end
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
conf.register_relation(Test::Tags)
|
222
|
-
schema = container.relations[:tags].schema
|
223
|
-
|
224
|
-
expect(schema.indexes.to_a).
|
225
|
-
to contain_exactly(
|
226
|
-
ROM::SQL::Index.new([define_attribute(:name, :String, source: schema.name)]),
|
227
|
-
ROM::SQL::Index.new(
|
228
|
-
[define_attribute(:created_at, :Time, source: schema.name),
|
229
|
-
define_attribute(:name, :String, source: schema.name)]
|
230
|
-
),
|
231
|
-
ROM::SQL::Index.new(
|
232
|
-
[define_attribute(:updated_at, :Time, source: schema.name)],
|
233
|
-
name: :recently_idx
|
234
|
-
),
|
235
|
-
ROM::SQL::Index.new(
|
236
|
-
[define_attribute(:created_at, :Time, source: schema.name)],
|
237
|
-
name: :unique_date,
|
238
|
-
unique: true
|
239
|
-
)
|
240
|
-
)
|
241
|
-
end
|
242
|
-
|
243
|
-
if metadata[:postgres]
|
244
|
-
it 'can provide index type' do
|
245
|
-
class Test::Tags < ROM::Relation[:sql]
|
246
|
-
schema(:tags) do
|
247
|
-
attribute :id, Types::Serial
|
248
|
-
attribute :name, Types::String
|
249
|
-
|
250
|
-
indexes do
|
251
|
-
index :name, type: :gist
|
252
|
-
end
|
253
|
-
end
|
254
|
-
end
|
255
|
-
|
256
|
-
conf.register_relation(Test::Tags)
|
257
|
-
schema = container.relations[:tags].schema
|
258
|
-
index = schema.indexes.first
|
259
|
-
|
260
|
-
expect(index).to eql(
|
261
|
-
ROM::SQL::Index.new(
|
262
|
-
[define_attribute(:name, :String, source: schema.name)],
|
263
|
-
type: :gist)
|
264
|
-
)
|
265
|
-
|
266
|
-
expect(index.type).to eql(:gist)
|
267
|
-
end
|
268
|
-
end
|
269
|
-
end
|
270
|
-
end
|
271
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
RSpec.describe ROM::SQL::Schema, '#call' do
|
4
|
-
include_context 'users'
|
5
|
-
|
6
|
-
with_adapters :postgres do
|
7
|
-
before do
|
8
|
-
conf.relation(:users) do
|
9
|
-
schema(infer: true)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
let(:schema) { relations[:users].schema }
|
14
|
-
|
15
|
-
it 'auto-projects a relation' do
|
16
|
-
expect(schema.(relations[:users]).dataset.sql).to eql('SELECT "id", "name" FROM "users" ORDER BY "users"."id"')
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'maintains schema' do
|
20
|
-
projected = relations[:users].schema.project(:name)
|
21
|
-
expect(projected.(relations[:users]).schema).to be(projected)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,45 +0,0 @@
|
|
1
|
-
RSpec.describe 'ROM::SQL::Schema::MysqlInferrer', :mysql do
|
2
|
-
include_context 'database setup'
|
3
|
-
|
4
|
-
before do
|
5
|
-
inferrable_relations.concat %i(test_inferrence)
|
6
|
-
end
|
7
|
-
|
8
|
-
before do
|
9
|
-
conn.create_table :test_inferrence do
|
10
|
-
tinyint :tiny
|
11
|
-
mediumint :medium
|
12
|
-
bigint :big
|
13
|
-
datetime :created_at
|
14
|
-
column :date_and_time, 'datetime(0)'
|
15
|
-
column :time_with_ms, 'datetime(3)'
|
16
|
-
timestamp :unix_time_usec
|
17
|
-
column :unix_time_sec, 'timestamp(0) null'
|
18
|
-
boolean :flag, null: false
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
before do
|
23
|
-
conf.relation(:test_inferrence) do
|
24
|
-
schema(infer: true)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
let(:schema) { container.relations[:test_inferrence].schema }
|
29
|
-
let(:source) { container.relations[:test_inferrence].name }
|
30
|
-
|
31
|
-
it 'can infer attributes for dataset' do
|
32
|
-
expect(schema.to_h).
|
33
|
-
to eql(
|
34
|
-
tiny: ROM::SQL::Types::Int.optional.meta(name: :tiny, source: source),
|
35
|
-
medium: ROM::SQL::Types::Int.optional.meta(name: :medium, source: source),
|
36
|
-
big: ROM::SQL::Types::Int.optional.meta(name: :big, source: source),
|
37
|
-
created_at: ROM::SQL::Types::Time.optional.meta(name: :created_at, source: source),
|
38
|
-
date_and_time: ROM::SQL::Types::Time.optional.meta(name: :date_and_time, source: source),
|
39
|
-
time_with_ms: ROM::SQL::Types::Time.optional.meta(name: :time_with_ms, source: source),
|
40
|
-
unix_time_usec: ROM::SQL::Types::Time.meta(name: :unix_time_usec, source: source),
|
41
|
-
unix_time_sec: ROM::SQL::Types::Time.optional.meta(name: :unix_time_sec, source: source),
|
42
|
-
flag: ROM::SQL::Types::Bool.meta(name: :flag, source: source)
|
43
|
-
)
|
44
|
-
end
|
45
|
-
end
|
@@ -1,203 +0,0 @@
|
|
1
|
-
RSpec.describe 'ROM::SQL::Schema::PostgresInferrer', :postgres do
|
2
|
-
include_context 'database setup'
|
3
|
-
|
4
|
-
before do
|
5
|
-
inferrable_relations.concat %i(test_inferrence)
|
6
|
-
end
|
7
|
-
|
8
|
-
colors = %w(red orange yellow green blue purple)
|
9
|
-
|
10
|
-
before do
|
11
|
-
conn.extension :pg_enum
|
12
|
-
|
13
|
-
conn.execute('create extension if not exists hstore')
|
14
|
-
conn.drop_table?(:test_inferrence)
|
15
|
-
conn.drop_enum(:rainbow, if_exists: true)
|
16
|
-
|
17
|
-
conn.create_enum(:rainbow, colors)
|
18
|
-
|
19
|
-
conn.create_table :test_inferrence do
|
20
|
-
primary_key :id, :uuid
|
21
|
-
bigint :big
|
22
|
-
Json :json_data
|
23
|
-
Jsonb :jsonb_data
|
24
|
-
Decimal :money, null: false
|
25
|
-
column :tags, "text[]"
|
26
|
-
column :tag_ids, "bigint[]"
|
27
|
-
column :ip, "inet"
|
28
|
-
column :subnet, "cidr"
|
29
|
-
column :hw_address, "macaddr"
|
30
|
-
rainbow :color
|
31
|
-
point :center
|
32
|
-
xml :page
|
33
|
-
hstore :mapping
|
34
|
-
line :line
|
35
|
-
circle :circle
|
36
|
-
box :box
|
37
|
-
lseg :lseg
|
38
|
-
polygon :polygon
|
39
|
-
path :path
|
40
|
-
timestamp :created_at
|
41
|
-
column :datetime, "timestamp(0) without time zone"
|
42
|
-
column :datetime_tz, "timestamp(0) with time zone"
|
43
|
-
boolean :flag, null: false
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
let(:schema) { container.relations[:test_inferrence].schema }
|
48
|
-
let(:source) { container.relations[:test_inferrence].name }
|
49
|
-
|
50
|
-
context 'inferring db-specific attributes' do
|
51
|
-
before do
|
52
|
-
conf.relation(:test_inferrence) do
|
53
|
-
schema(infer: true)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'can infer attributes for dataset' do
|
58
|
-
expect(schema.to_h).
|
59
|
-
to eql(
|
60
|
-
id: ROM::SQL::Types::PG::UUID.meta(name: :id, source: source, primary_key: true),
|
61
|
-
big: ROM::SQL::Types::Int.optional.meta(name: :big, source: source),
|
62
|
-
json_data: ROM::SQL::Types::PG::JSON.optional.meta(name: :json_data, source: source),
|
63
|
-
jsonb_data: ROM::SQL::Types::PG::JSONB.optional.meta(name: :jsonb_data, source: source),
|
64
|
-
money: ROM::SQL::Types::Decimal.meta(name: :money, source: source),
|
65
|
-
tags: ROM::SQL::Types::PG::Array('text').optional.meta(name: :tags, source: source),
|
66
|
-
tag_ids: ROM::SQL::Types::PG::Array('biging').optional.meta(name: :tag_ids, source: source),
|
67
|
-
color: ROM::SQL::Types::String.enum(*colors).optional.meta(name: :color, source: source),
|
68
|
-
ip: ROM::SQL::Types::PG::IPAddress.optional.meta(
|
69
|
-
name: :ip,
|
70
|
-
source: source,
|
71
|
-
read: ROM::SQL::Types::PG::IPAddressR.optional
|
72
|
-
),
|
73
|
-
subnet: ROM::SQL::Types::PG::IPAddress.optional.meta(
|
74
|
-
name: :subnet,
|
75
|
-
source: source,
|
76
|
-
read: ROM::SQL::Types::PG::IPAddressR.optional
|
77
|
-
),
|
78
|
-
hw_address: ROM::SQL::Types::String.optional.meta(name: :hw_address, source: source),
|
79
|
-
center: ROM::SQL::Types::PG::PointT.optional.meta(
|
80
|
-
name: :center,
|
81
|
-
source: source,
|
82
|
-
read: ROM::SQL::Types::PG::PointTR.optional
|
83
|
-
),
|
84
|
-
page: ROM::SQL::Types::String.optional.meta(name: :page, source: source),
|
85
|
-
mapping: ROM::SQL::Types::PG::HStore.optional.meta(
|
86
|
-
name: :mapping,
|
87
|
-
source: source,
|
88
|
-
read: ROM::SQL::Types::PG::HStoreR.optional
|
89
|
-
),
|
90
|
-
line: ROM::SQL::Types::PG::LineT.optional.meta(
|
91
|
-
name: :line,
|
92
|
-
source: source,
|
93
|
-
read: ROM::SQL::Types::PG::LineTR.optional
|
94
|
-
),
|
95
|
-
circle: ROM::SQL::Types::PG::CircleT.optional.meta(
|
96
|
-
name: :circle,
|
97
|
-
source: source,
|
98
|
-
read: ROM::SQL::Types::PG::CircleTR.optional
|
99
|
-
),
|
100
|
-
box: ROM::SQL::Types::PG::BoxT.optional.meta(
|
101
|
-
name: :box,
|
102
|
-
source: source,
|
103
|
-
read: ROM::SQL::Types::PG::BoxTR.optional
|
104
|
-
),
|
105
|
-
lseg: ROM::SQL::Types::PG::LineSegmentT.optional.meta(
|
106
|
-
name: :lseg,
|
107
|
-
source: source,
|
108
|
-
read: ROM::SQL::Types::PG::LineSegmentTR.optional
|
109
|
-
),
|
110
|
-
polygon: ROM::SQL::Types::PG::PolygonT.optional.meta(
|
111
|
-
name: :polygon,
|
112
|
-
source: source,
|
113
|
-
read: ROM::SQL::Types::PG::PolygonTR.optional
|
114
|
-
),
|
115
|
-
path: ROM::SQL::Types::PG::PathT.optional.meta(
|
116
|
-
name: :path,
|
117
|
-
source: source,
|
118
|
-
read: ROM::SQL::Types::PG::PathTR.optional
|
119
|
-
),
|
120
|
-
created_at: ROM::SQL::Types::Time.optional.meta(name: :created_at, source: source),
|
121
|
-
datetime: ROM::SQL::Types::Time.optional.meta(name: :datetime, source: source),
|
122
|
-
datetime_tz: ROM::SQL::Types::Time.optional.meta(name: :datetime_tz, source: source),
|
123
|
-
flag: ROM::SQL::Types::Bool.meta(name: :flag, source: source)
|
124
|
-
)
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
context 'with a table without columns' do
|
129
|
-
before do
|
130
|
-
conn.create_table(:dummy) unless conn.table_exists?(:dummy)
|
131
|
-
conf.relation(:dummy) { schema(infer: true) }
|
132
|
-
end
|
133
|
-
|
134
|
-
it 'does not fail with a weird error when a relation does not have attributes' do
|
135
|
-
expect(container.relations[:dummy].schema).to be_empty
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
context 'with a column with bi-directional mapping' do
|
140
|
-
before do
|
141
|
-
conn.execute('create extension if not exists hstore')
|
142
|
-
|
143
|
-
conn.create_table(:test_bidirectional) do
|
144
|
-
primary_key :id
|
145
|
-
inet :ip
|
146
|
-
point :center
|
147
|
-
hstore :mapping
|
148
|
-
line :line
|
149
|
-
circle :circle
|
150
|
-
box :box
|
151
|
-
lseg :lseg
|
152
|
-
polygon :polygon
|
153
|
-
path :closed_path
|
154
|
-
path :open_path
|
155
|
-
end
|
156
|
-
|
157
|
-
conf.relation(:test_bidirectional) { schema(infer: true) }
|
158
|
-
|
159
|
-
conf.commands(:test_bidirectional) do
|
160
|
-
define(:create) do
|
161
|
-
result :one
|
162
|
-
end
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
let(:point) { ROM::SQL::Types::PG::Point.new(7.5, 30.5) }
|
167
|
-
let(:point_2) { ROM::SQL::Types::PG::Point.new(8.5, 35.5) }
|
168
|
-
let(:line) { ROM::SQL::Types::PG::Line.new(2.3, 4.9, 3.1415) }
|
169
|
-
let(:dns) { IPAddr.new('8.8.8.8') }
|
170
|
-
let(:mapping) { Hash['hot' => 'cold'] }
|
171
|
-
let(:circle) { ROM::SQL::Types::PG::Circle.new(point, 1.0) }
|
172
|
-
let(:lseg) { ROM::SQL::Types::PG::LineSegment.new(point, point_2) }
|
173
|
-
let(:box_corrected) { ROM::SQL::Types::PG::Box.new(point_2, point) }
|
174
|
-
let(:box) do
|
175
|
-
upper_left = ROM::SQL::Types::PG::Point.new(point.x, point_2.y)
|
176
|
-
lower_right = ROM::SQL::Types::PG::Point.new(point_2.x, point.y)
|
177
|
-
|
178
|
-
ROM::SQL::Types::PG::Box.new(upper_left, lower_right)
|
179
|
-
end
|
180
|
-
let(:polygon) { ROM::SQL::Types::PG::Polygon[[point, point_2]] }
|
181
|
-
let(:closed_path) { ROM::SQL::Types::PG::Path.new([point, point_2], :closed) }
|
182
|
-
let(:open_path) { ROM::SQL::Types::PG::Path.new([point, point_2], :open) }
|
183
|
-
|
184
|
-
let(:relation) { container.relations[:test_bidirectional] }
|
185
|
-
let(:create) { commands[:test_bidirectional].create }
|
186
|
-
|
187
|
-
it 'writes and reads data & corrects data' do
|
188
|
-
# Box coordinates are reordered if necessary
|
189
|
-
inserted = create.call(
|
190
|
-
id: 1, center: point, ip: dns, mapping: mapping,
|
191
|
-
line: line, circle: circle, lseg: lseg, box: box,
|
192
|
-
polygon: polygon, closed_path: closed_path, open_path: open_path
|
193
|
-
)
|
194
|
-
expect(inserted).
|
195
|
-
to eql(
|
196
|
-
id: 1, center: point, ip: dns, mapping: mapping,
|
197
|
-
line: line, circle: circle, lseg: lseg, box: box_corrected,
|
198
|
-
polygon: polygon, closed_path: closed_path, open_path: open_path
|
199
|
-
)
|
200
|
-
expect(relation.to_a).to eql([inserted])
|
201
|
-
end
|
202
|
-
end
|
203
|
-
end
|