mini_record 0.3.6 → 0.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/mini_record/auto_schema.rb +45 -14
- data/lib/mini_record/version.rb +1 -1
- data/test/test_mini_record.rb +40 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf09f8afab0f2be41f0bd3e61a906d843430c58c
|
4
|
+
data.tar.gz: 6c7fbc8621bb0aef9f379854363ce4f4ca4e999f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5e1d29807ccff1ae65c3daf5dd8a3b5db185ff86bc7fef33196a084ea847bd516d6a76d5b03ee473d3de61500ff7191eb2ece88885051b005228e1fdf72aad5d
|
7
|
+
data.tar.gz: e9a376ad2b04e69f8ad98c9d38d0a713d8009380b86fce1baf47ec10f0a666fb130b22099b314f1b81bb66684f13433d49a1b1592480dd682251caa567852192
|
@@ -5,23 +5,38 @@ module MiniRecord
|
|
5
5
|
end
|
6
6
|
|
7
7
|
module ClassMethods
|
8
|
+
def init_table_definition(connection)
|
9
|
+
#connection.create_table(table_name) unless connection.table_exists?(table_name)
|
10
|
+
|
11
|
+
case ActiveRecord::ConnectionAdapters::TableDefinition.instance_method(:initialize).arity
|
12
|
+
when 1
|
13
|
+
# Rails 3.2 and earlier
|
14
|
+
ActiveRecord::ConnectionAdapters::TableDefinition.new(connection)
|
15
|
+
when 4
|
16
|
+
# Rails 4
|
17
|
+
ActiveRecord::ConnectionAdapters::TableDefinition.new(connection.native_database_types, table_name, false, {})
|
18
|
+
else
|
19
|
+
raise ArgumentError,
|
20
|
+
"Unsupported number of args for ActiveRecord::ConnectionAdapters::TableDefinition.new()"
|
21
|
+
end
|
22
|
+
end
|
8
23
|
|
9
24
|
def schema_tables
|
10
25
|
@@_schema_tables ||= []
|
11
26
|
end
|
12
27
|
|
13
28
|
def table_definition
|
14
|
-
return superclass.table_definition unless superclass == ActiveRecord::Base
|
29
|
+
return superclass.table_definition unless (superclass == ActiveRecord::Base) || (superclass.respond_to?(:abstract_class?) && superclass.abstract_class?)
|
15
30
|
|
16
31
|
@_table_definition ||= begin
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
32
|
+
tb = init_table_definition(connection)
|
33
|
+
tb.primary_key(primary_key)
|
34
|
+
tb
|
35
|
+
end
|
21
36
|
end
|
22
37
|
|
23
38
|
def indexes
|
24
|
-
return superclass.indexes unless superclass == ActiveRecord::Base
|
39
|
+
return superclass.indexes unless (superclass == ActiveRecord::Base) || (superclass.respond_to?(:abstract_class?) && superclass.abstract_class?)
|
25
40
|
|
26
41
|
@_indexes ||= {}
|
27
42
|
end
|
@@ -33,6 +48,16 @@ module MiniRecord
|
|
33
48
|
end
|
34
49
|
end
|
35
50
|
|
51
|
+
def get_sql_field_type(field)
|
52
|
+
if field.respond_to?(:sql_type)
|
53
|
+
# Rails 3.2 and earlier
|
54
|
+
field.sql_type.to_s.downcase
|
55
|
+
else
|
56
|
+
# Rails 4
|
57
|
+
connection.type_to_sql(field.type.to_sym, field.limit, field.precision, field.scale)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
36
61
|
def fields
|
37
62
|
table_definition.columns.inject({}) do |hash, column|
|
38
63
|
hash[column.name] = column
|
@@ -155,6 +180,7 @@ module MiniRecord
|
|
155
180
|
|
156
181
|
def auto_upgrade!
|
157
182
|
return unless connection?
|
183
|
+
return if respond_to?(:abstract_class?) && abstract_class?
|
158
184
|
|
159
185
|
if self == ActiveRecord::Base
|
160
186
|
descendants.each(&:auto_upgrade!)
|
@@ -166,7 +192,7 @@ module MiniRecord
|
|
166
192
|
class << connection; attr_accessor :table_definition; end unless connection.respond_to?(:table_definition=)
|
167
193
|
connection.table_definition = table_definition
|
168
194
|
connection.create_table(table_name)
|
169
|
-
connection.table_definition =
|
195
|
+
connection.table_definition = init_table_definition(connection)
|
170
196
|
end
|
171
197
|
|
172
198
|
# Add this to our schema tables
|
@@ -238,9 +264,12 @@ module MiniRecord
|
|
238
264
|
new_attr = {}
|
239
265
|
|
240
266
|
# First, check if the field type changed
|
241
|
-
|
242
|
-
|
243
|
-
|
267
|
+
old_sql_type = get_sql_field_type(fields_in_db[field])
|
268
|
+
new_sql_type = get_sql_field_type(fields[field])
|
269
|
+
|
270
|
+
if old_sql_type != new_sql_type
|
271
|
+
logger.debug "[MiniRecord] Detected schema change for #{table_name}.#{field}#type " +
|
272
|
+
" from #{old_sql_type.inspect} to #{new_sql_type.inspect}" if logger
|
244
273
|
changed = true
|
245
274
|
end
|
246
275
|
|
@@ -254,12 +283,14 @@ module MiniRecord
|
|
254
283
|
|
255
284
|
# Next, iterate through our extended attributes, looking for any differences
|
256
285
|
# This catches stuff like :null, :precision, etc
|
286
|
+
# Ignore junk attributes that different versions of Rails include
|
257
287
|
fields[field].each_pair do |att,value|
|
258
|
-
next
|
288
|
+
next unless [:name, :limit, :precision, :scale, :default, :null].include?(att)
|
259
289
|
value = true if att == :null && value.nil?
|
260
|
-
|
261
|
-
|
262
|
-
|
290
|
+
old_value = fields_in_db[field].send(att)
|
291
|
+
if value != old_value
|
292
|
+
logger.debug "[MiniRecord] Detected schema change for #{table_name}.#{field}##{att} " +
|
293
|
+
"from #{old_value.inspect} to #{value.inspect}" if logger
|
263
294
|
new_attr[att] = value
|
264
295
|
changed = true
|
265
296
|
end
|
data/lib/mini_record/version.rb
CHANGED
data/test/test_mini_record.rb
CHANGED
@@ -578,4 +578,44 @@ describe MiniRecord do
|
|
578
578
|
assert_equal 2, Foo.db_fields[:currency].scale
|
579
579
|
assert_equal 4, Foo.db_fields[:currency].limit
|
580
580
|
end
|
581
|
+
|
582
|
+
it 'should ignore abstract classes' do
|
583
|
+
class Foo < ActiveRecord::Base
|
584
|
+
self.abstract_class = true
|
585
|
+
end
|
586
|
+
|
587
|
+
class Bar < Foo
|
588
|
+
end
|
589
|
+
|
590
|
+
Foo.auto_upgrade!
|
591
|
+
Bar.auto_upgrade!
|
592
|
+
|
593
|
+
tables = Foo.connection.tables
|
594
|
+
|
595
|
+
refute_includes tables, 'foos'
|
596
|
+
refute_includes tables, ''
|
597
|
+
assert_includes tables, 'bars'
|
598
|
+
end
|
599
|
+
|
600
|
+
it 'should prevent abstract table class to leak columns to other tables' do
|
601
|
+
|
602
|
+
class Base < ActiveRecord::Base
|
603
|
+
self.abstract_class = true
|
604
|
+
end
|
605
|
+
|
606
|
+
class User < Base
|
607
|
+
col :name
|
608
|
+
end
|
609
|
+
|
610
|
+
class Book < Base
|
611
|
+
col :title
|
612
|
+
col :author
|
613
|
+
end
|
614
|
+
|
615
|
+
User.auto_upgrade!
|
616
|
+
Book.auto_upgrade!
|
617
|
+
|
618
|
+
assert_equal ['id', 'name'], User.db_columns.sort
|
619
|
+
assert_equal ['author', 'id', 'title'], Book.db_columns.sort
|
620
|
+
end
|
581
621
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mini_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Davide D'Agostino
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-09-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -64,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
64
|
version: '0'
|
65
65
|
requirements: []
|
66
66
|
rubyforge_project: mini_record
|
67
|
-
rubygems_version: 2.
|
67
|
+
rubygems_version: 2.1.2
|
68
68
|
signing_key:
|
69
69
|
specification_version: 4
|
70
70
|
summary: MiniRecord is a micro gem that allow you to write schema inside your model
|