class PgShrink::Database

Public Class Methods

new(opts = {}) click to toggle source
# File lib/pg_shrink/database.rb, line 75
def initialize(opts = {})
  @opts = opts
end

Public Instance Methods

delete_records(table_name, conditions, exclude_conditions = []) click to toggle source

The delete_records method takes a table name a condition to delete on, and a condition to prevent deletion on. This can be used to combine a targeted deletion with exclusions or to delete an entire table but for some exclusions by passing no conditions but some exclude conditions.

# File lib/pg_shrink/database.rb, line 48
def delete_records(table_name, conditions, exclude_conditions = [])
  raise "implement in subclass"
end
filter!() click to toggle source
# File lib/pg_shrink/database.rb, line 62
def filter!
  tables.values.each(&:filter!)
end
filter_table(table_name, opts = {}) { |table| ... } click to toggle source
# File lib/pg_shrink/database.rb, line 12
def filter_table(table_name, opts = {})
  table = self.table(table_name)
  # we want to allow composability of filter specifications, so we always
  # update existing options rather than overriding
  table.update_options(opts)
  yield table if block_given?
end
get_records(table_name, opts) click to toggle source

get_records should take a table name and options hash and return a specific set of records

# File lib/pg_shrink/database.rb, line 33
def get_records(table_name, opts)
  raise "implement in subclass"
end
log(message) click to toggle source
# File lib/pg_shrink/database.rb, line 79
def log(message)
  if @opts[:log]
    puts "#{Time.now}: #{message}"
  end
end
propagate_delete(opts) click to toggle source

This is kind of a leaky abstraction b/c I’m not sure how this would work outside of sql

# File lib/pg_shrink/database.rb, line 58
def propagate_delete(opts)
  raise "implement in subclass"
end
records_in_batches(table_name) click to toggle source

records_in_batches should yield a series of batches # of records.

# File lib/pg_shrink/database.rb, line 27
def records_in_batches(table_name)
  raise "implement in subclass"
end
remove_table(table_name, opts = {}) click to toggle source
# File lib/pg_shrink/database.rb, line 20
def remove_table(table_name, opts = {})
  table = self.table(table_name)
  table.update_options(opts)
  table.mark_for_removal!
end
sanitize!() click to toggle source
# File lib/pg_shrink/database.rb, line 66
def sanitize!
  tables.values.each(&:sanitize!)
end
shrink!() click to toggle source
# File lib/pg_shrink/database.rb, line 70
def shrink!
  filter!
  sanitize!
end
table(table_name) click to toggle source

table should return a unique table representation for this database.

# File lib/pg_shrink/database.rb, line 8
def table(table_name)
  tables[table_name] ||= Table.new(self, table_name)
end
tables() click to toggle source
# File lib/pg_shrink/database.rb, line 3
def tables
  @tables ||= {}
end
update_records(table_name, old_records, new_records) click to toggle source

The update_records method takes a set of original records and a new set of records. It should throw an error if there are any records missing, so it should not be used for deletion.

# File lib/pg_shrink/database.rb, line 40
def update_records(table_name, old_records, new_records)
  raise "implement in subclass"
end
vacuum_and_reindex!(table_name) click to toggle source

vacuum and reindex is pg specific… do nothing in other cases

# File lib/pg_shrink/database.rb, line 53
def vacuum_and_reindex!(table_name)
end