mini_record 0.3.1 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +2 -2
- data/Gemfile +4 -0
- data/README.md +10 -1
- data/Rakefile +4 -3
- data/lib/mini_record/auto_schema.rb +34 -1
- data/lib/mini_record/version.rb +1 -1
- data/test/helper.rb +4 -0
- data/test/test_mini_record.rb +80 -4
- metadata +39 -61
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6e4dc06c60db297bde1fa28fc64dc5e05c33a7d8
|
4
|
+
data.tar.gz: cd4c70eb0948b45cc0320e9909f5db4285815a74
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0b892d72b036a1d9be13cb12a49da31bc6242e15039260f9a449c8bb11ba961a9b4e88dfadff1fd294a8c741b3502b82aa591f10ed158d50ad719516c30b61cb
|
7
|
+
data.tar.gz: 0f7d088dc27778e79096f6a3ccc8ae6a43d9c97ae09bc34463df241da8a8e7a648fb7d50d7a0ba6963015895ace7ec790a1e183ea71acc4ec93af9c71ba6d199
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -51,7 +51,7 @@ Instead of `:as => :my_type` you can use `:type => :my_type`
|
|
51
51
|
Option `:as` or `:type` if not provided is `:string` by default, you can use all ActiveRecord types:
|
52
52
|
|
53
53
|
``` rb
|
54
|
-
:primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time,
|
54
|
+
:primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time,
|
55
55
|
:date, :binary, :boolean, :references, :belongs_to, :timestamp
|
56
56
|
```
|
57
57
|
|
@@ -89,6 +89,15 @@ end
|
|
89
89
|
```
|
90
90
|
Will result in a person_id column (you can override with the `foreign_key` option) which is indexed
|
91
91
|
|
92
|
+
#### belongs_to with foreign key in database
|
93
|
+
```ruby
|
94
|
+
class Address < ActiveRecord::Base
|
95
|
+
belongs_to :person
|
96
|
+
index :person_id, :foreign => true
|
97
|
+
end
|
98
|
+
```
|
99
|
+
The same as in the previous case, but foreign key will be added to the database with help of [foreigner](https://github.com/matthuhiggins/foreigner) gem.
|
100
|
+
|
92
101
|
#### belongs_to (polymorphic)
|
93
102
|
```ruby
|
94
103
|
class Address < ActiveRecord::Base
|
data/Rakefile
CHANGED
@@ -85,7 +85,7 @@ module MiniRecord
|
|
85
85
|
alias :col :field
|
86
86
|
|
87
87
|
def timestamps
|
88
|
-
field :created_at, :updated_at, :as => :datetime
|
88
|
+
field :created_at, :updated_at, :as => :datetime, :null => false
|
89
89
|
end
|
90
90
|
|
91
91
|
def reset_table_definition!
|
@@ -120,6 +120,35 @@ module MiniRecord
|
|
120
120
|
end
|
121
121
|
end
|
122
122
|
|
123
|
+
# Remove foreign keys for indexes without :foreign=>true option
|
124
|
+
def remove_foreign_keys
|
125
|
+
# fk cache to minimize quantity of sql queries
|
126
|
+
@foreign_keys = connection.foreign_keys(table_name)
|
127
|
+
indexes.each do |name, options|
|
128
|
+
unless options[:foreign]
|
129
|
+
foreign_key = @foreign_keys.detect { |fk| fk.options[:column] == options[:column].to_s }
|
130
|
+
if foreign_key
|
131
|
+
connection.remove_foreign_key(table_name, :name => foreign_key.options[:name])
|
132
|
+
@foreign_keys.delete(foreign_key)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
# Add foreign keys for indexes with :foreign=>true option, if the key doesn't exists
|
139
|
+
def add_foreign_keys
|
140
|
+
indexes.each do |name, options|
|
141
|
+
if options[:foreign]
|
142
|
+
column = options[:column].to_s
|
143
|
+
unless @foreign_keys.detect { |fk| fk[:options][:column] == column }
|
144
|
+
to_table = (reflect_on_all_associations.detect { |a| a.foreign_key==column }).table_name
|
145
|
+
connection.add_foreign_key(table_name, to_table, options)
|
146
|
+
@foreign_keys << { :options=> { :column=>column } }
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
123
152
|
def auto_upgrade!
|
124
153
|
return unless connection?
|
125
154
|
|
@@ -237,6 +266,8 @@ module MiniRecord
|
|
237
266
|
end
|
238
267
|
end
|
239
268
|
|
269
|
+
remove_foreign_keys if connection.respond_to?(:foreign_keys)
|
270
|
+
|
240
271
|
# Remove old index
|
241
272
|
(indexes_in_db.keys - indexes.keys).each do |name|
|
242
273
|
connection.remove_index(table_name, :name => name)
|
@@ -250,6 +281,8 @@ module MiniRecord
|
|
250
281
|
end
|
251
282
|
end
|
252
283
|
|
284
|
+
add_foreign_keys if connection.respond_to?(:foreign_keys)
|
285
|
+
|
253
286
|
# Reload column information
|
254
287
|
reset_column_information
|
255
288
|
end
|
data/lib/mini_record/version.rb
CHANGED
data/test/helper.rb
CHANGED
@@ -49,6 +49,10 @@ end # ActiveRecord::Base
|
|
49
49
|
case ENV['DB']
|
50
50
|
when 'mysql'
|
51
51
|
ActiveRecord::Base.establish_connection(:adapter => 'mysql', :database => 'test', :user => 'root')
|
52
|
+
when 'mysql2'
|
53
|
+
ActiveRecord::Base.establish_connection(:adapter => 'mysql2', :database => 'test', :user => 'root')
|
54
|
+
Bundler.require(:test) # require 'foreigner'
|
55
|
+
Foreigner.load
|
52
56
|
when 'pg', 'postgresql'
|
53
57
|
ActiveRecord::Base.establish_connection(:adapter => 'postgresql', :database => 'test', :user => 'postgres', :host => 'localhost')
|
54
58
|
else
|
data/test/test_mini_record.rb
CHANGED
@@ -3,8 +3,11 @@ require File.expand_path('../helper.rb', __FILE__)
|
|
3
3
|
describe MiniRecord do
|
4
4
|
|
5
5
|
before do
|
6
|
+
ActiveRecord::Base.clear_reloadable_connections!
|
7
|
+
ActiveRecord::Base.clear_cache!
|
8
|
+
ActiveRecord::Base.clear_active_connections!
|
6
9
|
conn.tables.each { |table| silence_stream(STDERR) { conn.execute "DROP TABLE IF EXISTS #{table}" } }
|
7
|
-
ActiveRecord::Base.descendants.each { |klass| Object.send(:remove_const, klass.to_s) }
|
10
|
+
ActiveRecord::Base.descendants.each { |klass| Object.send(:remove_const, klass.to_s) if Object.const_defined?(klass.to_s) }
|
8
11
|
ActiveSupport::DescendantsTracker.direct_descendants(ActiveRecord::Base).clear
|
9
12
|
load File.expand_path('../models.rb', __FILE__)
|
10
13
|
ActiveRecord::Base.auto_upgrade!
|
@@ -40,7 +43,7 @@ describe MiniRecord do
|
|
40
43
|
assert_equal 'foo', person.name
|
41
44
|
assert_nil person.surname
|
42
45
|
|
43
|
-
person.
|
46
|
+
person.update_column(:surname, 'bar')
|
44
47
|
assert_equal %w[created_at id name surname updated_at], Person.db_columns.sort
|
45
48
|
|
46
49
|
# Remove a column without lost data
|
@@ -217,7 +220,7 @@ describe MiniRecord do
|
|
217
220
|
foo = Foo.create(:name => 'test')
|
218
221
|
assert_empty Foo.first.name
|
219
222
|
|
220
|
-
foo.
|
223
|
+
foo.update_column(:name, 'foo')
|
221
224
|
|
222
225
|
assert_equal 'foo', Foo.first.name
|
223
226
|
end
|
@@ -319,7 +322,7 @@ describe MiniRecord do
|
|
319
322
|
article.reload
|
320
323
|
assert_nil article.body
|
321
324
|
|
322
|
-
article.
|
325
|
+
article.update_column(:body, 'null')
|
323
326
|
assert_equal 'null', article.body
|
324
327
|
|
325
328
|
# Finally check the index existance
|
@@ -335,6 +338,78 @@ describe MiniRecord do
|
|
335
338
|
assert_equal 4, Foo.db_fields[:user_id].limit
|
336
339
|
assert_equal false, Foo.db_fields[:user_id].null
|
337
340
|
end
|
341
|
+
|
342
|
+
it 'add/remove foreign key with :foreign option, when Foreigner gem used on mysql' do
|
343
|
+
skip unless conn.adapter_name =~ /mysql/i
|
344
|
+
|
345
|
+
class Book < ActiveRecord::Base
|
346
|
+
belongs_to :publisher
|
347
|
+
index :publisher_id, :foreign => true
|
348
|
+
end
|
349
|
+
Book.auto_upgrade!
|
350
|
+
|
351
|
+
assert_includes Book.db_columns, 'publisher_id'
|
352
|
+
assert_includes Book.db_indexes, 'index_books_on_publisher_id'
|
353
|
+
|
354
|
+
assert connection.foreign_keys(:books).detect {|fk| fk.options[:column] == 'publisher_id'}
|
355
|
+
|
356
|
+
Object.send(:remove_const, :Book)
|
357
|
+
class Book < ActiveRecord::Base
|
358
|
+
belongs_to :publisher
|
359
|
+
end
|
360
|
+
Book.auto_upgrade!
|
361
|
+
|
362
|
+
assert_nil connection.foreign_keys(:books).detect {|fk| fk.options[:column] == 'publisher_id'}
|
363
|
+
end
|
364
|
+
|
365
|
+
it 'add/remove named foreign key with :foreign option, when Foreigner gem used on mysql' do
|
366
|
+
skip unless conn.adapter_name =~ /mysql/i
|
367
|
+
|
368
|
+
class Book < ActiveRecord::Base
|
369
|
+
belongs_to :publisher
|
370
|
+
index :publisher_id, :name => 'my_super_publisher_id_fk', :foreign => true
|
371
|
+
end
|
372
|
+
Book.auto_upgrade!
|
373
|
+
|
374
|
+
assert_includes Book.db_columns, 'publisher_id'
|
375
|
+
assert_includes Book.db_indexes, 'my_super_publisher_id_fk'
|
376
|
+
|
377
|
+
assert connection.foreign_keys(:books).detect {|fk| fk.options[:column] == 'publisher_id'}
|
378
|
+
|
379
|
+
Object.send(:remove_const, :Book)
|
380
|
+
class Book < ActiveRecord::Base
|
381
|
+
belongs_to :publisher
|
382
|
+
index :publisher_id, :name => 'my_super_publisher_id_fk'
|
383
|
+
end
|
384
|
+
Book.auto_upgrade!
|
385
|
+
|
386
|
+
assert_nil connection.foreign_keys(:books).detect {|fk| fk.options[:column] == 'publisher_id'}
|
387
|
+
Object.send(:remove_const, :Book)
|
388
|
+
end
|
389
|
+
|
390
|
+
it 'support :foreign option in the index with custom :foreign_key in the belong_to association' do
|
391
|
+
skip unless conn.adapter_name =~ /mysql/i
|
392
|
+
|
393
|
+
class Book < ActiveRecord::Base
|
394
|
+
belongs_to :second_publisher, :foreign_key => 'second_publisher_id', :class_name => 'Publisher'
|
395
|
+
index :second_publisher_id, :foreign => true
|
396
|
+
end
|
397
|
+
Book.auto_upgrade!
|
398
|
+
|
399
|
+
assert_includes Book.db_columns, 'second_publisher_id'
|
400
|
+
assert_includes Book.db_indexes, 'index_books_on_second_publisher_id'
|
401
|
+
|
402
|
+
assert connection.foreign_keys(:books).detect {|fk| fk.options[:column] == 'second_publisher_id'}
|
403
|
+
|
404
|
+
Object.send(:remove_const, :Book)
|
405
|
+
class Book < ActiveRecord::Base
|
406
|
+
belongs_to :second_publisher, :foreign_key => 'second_publisher_id', :class_name => 'Publisher'
|
407
|
+
end
|
408
|
+
Book.auto_upgrade!
|
409
|
+
|
410
|
+
assert_nil connection.foreign_keys(:books).detect {|fk| fk.options[:column] == 'second_publisher_id'}
|
411
|
+
end
|
412
|
+
|
338
413
|
end
|
339
414
|
|
340
415
|
describe 'relation #habtm' do
|
@@ -500,4 +575,5 @@ describe MiniRecord do
|
|
500
575
|
assert_equal 2, Foo.db_fields[:currency].scale
|
501
576
|
assert_equal 4, Foo.db_fields[:currency].limit
|
502
577
|
end
|
578
|
+
|
503
579
|
end
|
metadata
CHANGED
@@ -1,51 +1,38 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: mini_record
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 3
|
9
|
-
- 1
|
10
|
-
version: 0.3.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.4
|
11
5
|
platform: ruby
|
12
|
-
authors:
|
6
|
+
authors:
|
13
7
|
- Davide D'Agostino
|
14
8
|
autorequire:
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- !ruby/object:Gem::Dependency
|
11
|
+
date: 2013-06-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
21
14
|
name: activerecord
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
- - ">="
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
hash: 15
|
29
|
-
segments:
|
30
|
-
- 3
|
31
|
-
- 2
|
32
|
-
- 0
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
33
19
|
version: 3.2.0
|
34
20
|
type: :runtime
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.2.0
|
27
|
+
description: "\nWith it you can add the ability to create columns outside the default
|
28
|
+
schema, directly\nin your model in a similar way that you just know in others projects\nlike
|
29
|
+
\ DataMapper or MongoMapper.\n "
|
30
|
+
email:
|
41
31
|
- d.dagostino@lipsiasoft.com
|
42
32
|
executables: []
|
43
|
-
|
44
33
|
extensions: []
|
45
|
-
|
46
34
|
extra_rdoc_files: []
|
47
|
-
|
48
|
-
files:
|
35
|
+
files:
|
49
36
|
- .gitignore
|
50
37
|
- .travis.yml
|
51
38
|
- Gemfile
|
@@ -60,38 +47,29 @@ files:
|
|
60
47
|
- test/test_mini_record.rb
|
61
48
|
homepage: https://github.com/DAddYE/mini_record
|
62
49
|
licenses: []
|
63
|
-
|
50
|
+
metadata: {}
|
64
51
|
post_install_message:
|
65
52
|
rdoc_options: []
|
66
|
-
|
67
|
-
require_paths:
|
53
|
+
require_paths:
|
68
54
|
- lib
|
69
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
none: false
|
80
|
-
requirements:
|
81
|
-
- - ">="
|
82
|
-
- !ruby/object:Gem::Version
|
83
|
-
hash: 3
|
84
|
-
segments:
|
85
|
-
- 0
|
86
|
-
version: "0"
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
87
65
|
requirements: []
|
88
|
-
|
89
66
|
rubyforge_project: mini_record
|
90
|
-
rubygems_version:
|
67
|
+
rubygems_version: 2.0.3
|
91
68
|
signing_key:
|
92
|
-
specification_version:
|
93
|
-
summary: MiniRecord is a micro gem that allow you to write schema inside your model
|
94
|
-
|
69
|
+
specification_version: 4
|
70
|
+
summary: MiniRecord is a micro gem that allow you to write schema inside your model
|
71
|
+
as you can do in DataMapper.
|
72
|
+
test_files:
|
95
73
|
- test/helper.rb
|
96
74
|
- test/models.rb
|
97
75
|
- test/test_mini_record.rb
|