Class: ActiveRecord::ConnectionAdapters::PostgreSQLVacuum

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/postgresql_extensions/vacuum.rb

Overview

Creates queries for invoking VACUUM.

This class is meant to be used by the PostgreSQLAdapter#vacuum method. VACUUMs can be performed against the database as a whole, on specific tables or on specific columns.

Examples

ActiveRecord::Base.connection.vacuum
# => VACUUM;

ActiveRecord::Base.connection.vacuum(:full => true, :analyze => true)
# => VACUUM FULL; # PostgreSQL < 9.0
# => VACUUM (FULL); # PostgreSQL >= 9.0

ActiveRecord::Base.connection.vacuum(:foos)
# => VACUUM "foos";

ActiveRecord::Base.connection.vacuum(:foos, :columns => [ :bar, :baz ])
# => VACUUM (ANALYZE) "foos" ("bar", "baz");

Options

  • :full, :freeze, :verbose and :analyze are all supported.

  • :columns - specifies the columns to VACUUM. You must specify a table when using this option. This option also forces the :analyze option to true, as PostgreSQL doesn’t like to try and VACUUM a column without analyzing it.

Constant Summary collapse

VACUUM_OPTIONS =
%w{
  FULL FREEZE VERBOSE ANALYZE
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, *args) ⇒ PostgreSQLVacuum

Returns a new instance of PostgreSQLVacuum.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_record/postgresql_extensions/vacuum.rb', line 51

def initialize(base, *args)
  if !args.length.between?(0, 2)
    raise ArgumentError.new("Wrong number of arguments #{args.length} for 0-2")
  end

  options = args.extract_options!
  table = args.first

  if options[:columns]
    if !table
      raise ArgumentError.new("You must specify a table when using the :columns option.")
    end

    options[:analyze] = true
  end

  @base, @table, @options = base, table, options
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



49
50
51
# File 'lib/active_record/postgresql_extensions/vacuum.rb', line 49

def base
  @base
end

#optionsObject

Returns the value of attribute options.



49
50
51
# File 'lib/active_record/postgresql_extensions/vacuum.rb', line 49

def options
  @options
end

#tableObject

Returns the value of attribute table.



49
50
51
# File 'lib/active_record/postgresql_extensions/vacuum.rb', line 49

def table
  @table
end

Instance Method Details

#to_sqlObject Also known as: to_s



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/active_record/postgresql_extensions/vacuum.rb', line 70

def to_sql
  vacuum_options = VACUUM_OPTIONS.select { |o|
    o = o.downcase.to_sym
    self.options[o.to_sym]
  }

  sql = 'VACUUM'

  if !vacuum_options.empty?
    sql << if ActiveRecord::PostgreSQLExtensions.SERVER_VERSION.to_f >= 9.0
      " (#{vacuum_options.join(', ')})"
    else
      ' ' << VACUUM_OPTIONS.collect { |v|
        v.upcase if vacuum_options.include?(v)
      }.compact.join(' ')
    end
  end

  sql << " #{base.quote_table_name(table)}" if self.table

  if options[:columns]
    sql << ' (' << Array.wrap(options[:columns]).collect { |column|
      base.quote_column_name(column)
    }.join(', ') << ')'
  end

  sql
end