public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Search Repo:
applying to stable: Properly quote index names in migrations (closes 
#4764) [John Long]

git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/stable@4240 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
technoweenie (author)
Wed Apr 19 19:48:53 -0700 2006
commit  bf150f0c3a250c1e1cc1f275b6ae98bb76827065
tree    2e566fd04307a706e2170ff6b7a2c13f799de4cf
parent  a9ad634dbc7b2810dffa98079b0d7193be7a4f4f
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* Properly quote index names in migrations (closes #4764) [John Long]
0
+
0
 * Ensure that Associations#include_eager_conditions? checks both scoped and explicit conditions [Rick]
0
 
0
 * Associations#select_limited_ids_list adds the ORDER BY columns to the SELECT DISTINCT List for postgresql. [Rick]
...
119
120
121
122
 
123
124
125
...
128
129
130
131
 
132
133
134
...
184
185
186
187
 
 
188
189
190
...
192
193
194
195
196
 
 
197
198
199
...
209
210
211
212
 
213
214
215
...
119
120
121
 
122
123
124
125
...
128
129
130
 
131
132
133
134
...
184
185
186
 
187
188
189
190
191
...
193
194
195
 
 
196
197
198
199
200
...
210
211
212
 
213
214
215
216
0
@@ -119,7 +119,7 @@
0
       # Adds a new column to the named table.
0
       # See TableDefinition#column for details of the options you can use.
0
       def add_column(table_name, column_name, type, options = {})
0
- add_column_sql = "ALTER TABLE #{table_name} ADD #{column_name} #{type_to_sql(type, options[:limit])}"
0
+ add_column_sql = "ALTER TABLE #{table_name} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit])}"
0
         add_column_options!(add_column_sql, options)
0
         execute(add_column_sql)
0
       end
0
@@ -128,7 +128,7 @@
0
       # ===== Examples
0
       # remove_column(:suppliers, :qualification)
0
       def remove_column(table_name, column_name)
0
- execute "ALTER TABLE #{table_name} DROP #{column_name}"
0
+ execute "ALTER TABLE #{table_name} DROP #{quote_column_name(column_name)}"
0
       end
0
 
0
       # Changes the column's definition according to the new options.
0
@@ -184,7 +184,8 @@
0
       # generates
0
       # CREATE UNIQUE INDEX by_branch_party ON accounts(branch_id, party_id)
0
       def add_index(table_name, column_name, options = {})
0
- index_name = "#{table_name}_#{Array(column_name).first}_index"
0
+ column_names = Array(column_name)
0
+ index_name = index_name(table_name, :column => column_names.first)
0
 
0
         if Hash === options # legacy support, since this param was a string
0
           index_type = options[:unique] ? "UNIQUE" : ""
0
@@ -192,8 +193,8 @@
0
         else
0
           index_type = options
0
         end
0
-
0
- execute "CREATE #{index_type} INDEX #{index_name} ON #{table_name} (#{Array(column_name).join(", ")})"
0
+ quoted_column_names = column_names.map { |e| quote_column_name(e) }.join(", ")
0
+ execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{table_name} (#{quoted_column_names})"
0
       end
0
 
0
       # Remove the given index from the table.
0
@@ -209,7 +210,7 @@
0
       # add_index :accounts, [:username, :password]
0
       # remove_index :accounts, :username
0
       def remove_index(table_name, options = {})
0
- execute "DROP INDEX #{index_name(table_name, options)} ON #{table_name}"
0
+ execute "DROP INDEX #{quote_column_name(index_name(table_name, options))} ON #{table_name}"
0
       end
0
 
0
       def index_name(table_name, options) #:nodoc:
...
18
19
20
21
22
23
24
...
18
19
20
 
21
22
23
0
@@ -18,7 +18,6 @@
0
         end
0
       end
0
 
0
-
0
       config = config.symbolize_keys
0
       host = config[:host]
0
       port = config[:port]
...
337
338
339
340
341
 
342
343
344
...
337
338
339
 
 
340
341
342
343
0
@@ -337,8 +337,7 @@
0
 
0
       def remove_index(table_name, options) #:nodoc:
0
         execute "DROP INDEX #{index_name(table_name, options)}"
0
- end
0
-
0
+ end
0
 
0
       private
0
         BYTEA_COLUMN_TYPE_OID = 17
...
213
214
215
216
217
218
219
220
221
222
 
223
224
225
...
213
214
215
 
 
 
 
 
 
 
216
217
218
219
0
@@ -213,13 +213,7 @@
0
       end
0
 
0
       def remove_index(table_name, options={}) #:nodoc:
0
- if Hash === options
0
- index_name = options[:name]
0
- else
0
- index_name = "#{table_name}_#{options}_index"
0
- end
0
-
0
- execute "DROP INDEX #{index_name}"
0
+ execute "DROP INDEX #{quote_column_name(index_name(table_name, options))}"
0
       end
0
       
0
       def rename_table(name, new_name)
...
34
35
36
 
37
38
39
40
...
47
48
49
 
50
51
52
53
54
55
 
 
 
 
56
57
58
...
34
35
36
37
38
39
40
41
...
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
0
@@ -34,6 +34,7 @@
0
       Reminder.reset_column_information
0
 
0
       Person.connection.remove_column("people", "last_name") rescue nil
0
+ Person.connection.remove_column("people", "key") rescue nil
0
       Person.connection.remove_column("people", "bio") rescue nil
0
       Person.connection.remove_column("people", "age") rescue nil
0
       Person.connection.remove_column("people", "height") rescue nil
0
0
@@ -47,12 +48,17 @@
0
     def test_add_index
0
       Person.connection.add_column "people", "last_name", :string
0
       Person.connection.add_column "people", "administrator", :boolean
0
+ Person.connection.add_column "people", "key", :string
0
       
0
       assert_nothing_raised { Person.connection.add_index("people", "last_name") }
0
       assert_nothing_raised { Person.connection.remove_index("people", "last_name") }
0
 
0
       assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
0
       assert_nothing_raised { Person.connection.remove_index("people", "last_name") }
0
+
0
+ # quoting
0
+ assert_nothing_raised { Person.connection.add_index("people", ["key"], :name => "key", :unique => true) }
0
+ assert_nothing_raised { Person.connection.remove_index("people", :name => "key") }
0
 
0
       # Sybase adapter does not support indexes on :boolean columns
0
       unless current_adapter?(:SybaseAdapter)

Comments

    No one has commented yet.