Class: PgShrink::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_shrink/database.rb

Direct Known Subclasses

Postgres

Defined Under Namespace

Classes: Postgres

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Database

Returns a new instance of Database.



75
76
77
# File 'lib/pg_shrink/database.rb', line 75

def initialize(opts = {})
  @opts = opts
end

Instance Method Details

#delete_records(table_name, conditions, exclude_conditions = []) ⇒ Object

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.



48
49
50
# File 'lib/pg_shrink/database.rb', line 48

def delete_records(table_name, conditions, exclude_conditions = [])
  raise "implement in subclass"
end

#filter!Object



62
63
64
# File 'lib/pg_shrink/database.rb', line 62

def filter!
  tables.values.each(&:filter!)
end

#filter_table(table_name, opts = {}) {|table| ... } ⇒ Object

Yields:



12
13
14
15
16
17
18
# 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) ⇒ Object

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



33
34
35
# File 'lib/pg_shrink/database.rb', line 33

def get_records(table_name, opts)
  raise "implement in subclass"
end

#log(message) ⇒ Object



79
80
81
82
83
# File 'lib/pg_shrink/database.rb', line 79

def log(message)
  if @opts[:log]
    puts "#{Time.now}: #{message}"
  end
end

#propagate_delete(opts) ⇒ Object

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



58
59
60
# File 'lib/pg_shrink/database.rb', line 58

def propagate_delete(opts)
  raise "implement in subclass"
end

#records_in_batches(table_name) ⇒ Object

records_in_batches should yield a series of batches # of records.



27
28
29
# File 'lib/pg_shrink/database.rb', line 27

def records_in_batches(table_name)
  raise "implement in subclass"
end

#remove_table(table_name, opts = {}) ⇒ Object



20
21
22
23
24
# 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!Object



66
67
68
# File 'lib/pg_shrink/database.rb', line 66

def sanitize!
  tables.values.each(&:sanitize!)
end

#shrink!Object



70
71
72
73
# File 'lib/pg_shrink/database.rb', line 70

def shrink!
  filter!
  sanitize!
end

#table(table_name) ⇒ Object

table should return a unique table representation for this database.



8
9
10
# File 'lib/pg_shrink/database.rb', line 8

def table(table_name)
  tables[table_name] ||= Table.new(self, table_name)
end

#tablesObject



3
4
5
# File 'lib/pg_shrink/database.rb', line 3

def tables
  @tables ||= {}
end

#update_records(table_name, old_records, new_records) ⇒ Object

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.



40
41
42
# 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) ⇒ Object

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



53
54
# File 'lib/pg_shrink/database.rb', line 53

def vacuum_and_reindex!(table_name)
end