mini_record 0.3.9 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 54cb420cd8731059dd9c17ae15191718a354b6ae
4
- data.tar.gz: 3d3b76d3752e50992241c052051a84055a37362f
3
+ metadata.gz: 865494ec47990622d36b41ccf2c468bfce175efd
4
+ data.tar.gz: 1dd728a8022e9d1eda5901348e121ca42249ea41
5
5
  SHA512:
6
- metadata.gz: c782dd8fb168ef3e0254b30bfe99262b12c0685a4ce3c3d2c5ae314b0b4dd236761d1005641d42915c3b7f70ca448e5667a9454ff30a7e0c312cbbae2e29b260
7
- data.tar.gz: 93f5207799aee9b3e735e94b348907297fdb0a1d0bc31166a5d1af94778a89d9e8668752885ecda649ebffcbf983ffe4cf0bacda62d7aaccaf9ff3e84e7d00ed
6
+ metadata.gz: 835e2394dc4c27ee55dacfbf1479da6480100193d1e680292e35dada29d620c569494a316b8c52791d7b4a32716ee4a7e0e1a7940f734b540ea2d50b7ed58c46
7
+ data.tar.gz: 06f2bc360a0a6a6042365fbb0de94ecaf9cf6fb03731f56d104c9d16749e0b3958690a431622b0fb79bb00145ed25d9e659a50a4e0ecee82a434fdfe4e7fc647
@@ -61,6 +61,10 @@ module MiniRecord
61
61
  end
62
62
  end
63
63
 
64
+ def rename_fields
65
+ @rename_fields ||= {}
66
+ end
67
+
64
68
  def fields
65
69
  table_definition.columns.inject({}) do |hash, column|
66
70
  hash[column.name] = column
@@ -75,6 +79,20 @@ module MiniRecord
75
79
  end
76
80
  end
77
81
 
82
+ def rename_field(*args)
83
+ return unless connection?
84
+
85
+ options = args.extract_options!
86
+ new_name = options.delete(:new_name)
87
+ old_name = args.first
88
+ if old_name && new_name
89
+ rename_fields[old_name] = new_name
90
+ end
91
+ end
92
+ alias :rename_key :rename_field
93
+ alias :rename_property :rename_field
94
+ alias :rename_col :rename_field
95
+
78
96
  def field(*args)
79
97
  return unless connection?
80
98
 
@@ -243,15 +261,26 @@ module MiniRecord
243
261
  field inheritance_column, :as => :string unless fields.key?(inheritance_column.to_s)
244
262
  index inheritance_column
245
263
  end
264
+
265
+ # Rename fields
266
+ rename_fields.each do |old_name, new_name|
267
+ old_column = fields_in_db[old_name.to_s]
268
+ new_column = fields_in_db[new_name.to_s]
269
+ if old_column && !new_column
270
+ connection.rename_column(table_name, old_column.name, new_name)
271
+ end
272
+ end
246
273
 
247
274
  # Remove fields from db no longer in schema
248
- (fields_in_db.keys - fields.keys & fields_in_db.keys).each do |field|
275
+ columns_to_delete = fields_in_db.keys - fields.keys & fields_in_db.keys
276
+ columns_to_delete.each do |field|
249
277
  column = fields_in_db[field]
250
278
  connection.remove_column table_name, column.name
251
279
  end
252
280
 
253
281
  # Add fields to db new to schema
254
- (fields.keys - fields_in_db.keys).each do |field|
282
+ columns_to_add = fields.keys - fields_in_db.keys
283
+ columns_to_add.each do |field|
255
284
  column = fields[field]
256
285
  options = {:limit => column.limit, :precision => column.precision, :scale => column.scale}
257
286
  options[:default] = column.default unless column.default.nil?
@@ -1,3 +1,3 @@
1
1
  module MiniRecord
2
- VERSION = "0.3.9"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -618,4 +618,35 @@ describe MiniRecord do
618
618
  assert_equal ['id', 'name'], User.db_columns.sort
619
619
  assert_equal ['author', 'id', 'title'], Book.db_columns.sort
620
620
  end
621
+
622
+ it 'should rename a column specified by rename_field' do
623
+ class Foo < ActiveRecord::Base
624
+ field :currency, :limit => 3
625
+ end
626
+ Foo.auto_upgrade!
627
+ assert_match /CREATE TABLE/, Foo.queries
628
+
629
+ Foo.create :currency => 'USD'
630
+
631
+ Foo.rename_field :currency, :new_name => :currency_iso
632
+ Foo.field :currency_iso, :limit => 3
633
+
634
+ Foo.auto_upgrade!
635
+
636
+ case conn.adapter_name
637
+ when /sqlite/i
638
+ assert_match /CREATE TEMPORARY TABLE "altered_foos"/i, Foo.queries
639
+ when /mysql/i
640
+ assert_match /ALTER TABLE `foos` CHANGE `currency` `currency_iso`/i, Foo.queries
641
+ when /postgres/i
642
+ assert_match /ALTER TABLE "foos" RENAME COLUMN "currency" TO "currency_iso"/, Foo.queries
643
+ end
644
+
645
+ foo = Foo.first
646
+ assert_equal 'USD', foo.currency_iso
647
+
648
+ Foo.auto_upgrade!
649
+ assert_match '', Foo.queries
650
+
651
+ end
621
652
  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.9
4
+ version: 0.4.0
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: 2014-06-23 00:00:00.000000000 Z
11
+ date: 2014-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord