class ActiveRecord::ConnectionAdapters::PostgreSQLVacuum
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.
Constants
- VACUUM_OPTIONS
Attributes
base[RW]
options[RW]
table[RW]
Public Class Methods
new(base, *args)
click to toggle source
# 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
Public Instance Methods
to_sql()
click to toggle source
# 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
Also aliased as: to_s