public
Last active

A trivial Sequel database cleaner.

  • Download Gist
dbcleaner.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
require 'sequel'
require 'tsort'
 
class TableCleaner
def initialize db, excluded_tables
@db = db
@excluded_tables = excluded_tables
end
 
def clean
@db.transaction do
tables_to_clean do |t|
@db[t].delete
end
end
end
 
private
include TSort
 
def tables_to_clean &block
tsort.reverse.each &block
end
 
def tsort_each_node &block
@db.tables.each do |t|
block.call t unless @excluded_tables.include? t
end
end
 
def tsort_each_child table, &block
@db.foreign_key_list(table).each do |fk|
block.call fk[:table] unless @excluded_tables.include? fk[:table]
end
end
end
 
# Example usage.
require 'logger'
db = Sequel.connect ARGV[0], logger: Logger.new($stderr)
 
cleaner = TableCleaner.new db, [:spatial_ref_sys]
 
cleaner.clean

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.