Skip to content

Instantly share code, notes, and snippets.

@cstorey
Last active December 20, 2015 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cstorey/6126229 to your computer and use it in GitHub Desktop.
Save cstorey/6126229 to your computer and use it in GitHub Desktop.
A trivial Sequel database cleaner.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment