Class: ActiveRecord::ConnectionAdapters::PostgreSQLVacuum
- Inherits:
-
Object
- Object
- ActiveRecord::ConnectionAdapters::PostgreSQLVacuum
- 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
-
#base ⇒ Object
Returns the value of attribute base.
-
#options ⇒ Object
Returns the value of attribute options.
-
#table ⇒ Object
Returns the value of attribute table.
Instance Method Summary collapse
-
#initialize(base, *args) ⇒ PostgreSQLVacuum
constructor
A new instance of PostgreSQLVacuum.
- #to_sql ⇒ Object (also: #to_s)
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 = args. table = args.first if [:columns] if !table raise ArgumentError.new("You must specify a table when using the :columns option.") end [:analyze] = true end @base, @table, @options = base, table, end |
Instance Attribute Details
#base ⇒ Object
Returns the value of attribute base.
49 50 51 |
# File 'lib/active_record/postgresql_extensions/vacuum.rb', line 49 def base @base end |
#options ⇒ Object
Returns the value of attribute options.
49 50 51 |
# File 'lib/active_record/postgresql_extensions/vacuum.rb', line 49 def @options end |
#table ⇒ Object
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_sql ⇒ Object 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.select { |o| o = o.downcase.to_sym self.[o.to_sym] } sql = 'VACUUM' if !.empty? sql << if ActiveRecord::PostgreSQLExtensions.SERVER_VERSION.to_f >= 9.0 " (#{.join(', ')})" else ' ' << VACUUM_OPTIONS.collect { |v| v.upcase if .include?(v) }.compact.join(' ') end end sql << " #{base.quote_table_name(table)}" if self.table if [:columns] sql << ' (' << Array.wrap([:columns]).collect { |column| base.quote_column_name(column) }.join(', ') << ')' end sql end |