Module: Peegee::Clustering

Included in:
Table
Defined in:
lib/peegee/clustering.rb

Instance Method Summary collapse

Instance Method Details

#cluster(cluster_index = nil) ⇒ Object

Clusters this table. See www.postgresql.org/docs/8.3/interactive/sql-cluster.html Optionally specify the index to cluster by (by name or Peegee::Index object). The index to use would ideally be specified using the Peegee::Configuration instance. If the index is not specified, it will try to induce which index to use by looking at postgresql’s internals. If a proper index can’t be found, an exception will be raised.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/peegee/clustering.rb', line 10

def cluster(cluster_index = nil)
  cluster_index = find_cluster_index(cluster_index)
  puts "Cluster index is: #{cluster_index.inspect}"
  puts "Cluster index type is: #{cluster_index.class.name}"
  dependencies, dependent_foreign_keys = remember_dependencies
  ActiveRecord::Base.transaction do
    dependent_foreign_keys.map { |dfk| dfk.drop }
    create_tmp_table
    move_data(cluster_index)
    drop_original_and_rename_tmp_table
    dependencies.reverse.map { |dep| dep.create }
    dependent_foreign_keys.map { |dfk| dfk.create }
  end
end