Skip to content

Instantly share code, notes, and snippets.

@portableworld
Created December 3, 2009 18:09
Show Gist options
  • Save portableworld/248385 to your computer and use it in GitHub Desktop.
Save portableworld/248385 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/ruby
# This script will be set by crontab to run once a day at midnight
require 'rubygems' # require to bring in other Gems
require 'mysql' # require Mysql gem (2.8.1) to interact with database
require 'activesupport' # To make time calculations easier and more readable
# Establish today's date
today = Date.today # Will output "year-month-day" if sent to_s
# Connect to database
con = Mysql.new('localhost', username, password, database)
# Query the database
result = con.query("SELECT * FROM panels")
# .each to go through all results from database
result.each do |row|
# compare row[2] (date) to 7 days ago
# get the date_made column of current |row|
date_made = row[2]
# change it from a String to a Date
date_made = Date.parse(date_made)
# get how many days ago it was made as an Integer
days_since = (today - date_made).to_i
if days_since > 7 then
# delete entry from database
con.query("DELETE FROM panels WHERE id = '#{row[0]}'}")
# grab all Contents and Links with row[0] and store their IDs in an array
links_results = con.query("SELECT * FROM links WHERE panel_id ='#{row[0]}'")
contents_results = con.query("SELECT * FROM contents WHERE panel_id ='#{row[0]}'")
# now delete all Contents and Links with row[0]
con.query("DELETE FROM contents WHERE panel_id ='#{row[0]}'")
con.query("DELETE FROM links WHERE panel_id ='#{row[0]}'")
# delete all Compilations with Contents and Links IDs
links_results.each do |link|
con.query("DELETE FROM compilations WHERE link_id = '#{link[0]}'")
end
contents_results.each do |content|
con.query("DELETE FROM compilations WHERE content_id = '#{content[0]}'")
end
# delete all Visualations with row[0]
con.query("DELETE FROM visualations WHERE panel_id ='#{row[0]}'")
end
end
# Also delete any showdate_links that are over five business days old
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment