Permalink
Browse files

create_index helper. WARNING: no specs. use at your own risk

  • Loading branch information...
1 parent a86799a commit 6bb88825ea747c6344774fbfdfd5a46b16a1305e @paul paul committed Sep 26, 2008
Showing with 23 additions and 0 deletions.
  1. +23 −0 lib/migration.rb
View
@@ -98,6 +98,22 @@ def modify_table(table_name, opts = {}, &block)
end
end
+ def create_index(table_name, *columns_and_options)
+ if columns_and_options.last.is_a?(Hash)
+ opts = columns_and_options.pop
+ else
+ opts = {}
+ end
+ columns = columns_and_options
+
+ opts[:name] ||= "#{opts[:unique] ? 'unique_' : ''}index_#{table_name}_#{columns.join('_')}"
+
+ execute <<-SQL.compress_lines
+ CREATE #{opts[:unique] ? 'UNIQUE ' : '' }INDEX #{quote_column_name(opts[:name])} ON
+ #{quote_table_name(table_name)} (#{columns.map { |c| quote_column_name(c) }.join(', ') })
+ SQL
+ end
+
# Orders migrations by position, so we know what order to run them in.
# First order by postition, then by name, so at least the order is predictable.
def <=> other
@@ -186,5 +202,12 @@ def migration_name_column
@migration_name_column ||= @adapter.send(:quote_column_name, 'migration_name')
end
+ def quote_table_name(table_name)
+ @adapter.send(:quote_table_name, table_name.to_s)
+ end
+
+ def quote_column_name(column_name)
+ @adapter.send(:quote_column_name, column_name.to_s)
+ end
end
end

0 comments on commit 6bb8882

Please sign in to comment.