public this repo is viewable by everyone
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Added change_table for migrations (Jeff Dean) [#71 state:resolved]
dhh (author)
13 days ago
commit  96980bd561d79824b6cb6efbcbecdcbf8785d452
tree    66c4d506c883dbebf628c7bed020b704980d6729
parent  64092de25727c1943807bf5345107d90428135a0
...
1
2
 
 
 
 
 
 
 
 
 
3
4
5
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
0
@@ -1,5 +1,14 @@
0
 *SVN*
0
 
0
+* Added change_table for migrations (Jeff Dean) [#71]. Example:
0
+
0
+ change_table :videos do |t|
0
+ t.timestamps # adds created_at, updated_at
0
+ t.belongs_to :goat # add goat_id integer
0
+ t.string :name, :email, :limit => 20 # adds name and email both with a 20 char limit
0
+ t.remove :name, :email # removes the name and email columns
0
+ end
0
+
0
 * Fixed has_many :through .create with no parameters caused a "can't dup NilClass" error (Steven Soroka) [#85]
0
 
0
 * Added block-setting of attributes for Base.create like Base.new already has (Adam Meehan) [#39]
...
164
165
166
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
168
169
...
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
0
@@ -164,6 +164,28 @@ A short rundown of the major features:
0
     ActiveRecord::Base.logger = Log4r::Logger.new("Application Log")
0
 
0
 
0
+* Database agnostic schema management with Migrations
0
+
0
+ class AddSystemSettings < ActiveRecord::Migration
0
+ def self.up
0
+ create_table :system_settings do |t|
0
+ t.string :name
0
+ t.string :label
0
+ t.text :value
0
+ t.string :type
0
+ t.integer :position
0
+ end
0
+
0
+ SystemSetting.create :name => "notice", :label => "Use notice?", :value => 1
0
+ end
0
+
0
+ def self.down
0
+ drop_table :system_settings
0
+ end
0
+ end
0
+
0
+ {Learn more}[link:classes/ActiveRecord/Migration.html]
0
+
0
 == Simple example (1/2): Defining tables and classes (using MySQL)
0
 
0
 Data definitions are specified only in the database. Active Record queries the database for
...
27
28
29
30
 
 
 
 
31
32
33
...
27
28
29
 
30
31
32
33
34
35
36
0
@@ -27,7 +27,10 @@ you can do so with:
0
 
0
    rake test_mysql TEST=test/cases/base_test.rb
0
    
0
-That'll run the base suite using the MySQL-Ruby adapter.
0
+That'll run the base suite using the MySQL-Ruby adapter. Some tests rely on the schema
0
+being initialized - you can initialize the schema with:
0
+
0
+ rake test_mysql TEST=test/cases/aaa_create_tables_test.rb
0
 
0
 
0
 
...
469
470
471
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
472
473
...
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
0
@@ -469,5 +469,195 @@ module ActiveRecord
0
           @base.native_database_types
0
         end
0
     end
0
+
0
+ # Represents a SQL table in an abstract way for updating a table.
0
+ # Also see TableDefinition and SchemaStatements#create_table
0
+ #
0
+ # Available transformations are:
0
+ #
0
+ # change_table :table do |t|
0
+ # t.column
0
+ # t.index
0
+ # t.timestamps
0
+ # t.change
0
+ # t.change_default
0
+ # t.rename
0
+ # t.references
0
+ # t.belongs_to
0
+ # t.string
0
+ # t.text
0
+ # t.integer
0
+ # t.float
0
+ # t.decimal
0
+ # t.datetime
0
+ # t.timestamp
0
+ # t.time
0
+ # t.date
0
+ # t.binary
0
+ # t.boolean
0
+ # t.remove
0
+ # t.remove_references
0
+ # t.remove_belongs_to
0
+ # t.remove_index
0
+ # t.remove_timestamps
0
+ # end
0
+ #
0
+ class Table
0
+ def initialize(table_name, base)
0
+ @table_name = table_name
0
+ @base = base
0
+ end
0
+
0
+ # Adds a new column to the named table.
0
+ # See TableDefinition#column for details of the options you can use.
0
+ # ===== Examples
0
+ # ====== Creating a simple columns
0
+ # t.column(:name, :string)
0
+ def column(column_name, type, options = {})
0
+ @base.add_column(@table_name, column_name, type, options)
0
+ end
0
+
0
+ # Adds a new index to the table. +column_name+ can be a single Symbol, or
0
+ # an Array of Symbols. See SchemaStatements#add_index
0
+ #
0
+ # ===== Examples
0
+ # ====== Creating a simple index
0
+ # t.index(:name)
0
+ # ====== Creating a unique index
0
+ # t.index([:branch_id, :party_id], :unique => true)
0
+ # ====== Creating a named index
0
+ # t.index([:branch_id, :party_id], :unique => true, :name => 'by_branch_party')
0
+ def index(column_name, options = {})
0
+ @base.add_index(@table_name, column_name, options)
0
+ end
0
+
0
+ # Adds timestamps (created_at and updated_at) columns to the table. See SchemaStatements#timestamps
0
+ # ===== Examples
0
+ # t.timestamps
0
+ def timestamps
0
+ @base.add_timestamps(@table_name)
0
+ end
0
+
0
+ # Changes the column's definition according to the new options.
0
+ # See TableDefinition#column for details of the options you can use.
0
+ # ===== Examples
0
+ # t.change(:name, :string, :limit => 80)
0
+ # t.change(:description, :text)
0
+ def change(column_name, type, options = {})
0
+ @base.change_column(@table_name, column_name, type, options)
0
+ end
0
+
0
+ # Sets a new default value for a column. See
0
+ # ===== Examples
0
+ # t.change_default(:qualification, 'new')
0
+ # t.change_default(:authorized, 1)
0
+ def change_default(column_name, default)
0
+ @base.change_column_default(@table_name, column_name, default)
0
+ end
0
+
0
+ # Removes the column(s) from the table definition.
0
+ # ===== Examples
0
+ # t.remove(:qualification)
0
+ # t.remove(:qualification, :experience)
0
+ # t.removes(:qualification, :experience)
0
+ def remove(*column_names)
0
+ @base.remove_column(@table_name, column_names)
0
+ end
0
+
0
+ # Remove the given index from the table.
0
+ #
0
+ # Remove the suppliers_name_index in the suppliers table.
0
+ # t.remove_index :name
0
+ # Remove the index named accounts_branch_id_index in the accounts table.
0
+ # t.remove_index :column => :branch_id
0
+ # Remove the index named accounts_branch_id_party_id_index in the accounts table.
0
+ # t.remove_index :column => [:branch_id, :party_id]
0
+ # Remove the index named by_branch_party in the accounts table.
0
+ # t.remove_index :name => :by_branch_party
0
+ def remove_index(options = {})
0
+ @base.remove_index(@table_name, options)
0
+ end
0
+
0
+ # Removes the timestamp columns (created_at and updated_at) from the table.
0
+ # ===== Examples
0
+ # t.remove_timestamps
0
+ def remove_timestamps
0
+ @base.remove_timestamps(@table_name)
0
+ end
0
+
0
+ # Renames a column.
0
+ # ===== Example
0
+ # t.rename(:description, :name)
0
+ def rename(column_name, new_column_name)
0
+ @base.rename_column(@table_name, column_name, new_column_name)
0
+ end
0
+
0
+ # Adds a reference. Optionally adds a +type+ column. <tt>reference</tt>,
0
+ # <tt>references</tt> and <tt>belongs_to</tt> are all acceptable
0
+ # ===== Example
0
+ # t.references(:goat)
0
+ # t.references(:goat, :polymorphic => true)
0
+ # t.references(:goat)
0
+ # t.belongs_to(:goat)
0
+ def references(*args)
0
+ options = args.extract_options!
0
+ polymorphic = options.delete(:polymorphic)
0
+ args.each do |col|
0
+ @base.add_column(@table_name, "#{col}_id", :integer, options)
0
+ @base.add_column(@table_name, "#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) unless polymorphic.nil?
0
+ end
0
+ end
0
+ alias :belongs_to :references
0
+
0
+ # Adds a reference. Optionally removes a +type+ column. <tt>remove_reference</tt>,
0
+ # <tt>remove_references</tt> and <tt>remove_belongs_to</tt> are all acceptable
0
+ # ===== Example
0
+ # t.remove_reference(:goat)
0
+ # t.remove_reference(:goat, :polymorphic => true)
0
+ # t.remove_references(:goat)
0
+ # t.remove_belongs_to(:goat)
0
+ def remove_references(*args)
0
+ options = args.extract_options!
0
+ polymorphic = options.delete(:polymorphic)
0
+ args.each do |col|
0
+ @base.remove_column(@table_name, "#{col}_id")
0
+ @base.remove_column(@table_name, "#{col}_type") unless polymorphic.nil?
0
+ end
0
+ end
0
+ alias :remove_belongs_to :remove_references
0
+
0
+ # Adds a column or columns of a specified type
0
+ # ===== Example
0
+ # t.string(:goat)
0
+ # t.string(:goat, :sheep)
0
+ %w( string text integer float decimal datetime timestamp time date binary boolean ).each do |column_type|
0
+ class_eval <<-EOV
0
+ def #{column_type}(*args)
0
+ options = args.extract_options!
0
+ column_names = args
0
+
0
+ column_names.each do |name|
0
+ column = ColumnDefinition.new(@base, name, '#{column_type}')
0
+ if options[:limit]
0
+ column.limit = options[:limit]
0
+ elsif native['#{column_type}'.to_sym].is_a?(Hash)
0
+ column.limit = native['#{column_type}'.to_sym][:limit]
0
+ end
0
+ column.precision = options[:precision]
0
+ column.scale = options[:scale]
0
+ column.default = options[:default]
0
+ column.null = options[:null]
0
+ @base.add_column(@table_name, name, column.sql_type, options)
0
+ end
0
+ end
0
+ EOV
0
+ end
0
+
0
+ private
0
+ def native
0
+ @base.native_database_types
0
+ end
0
+ end
0
+
0
   end
0
 end
...
104
105
106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
108
109
...
124
125
126
127
 
128
129
130
131
 
 
 
 
 
132
133
 
 
134
135
136
...
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
...
185
186
187
 
188
189
190
 
 
191
192
193
194
195
196
 
197
198
199
200
201
0
@@ -104,6 +104,67 @@ module ActiveRecord
0
         execute create_sql
0
       end
0
 
0
+ # A block for changing columns in +table+.
0
+ #
0
+ # === Example
0
+ # # change_table() yields a Table instance
0
+ # change_table(:suppliers) do |t|
0
+ # t.column :name, :string, :limit => 60
0
+ # # Other column alterations here
0
+ # end
0
+ #
0
+ # ===== Examples
0
+ # ====== Add a column
0
+ # change_table(:suppliers) do |t|
0
+ # t.column :name, :string, :limit => 60
0
+ # end
0
+ #
0
+ # ====== Add 2 integer columns
0
+ # change_table(:suppliers) do |t|
0
+ # t.integer :width, :height, :null => false, :default => 0
0
+ # end
0
+ #
0
+ # ====== Add created_at/updated_at columns
0
+ # change_table(:suppliers) do |t|
0
+ # t.timestamps
0
+ # end
0
+ #
0
+ # ====== Add a foreign key column
0
+ # change_table(:suppliers) do |t|
0
+ # t.references :company
0
+ # end
0
+ #
0
+ # Creates a <tt>company_id(integer)</tt> column
0
+ #
0
+ # ====== Add a polymorphic foreign key column
0
+ # change_table(:suppliers) do |t|
0
+ # t.belongs_to :company, :polymorphic => true
0
+ # end
0
+ #
0
+ # Creates <tt>company_type(varchar)</tt> and <tt>company_id(integer)</tt> columns
0
+ #
0
+ # ====== Remove a column
0
+ # change_table(:suppliers) do |t|
0
+ # t.remove :company
0
+ # end
0
+ #
0
+ # ====== Remove a column
0
+ # change_table(:suppliers) do |t|
0
+ # t.remove :company_id
0
+ # t.remove :width, :height
0
+ # end
0
+ #
0
+ # ====== Remove an index
0
+ # change_table(:suppliers) do |t|
0
+ # t.remove_index :company_id
0
+ # end
0
+ #
0
+ # See also Table for details on
0
+ # all of the various column transformation
0
+ def change_table(table_name)
0
+ yield Table.new(table_name, self)
0
+ end
0
+
0
       # Renames a table.
0
       # ===== Example
0
       # rename_table('octopuses', 'octopi')
0
@@ -124,13 +185,17 @@ module ActiveRecord
0
         execute(add_column_sql)
0
       end
0
 
0
- # Removes the column from the table definition.
0
+ # Removes the column(s) from the table definition.
0
       # ===== Examples
0
       # remove_column(:suppliers, :qualification)
0
- def remove_column(table_name, column_name)
0
- execute "ALTER TABLE #{quote_table_name(table_name)} DROP #{quote_column_name(column_name)}"
0
+ # remove_columns(:suppliers, :qualification, :experience)
0
+ def remove_column(table_name, *column_names)
0
+ column_names.flatten.each do |column_name|
0
+ execute "ALTER TABLE #{quote_table_name(table_name)} DROP #{quote_column_name(column_name)}"
0
+ end
0
       end
0
-
0
+ alias :remove_columns :remove_column
0
+
0
       # Changes the column's definition according to the new options.
0
       # See TableDefinition#column for details of the options you can use.
0
       # ===== Examples
...
219
220
221
222
223
224
 
 
 
 
 
225
226
 
227
228
229
...
219
220
221
 
 
 
222
223
224
225
226
227
228
229
230
231
232
0
@@ -219,11 +219,14 @@ module ActiveRecord
0
         execute "VACUUM"
0
       end
0
 
0
- def remove_column(table_name, column_name) #:nodoc:
0
- alter_table(table_name) do |definition|
0
- definition.columns.delete(definition[column_name])
0
+ def remove_column(table_name, *column_names) #:nodoc:
0
+ column_names.flatten.each do |column_name|
0
+ alter_table(table_name) do |definition|
0
+ definition.columns.delete(definition[column_name])
0
+ end
0
         end
0
       end
0
+ alias :remove_columns :remove_column
0
 
0
       def change_column_default(table_name, column_name, default) #:nodoc:
0
         alter_table(table_name) do |definition|
...
1077
1078
1079
1080
 
1081
1082
1083
...
1086
1087
1088
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1089
...
1077
1078
1079
 
1080
1081
1082
1083
...
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
0
@@ -1077,7 +1077,7 @@ if ActiveRecord::Base.connection.supports_migrations?
0
 
0
       protected
0
       def with_new_table
0
- Person.connection.create_table :delete_me do |t|
0
+ Person.connection.create_table :delete_me, :force => true do |t|
0
           yield t
0
         end
0
       ensure
0
@@ -1086,4 +1086,210 @@ if ActiveRecord::Base.connection.supports_migrations?
0
 
0
     end # SexyMigrationsTest
0
   end # uses_mocha
0
+
0
+ uses_mocha 'ChangeTable migration tests' do
0
+ class ChangeTableMigrationsTest < ActiveRecord::TestCase
0
+ def setup
0
+ @connection = Person.connection
0
+ @connection.create_table :delete_me, :force => true do |t|
0
+ end
0
+ end
0
+
0
+ def teardown
0
+ Person.connection.drop_table :delete_me rescue nil
0
+ end
0
+
0
+ def test_references_column_type_adds_id
0
+ with_change_table do |t|
0
+ @connection.expects(:add_column).with(:delete_me, 'customer_id', :integer, {})
0
+ t.references :customer
0
+ end
0
+ end
0
+
0
+ def test_remove_references_column_type_removes_id
0
+ with_change_table do |t|
0
+ @connection.expects(:remove_column).with(:delete_me, 'customer_id')
0
+ t.remove_references :customer
0
+ end
0
+ end
0
+
0
+ def test_add_belongs_to_works_like_add_references
0
+ with_change_table do |t|
0
+ @connection.expects(:add_column).with(:delete_me, 'customer_id', :integer, {})
0
+ t.belongs_to :customer
0
+ end
0
+ end
0
+
0
+ def test_remove_belongs_to_works_like_remove_references
0
+ with_change_table do |t|
0
+ @connection.expects(:remove_column).with(:delete_me, 'customer_id')
0
+ t.remove_belongs_to :customer
0
+ end
0
+ end
0
+
0
+ def test_references_column_type_with_polymorphic_adds_type
0
+ with_change_table do |t|
0
+ @connection.expects(:add_column).with(:delete_me, 'taggable_type', :string, {})
0
+ @connection.expects(:add_column).with(:delete_me, 'taggable_id', :integer, {})
0
+ t.references :taggable, :polymorphic => true
0
+ end
0
+ end
0
+
0
+ def test_remove_references_column_type_with_polymorphic_removes_type
0
+ with_change_table do |t|
0
+ @connection.expects(:remove_column).with(:delete_me, 'taggable_type')
0
+ @connection.expects(:remove_column).with(:delete_me, 'taggable_id')
0
+ t.remove_references :taggable, :polymorphic => true
0
+ end
0
+ end
0
+
0
+ def test_references_column_type_with_polymorphic_and_options_null_is_false_adds_table_flag
0
+ with_change_table do |t|
0
+ @connection.expects(:add_column).with(:delete_me, 'taggable_type', :string, {:null => false})
0
+ @connection.expects(:add_column).with(:delete_me, 'taggable_id', :integer, {:null => false})
0
+ t.references :taggable, :polymorphic => true, :null => false
0
+ end
0
+ end
0
+
0
+ def test_remove_references_column_type_with_polymorphic_and_options_null_is_false_removes_table_flag
0
+ with_change_table do |t|
0
+ @connection.expects(:remove_column).with(:delete_me, 'taggable_type')
0
+ @connection.expects(:remove_column).with(:delete_me, 'taggable_id')
0
+ t.remove_references :taggable, :polymorphic => true, :null => false
0
+ end
0
+ end
0
+
0
+ def test_timestamps_creates_updated_at_and_created_at
0
+ with_change_table do |t|
0
+ @connection.expects(:add_timestamps).with(:delete_me)
0
+ t.timestamps
0
+ end
0
+ end
0
+
0
+ def test_remove_timestamps_creates_updated_at_and_created_at
0
+ with_change_table do |t|
0
+ @connection.expects(:remove_timestamps).with(:delete_me)
0
+ t.remove_timestamps
0
+ end
0
+ end
0
+
0
+ def string_column
0
+ if current_adapter?(:PostgreSQLAdapter)
0
+ "character varying(255)"
0
+ else
0
+ 'varchar(255)'
0
+ end
0
+ end
0
+
0
+ def integer_column
0
+ if current_adapter?(:SQLite3Adapter) || current_adapter?(:SQLiteAdapter) || current_adapter?(:PostgreSQLAdapter)
0
+ "integer"
0
+ else
0
+ 'int(11)'
0
+ end
0
+ end
0
+
0
+ def test_integer_creates_integer_column
0
+ with_change_table do |t|
0
+ @connection.expects(:add_column).with(:delete_me, :foo, integer_column, {})
0
+ @connection.expects(:add_column).with(:delete_me, :bar, integer_column, {})
0
+ t.integer :foo, :bar
0
+ end
0
+ end
0
+
0
+ def test_string_creates_string_column
0
+ with_change_table do |t|
0
+ @connection.expects(:add_column).with(:delete_me, :foo, string_column, {})
0
+ @connection.expects(:add_column).with(:delete_me, :bar, string_column, {})
0
+ t.string :foo, :bar
0
+ end
0
+ end
0
+
0
+ def test_column_creates_column
0
+ with_change_table do |t|
0
+ @connection.expects(:add_column).with(:delete_me, :bar, :integer, {})
0
+ t.column :bar, :integer
0
+ end
0
+ end
0
+
0
+ def test_column_creates_column_with_options
0
+ with_change_table do |t|
0
+ @connection.expects(:add_column).with(:delete_me, :bar, :integer, {:null => false})
0
+ t.column :bar, :integer, :null => false
0
+ end
0
+ end
0
+
0
+ def test_index_creates_index
0
+ with_change_table do |t|
0
+ @connection.expects(:add_index).with(:delete_me, :bar, {})
0
+ t.index :bar
0
+ end
0
+ end
0
+
0
+ def test_index_creates_index_with_options
0
+ with_change_table do |t|
0
+ @connection.expects(:add_index).with(:delete_me, :bar, {:unique => true})
0
+ t.index :bar, :unique => true
0
+ end
0
+ end
0
+
0
+ def test_change_changes_column
0
+ with_change_table do |t|
0
+ @connection.expects(:change_column).with(:delete_me, :bar, :string, {})
0
+ t.change :bar, :string
0
+ end
0
+ end
0
+
0
+ def test_change_changes_column_with_options
0
+ with_change_table do |t|
0
+ @connection.expects(:change_column).with(:delete_me, :bar, :string, {:null => true})
0
+ t.change :bar, :string, :null => true
0
+ end
0
+ end
0
+
0
+ def test_change_default_changes_column
0
+ with_change_table do |t|
0
+ @connection.expects(:change_column_default).with(:delete_me, :bar, :string)
0
+ t.change_default :bar, :string
0
+ end
0
+ end
0
+
0
+ def test_remove_drops_single_column
0
+ with_change_table do |t|
0
+ @connection.expects(:remove_column).with(:delete_me, [:bar])
0
+ t.remove :bar
0
+ end
0
+ end
0
+
0
+ def test_remove_drops_multiple_columns
0
+ with_change_table do |t|
0
+ @connection.expects(:remove_column).with(:delete_me, [:bar, :baz])
0
+ t.remove :bar, :baz
0
+ end
0
+ end
0
+
0
+ def test_remove_index_removes_index_with_options
0
+ with_change_table do |t|
0
+ @connection.expects(:remove_index).with(:delete_me, {:unique => true})
0
+ t.remove_index :unique => true
0
+ end
0
+ end
0
+
0
+ def test_rename_renames_column
0
+ with_change_table do |t|
0
+ @connection.expects(:rename_column).with(:delete_me, :bar, :baz)
0
+ t.rename :bar, :baz
0
+ end
0
+ end
0
+
0
+ protected
0
+ def with_change_table
0
+ Person.connection.change_table :delete_me do |t|
0
+ yield t
0
+ end
0
+ end
0
+
0
+ end # ChangeTable test
0
+ end # uses_mocha
0
+
0
 end

Comments

  • thetamind 12 days ago

    I’ve contributed a patch to cleanup some of the documentation and whitespace as well as add two missing aliased methods.
    http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/108-patch-change_table-cleanup
    I would appreciate feedback.

  • jerome 7 days ago

    Mmmm
    Submodule path ‘vendor/rails’: checked out ‘7f4171da5e3ed5b3e038b95f8f5ae05ba6e21bef’
    $ rake db:migrate
    (...)
    —change_table(:pages)
    rake aborted!
    undefined method `change_table’ for #<ActiveRecord::ConnectionAdapters::SQLite3Adapter:0×2131e40>