| | 289 | # Adds a new index to the table. +column_name+ can be a single Symbol, |
|---|
| | 290 | # a single Hash, or an array of Symbols / Hashes. |
|---|
| | 291 | # |
|---|
| | 292 | # The index will be named after the table and the first column names, |
|---|
| | 293 | # unless you pass +:name+ as an option. |
|---|
| | 294 | # |
|---|
| | 295 | # When creating an index on multiple columns, the first column is used as a name |
|---|
| | 296 | # for the index. For example, when you specify an index on two columns |
|---|
| | 297 | # [+:first+, +:last+], the DBMS creates an index for both columns as well as an |
|---|
| | 298 | # index for the first colum +:first+. Using just the first name for this index |
|---|
| | 299 | # makes sense, because you will never have to create a singular index with this |
|---|
| | 300 | # name. |
|---|
| | 301 | # |
|---|
| | 302 | # ===== Examples |
|---|
| | 303 | # ====== Creating a simple index |
|---|
| | 304 | # add_index(:suppliers, :name) |
|---|
| | 305 | # generates |
|---|
| | 306 | # CREATE INDEX suppliers_name_index ON suppliers(`name`) |
|---|
| | 307 | # ====== Creating a unique index |
|---|
| | 308 | # add_index(:accounts, [:branch_id, :party_id], :unique => true) |
|---|
| | 309 | # generates |
|---|
| | 310 | # CREATE UNIQUE INDEX accounts_branch_id_index ON accounts(`branch_id`, `party_id`) |
|---|
| | 311 | # ====== Creating a named index |
|---|
| | 312 | # add_index(:accounts, [:branch_id, :party_id], :unique => true, :name => 'by_branch_party') |
|---|
| | 313 | # generates |
|---|
| | 314 | # CREATE UNIQUE INDEX by_branch_party ON accounts(`branch_id`, `party_id`) |
|---|
| | 315 | # ====== Creating an index with a prefix |
|---|
| | 316 | # add_index(:comments, {:name => :body, :length => 200}) |
|---|
| | 317 | # generates |
|---|
| | 318 | # CREATE INDEX comments_body_index ON comments(`body`(200)) |
|---|
| | 319 | # ====== Creating a multi-column index with mixed prefixed and non-prefixed fields |
|---|
| | 320 | # add_index(:comments, [{:name => :body, :length => 200}, :type], :name => 'body_type_idx' |
|---|
| | 321 | # generates |
|---|
| | 322 | # CREATE INDEX body_type_idx ON comments(`body`(200), `length`) |
|---|
| | 323 | def add_index(table_name, column_name, options = {}) |
|---|
| | 324 | column_names = column_name.kind_of?(Hash) ? [column_name] : Array(column_name) |
|---|
| | 325 | index_name = index_name(table_name, :column => column_names.first.to_s) |
|---|
| | 326 | |
|---|
| | 327 | if Hash === options # legacy support, since this param was a string |
|---|
| | 328 | index_type = options[:unique] ? "UNIQUE" : "" |
|---|
| | 329 | index_name = options[:name] || index_name |
|---|
| | 330 | else |
|---|
| | 331 | index_type = options |
|---|
| | 332 | end |
|---|
| | 333 | quoted_column_names = column_names.map { |e| |
|---|
| | 334 | if Hash === e |
|---|
| | 335 | quote_column_name(e[:name]) + (e[:length] ? "(#{e[:length]})" : "") |
|---|
| | 336 | else |
|---|
| | 337 | quote_column_name(e) |
|---|
| | 338 | end |
|---|
| | 339 | }.join(", ") |
|---|
| | 340 | execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{table_name} (#{quoted_column_names})" |
|---|
| | 341 | end |
|---|
| | 342 | |
|---|