Add some extra indexes to the changesets table.
authorTom Hughes <tom@compton.nu>
Mon, 20 Apr 2009 14:54:05 +0000 (14:54 +0000)
committerTom Hughes <tom@compton.nu>
Mon, 20 Apr 2009 14:54:05 +0000 (14:54 +0000)
db/migrate/028_add_more_changeset_indexes.rb [new file with mode: 0644]
lib/migrate.rb

diff --git a/db/migrate/028_add_more_changeset_indexes.rb b/db/migrate/028_add_more_changeset_indexes.rb
new file mode 100644 (file)
index 0000000..d93cc79
--- /dev/null
@@ -0,0 +1,13 @@
+class AddMoreChangesetIndexes < ActiveRecord::Migration
+  def self.up
+    add_index "changesets", ["created_at"], :name => "changesets_created_at_idx"
+    add_index "changesets", ["closed_at"], :name => "changesets_closed_at_idx"
+    add_index "changesets", ["min_lat","max_lat","min_lon","max_lon"], :name => "changesets_bbox_idx", :method => "GIST"
+  end
+
+  def self.down
+    remove_index "changesets", :name => "changesets_bbox_idx"
+    remove_index "changesets", :name => "changesets_closed_at_idx"
+    remove_index "changesets", :name => "changesets_created_at_idx"
+  end
+end
index 392060b6015a8cdb34d5fcfa6172e8a215b05eb4..05b3c90f24da86a9cf541ec9cf60c7e90c9c7520 100644 (file)
@@ -158,6 +158,21 @@ module ActiveRecord
       def interval_constant(interval)
         "'#{interval}'::interval"
       end
+
+      def add_index(table_name, column_name, options = {})
+        column_names = Array(column_name)
+        index_name   = index_name(table_name, :column => column_names)
+
+        if Hash === options # legacy support, since this param was a string
+          index_type = options[:unique] ? "UNIQUE" : ""
+          index_name = options[:name] || index_name
+          index_method = options[:method] || "BTREE"
+        else
+          index_type = options
+        end
+        quoted_column_names = column_names.map { |e| quote_column_name(e) }.join(", ")
+        execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} USING #{index_method} (#{quoted_column_names})"
+      end
     end
   end
 end