mv-core 2.2.0 → 2.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +67 -39
- data/lib/mv/core/active_record/connection_adapters/abstract_adapter_decorator.rb +7 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 95b749c7932e7978179b53e4ab0dc8face1ab96f
|
4
|
+
data.tar.gz: 7a434f15ec944f890091da0a20608dae0aeeac69
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e4d963a43ee163fe83564641147d970f7915f35076c627e9535f03c87fc0ad1e27d071812824b13d7ed69c1bf6cea41321de7cbb88af251022f268f9af9f88d
|
7
|
+
data.tar.gz: 2074b4173da82081b6d57b6e9e6cb1f7191f3367ea3e19bd2c675be1ef0cf139e04a77028a0afe2f66436be22fc0b6b02e629658d68a10ae19fdc4fee7d1b05f
|
data/README.md
CHANGED
@@ -4,7 +4,9 @@
|
|
4
4
|
|
5
5
|
# Define validations in database and use them in model
|
6
6
|
|
7
|
-
Project ```Migration Validators``` (MV) makes it possible for RoR developer to define validations directly in db and then bubble them up to model so they available as normal ActiveModel::Validations there. And all that without code duplication.
|
7
|
+
Project ```Migration Validators``` (MV) makes it possible for RoR developer to define validations directly in db and then bubble them up to model so they available as normal ActiveModel::Validations there. And all that without code duplication.
|
8
|
+
|
9
|
+
**WARNING** Versions lower than 2.0 are not supported anymore. As results, rails v.3 or older are not supported either.
|
8
10
|
|
9
11
|
# Abbreviations
|
10
12
|
|
@@ -19,6 +21,7 @@ MV - Migration Validators Projects. All gems that belongs to that project are pr
|
|
19
21
|
* [MySQL](#mysql)
|
20
22
|
* [SQLite](#sqlite)
|
21
23
|
* [Integration with ActiveRecord](#integration-with-activerecord)
|
24
|
+
* [SchemaRb](#schemarb)
|
22
25
|
* [Tasks](#tasks)
|
23
26
|
* [Drivers](#drivers)
|
24
27
|
* [Version History](#version history)
|
@@ -26,13 +29,13 @@ MV - Migration Validators Projects. All gems that belongs to that project are pr
|
|
26
29
|
|
27
30
|
# Why `Migration Validators`
|
28
31
|
|
29
|
-
It's commonly accepted in RoR community to ignore database constraints and define data validations in ActiveModel. In most cases such approach is perfectly acceptable and allows developer to concentrate on business logic rather than on writing database - specific code.
|
32
|
+
It's commonly accepted in RoR community to ignore database constraints and define data validations in ActiveModel. In most cases such approach is perfectly acceptable and allows developer to concentrate on business logic rather than on writing database - specific code.
|
30
33
|
|
31
|
-
But when your application grows significantly then possibility of the data error with such approach became more tangible. Data consistency could be violated in many ways: directly ( with db console for ex ), as result of some error in the code, by other application if database is shared.
|
34
|
+
But when your application grows significantly then possibility of the data error with such approach became more tangible. Data consistency could be violated in many ways: directly ( with db console for ex ), as result of some error in the code, by other application if database is shared.
|
32
35
|
|
33
|
-
DB constraints could help in such case. But there are several reasons why they are not widely spread in RoR: such constraints are DB - specific in most cases and their management is quite tricky.
|
36
|
+
DB constraints could help in such case. But there are several reasons why they are not widely spread in RoR: such constraints are DB - specific in most cases and their management is quite tricky.
|
34
37
|
|
35
|
-
The goal of the `Migration Validators` project is to resolve those problems and make DB constraints management straightforward
|
38
|
+
The goal of the `Migration Validators` project is to resolve those problems and make DB constraints management straightforward
|
36
39
|
|
37
40
|
# How It Works
|
38
41
|
|
@@ -40,7 +43,7 @@ The goal of the `Migration Validators` project is to resolve those problems and
|
|
40
43
|
|
41
44
|
Most of the validations could be defined in several ways: as condition inside trigger, as condition inside check constraint or as index ( for ```uniqueness```)
|
42
45
|
|
43
|
-
In most cases developer can select how and where validation should be implemented. By default most optimal way is proposed.
|
46
|
+
In most cases developer can select how and where validation should be implemented. By default most optimal way is proposed.
|
44
47
|
|
45
48
|
For example: ```uniqueness``` validation is defined as unique index by default. But developer can select other way - trigger of check constraint. Each way has own advantages and disadvantages
|
46
49
|
|
@@ -49,17 +52,17 @@ For example: ```uniqueness``` validation is defined as unique index by default.
|
|
49
52
|
Create new table:
|
50
53
|
|
51
54
|
```ruby
|
52
|
-
def change
|
55
|
+
def change
|
53
56
|
create_table do |t|
|
54
|
-
t.string :str_column, validates: { uniqueness: :true,
|
57
|
+
t.string :str_column, validates: { uniqueness: :true,
|
55
58
|
inclusion: { in: 1..3 }}
|
56
59
|
t.column :column_name, :integer, validates: { exclusion: { in: [1,2,3]}}
|
57
60
|
end
|
58
61
|
end
|
59
62
|
```
|
60
63
|
|
61
|
-
Modify existing table:
|
62
|
-
|
64
|
+
Modify existing table:
|
65
|
+
|
63
66
|
```ruby
|
64
67
|
def up
|
65
68
|
change_table do |t|
|
@@ -76,7 +79,7 @@ For example: ```uniqueness``` validation is defined as unique index by default.
|
|
76
79
|
end
|
77
80
|
```
|
78
81
|
|
79
|
-
Update validation definition:
|
82
|
+
Update validation definition:
|
80
83
|
|
81
84
|
```ruby
|
82
85
|
def up
|
@@ -88,8 +91,8 @@ For example: ```uniqueness``` validation is defined as unique index by default.
|
|
88
91
|
end
|
89
92
|
```
|
90
93
|
|
91
|
-
There are many ways to define desired database constraint. And those ways might vary for each RDBMS. One could define the way how constraint should be
|
92
|
-
defined in DB:
|
94
|
+
There are many ways to define desired database constraint. And those ways might vary for each RDBMS. One could define the way how constraint should be
|
95
|
+
defined in DB:
|
93
96
|
|
94
97
|
as trigger:
|
95
98
|
|
@@ -115,9 +118,9 @@ For example: ```uniqueness``` validation is defined as unique index by default.
|
|
115
118
|
end
|
116
119
|
```
|
117
120
|
|
118
|
-
Also there is possibility to define when validations should occur:
|
121
|
+
Also there is possibility to define when validations should occur:
|
119
122
|
|
120
|
-
when new record created:
|
123
|
+
when new record created:
|
121
124
|
|
122
125
|
```ruby
|
123
126
|
def up
|
@@ -141,12 +144,12 @@ For example: ```uniqueness``` validation is defined as unique index by default.
|
|
141
144
|
end
|
142
145
|
```
|
143
146
|
|
144
|
-
And if you need to define some custom validation you can use custom validation (version >= 2.1 is required):
|
147
|
+
And if you need to define some custom validation you can use custom validation (version >= 2.1 is required):
|
145
148
|
|
146
149
|
```ruby
|
147
150
|
def up
|
148
|
-
validates :table_name, :str_column,
|
149
|
-
custom: { statement: 'LENGTH(TRIM({str_column})) > 10',
|
151
|
+
validates :table_name, :str_column,
|
152
|
+
custom: { statement: 'LENGTH(TRIM({str_column})) > 10',
|
150
153
|
on: :update }
|
151
154
|
end
|
152
155
|
|
@@ -156,8 +159,8 @@ For example: ```uniqueness``` validation is defined as unique index by default.
|
|
156
159
|
```
|
157
160
|
|
158
161
|
as result only values with length greater than 10 will be allowed and that condition will be implemented inside ON UPDATE trigger
|
159
|
-
|
160
|
-
Almost all validations supports shorter notation (simplification) that is not compatible with ActiveRecord validation but much shorter (version >= 2.1 is required):
|
162
|
+
|
163
|
+
Almost all validations supports shorter notation (simplification) that is not compatible with ActiveRecord validation but much shorter (version >= 2.1 is required):
|
161
164
|
|
162
165
|
```ruby
|
163
166
|
def up
|
@@ -181,7 +184,7 @@ For example: ```uniqueness``` validation is defined as unique index by default.
|
|
181
184
|
|
182
185
|
```ruby
|
183
186
|
def up
|
184
|
-
validates :table_name, :str_column, custom:
|
187
|
+
validates :table_name, :str_column, custom:
|
185
188
|
'LENGTH(TRIM({str_column})) > 10'
|
186
189
|
end
|
187
190
|
|
@@ -190,13 +193,13 @@ For example: ```uniqueness``` validation is defined as unique index by default.
|
|
190
193
|
end
|
191
194
|
```
|
192
195
|
|
193
|
-
Supported validators, simplification and their properties might vary from one db driver to another. See detailed properties description in correspondent [driver](#drivers) section.
|
196
|
+
Supported validators, simplification and their properties might vary from one db driver to another. See detailed properties description in correspondent [driver](#drivers) section.
|
194
197
|
|
195
198
|
# Installation
|
196
199
|
|
197
|
-
`mv-core` is a set of core classes that are used everywhere across `Migration Validators` project gems.
|
200
|
+
`mv-core` is a set of core classes that are used everywhere across `Migration Validators` project gems.
|
198
201
|
|
199
|
-
This gem is not intended to be installed directly and referenced from within the application. You should rather install appropriate driver.
|
202
|
+
This gem is not intended to be installed directly and referenced from within the application. You should rather install appropriate driver.
|
200
203
|
|
201
204
|
### PostgreSQL:
|
202
205
|
|
@@ -210,19 +213,19 @@ This gem is not intended to be installed directly and referenced from within the
|
|
210
213
|
gem install mv-mysql
|
211
214
|
```
|
212
215
|
|
213
|
-
### SQLite:
|
216
|
+
### SQLite:
|
214
217
|
|
215
218
|
```
|
216
219
|
gem install mv-sqlite
|
217
220
|
```
|
218
|
-
|
221
|
+
|
219
222
|
# Integration With ActiveRecord
|
220
223
|
|
221
|
-
You can level up validations that are defined in DB to your model using `enforce_migration_validations` method.
|
224
|
+
You can level up validations that are defined in DB to your model using `enforce_migration_validations` method.
|
222
225
|
|
223
|
-
Example:
|
226
|
+
Example:
|
224
227
|
|
225
|
-
migration:
|
228
|
+
migration:
|
226
229
|
|
227
230
|
```ruby
|
228
231
|
def change
|
@@ -232,7 +235,7 @@ migration:
|
|
232
235
|
end
|
233
236
|
```
|
234
237
|
|
235
|
-
model:
|
238
|
+
model:
|
236
239
|
|
237
240
|
```ruby
|
238
241
|
class Post << ActiveRecord::Base
|
@@ -240,18 +243,39 @@ model:
|
|
240
243
|
end
|
241
244
|
```
|
242
245
|
|
243
|
-
console:
|
246
|
+
console:
|
244
247
|
|
245
248
|
```ruby
|
246
249
|
p = Post.new(title: nil)
|
247
250
|
|
248
|
-
p.valid?
|
251
|
+
p.valid?
|
249
252
|
=> false
|
250
253
|
|
251
254
|
p.errors.full_messages
|
252
255
|
=> ["Title can't be blank"]
|
253
256
|
```
|
254
257
|
|
258
|
+
# SchemaRb
|
259
|
+
|
260
|
+
All validations that you've defined are dumped to schema.rb automatically:
|
261
|
+
|
262
|
+
in migration:
|
263
|
+
|
264
|
+
```ruby
|
265
|
+
def change
|
266
|
+
create_table :posts do |t|
|
267
|
+
t.string :title, presence: { message: "can't be blank", as: :trigger }
|
268
|
+
end
|
269
|
+
end
|
270
|
+
```
|
271
|
+
|
272
|
+
in 'schema.rb':
|
273
|
+
|
274
|
+
```ruby
|
275
|
+
validates(:posts, :title,
|
276
|
+
presence: { message: "can't be blank", as: :trigger})
|
277
|
+
```
|
278
|
+
|
255
279
|
# Tasks
|
256
280
|
|
257
281
|
Show all constraints on the specified tables:
|
@@ -260,7 +284,7 @@ console:
|
|
260
284
|
bundle exec rake mv:show_constraints['table_name other_table_name']
|
261
285
|
```
|
262
286
|
|
263
|
-
or show all constraints are created in migrations:
|
287
|
+
or show all constraints are created in migrations:
|
264
288
|
|
265
289
|
```ruby
|
266
290
|
bundle exec rake mv:show_constraints
|
@@ -272,25 +296,25 @@ console:
|
|
272
296
|
bundle exec rake mv:delete_constraints['table_name other_table_name']
|
273
297
|
```
|
274
298
|
|
275
|
-
or remove all constraints are created in migrations:
|
299
|
+
or remove all constraints are created in migrations:
|
276
300
|
|
277
301
|
```ruby
|
278
302
|
bundle exec rake mv:delete_constraints
|
279
303
|
```
|
280
304
|
|
281
|
-
Create / restore / update constraints on the specified tables:
|
305
|
+
Create / restore / update constraints on the specified tables:
|
282
306
|
|
283
307
|
```ruby
|
284
308
|
bundle exec rake mv:create_constraints['table_name other_table_name']
|
285
309
|
```
|
286
310
|
|
287
|
-
or do it for the all tables:
|
311
|
+
or do it for the all tables:
|
288
312
|
|
289
313
|
```ruby
|
290
314
|
bundle exec rake mv:create_constraints
|
291
315
|
```
|
292
316
|
|
293
|
-
Remove all constraints and drop `migration_validators` table:
|
317
|
+
Remove all constraints and drop `migration_validators` table:
|
294
318
|
|
295
319
|
```ruby
|
296
320
|
bundle exec rake mv:uninstall
|
@@ -306,7 +330,7 @@ console:
|
|
306
330
|
|
307
331
|
Currently there are drivers for MySQL, PostgreSQL and SQLite RDBMS
|
308
332
|
|
309
|
-
So - see detailed info here:
|
333
|
+
So - see detailed info here:
|
310
334
|
|
311
335
|
* PostgreSQL: https://github.com/vprokopchuk256/mv-postgresql
|
312
336
|
* MySQL: https://github.com/vprokopchuk256/mv-mysql
|
@@ -320,12 +344,16 @@ So - see detailed info here:
|
|
320
344
|
|
321
345
|
**(2.1.0)** (22 Jan, 2015)
|
322
346
|
|
323
|
-
* Custom validation
|
347
|
+
* Custom validation
|
324
348
|
|
325
349
|
**(2.2.0)** (28 Jan, 2015)
|
326
350
|
|
327
351
|
* Integration with ActiveRecord
|
328
352
|
|
353
|
+
**(2.2.1)** (20 Jul, 2015)
|
354
|
+
|
355
|
+
* Fix issue with invalid parameters number in `add_column` and `change_column` methods
|
356
|
+
|
329
357
|
## Contributing
|
330
358
|
|
331
359
|
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
@@ -6,16 +6,16 @@ module Mv
|
|
6
6
|
module ActiveRecord
|
7
7
|
module ConnectionAdapters
|
8
8
|
module AbstractAdapterDecorator
|
9
|
-
def add_column table_name, column_name, type, opts
|
10
|
-
Mv::Core::Migration::Base.add_column(table_name, column_name, params(opts))
|
11
|
-
|
12
|
-
super
|
9
|
+
def add_column table_name, column_name, type, opts = {}
|
10
|
+
Mv::Core::Migration::Base.add_column(table_name, column_name, params(opts))
|
11
|
+
|
12
|
+
super
|
13
13
|
end
|
14
14
|
|
15
15
|
def remove_column table_name, column_name, type = nil, options = {}
|
16
16
|
Mv::Core::Migration::Base.remove_column table_name, column_name
|
17
17
|
|
18
|
-
super
|
18
|
+
super
|
19
19
|
end
|
20
20
|
|
21
21
|
def rename_column table_name, old_column_name, new_column_name
|
@@ -24,7 +24,7 @@ module Mv
|
|
24
24
|
super
|
25
25
|
end
|
26
26
|
|
27
|
-
def change_column table_name, column_name, type, opts
|
27
|
+
def change_column table_name, column_name, type, opts = {}
|
28
28
|
Mv::Core::Migration::Base.change_column(table_name, column_name, params(opts))
|
29
29
|
|
30
30
|
super
|
@@ -55,4 +55,4 @@ module Mv
|
|
55
55
|
end
|
56
56
|
end
|
57
57
|
end
|
58
|
-
end
|
58
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mv-core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.2.
|
4
|
+
version: 2.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Valeriy Prokopchuk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-07-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|