Guest User

Untitled

a guest
Apr 21st, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #!/usr/local/bin/ruby
  2.  
  3. # This script will be set by crontab to run once a day at midnight
  4.  
  5. require 'rubygems' # require to bring in other Gems
  6. require 'mysql' # require Mysql gem (2.8.1) to interact with database
  7. require 'activesupport' # To make time calculations easier and more readable
  8.  
  9. # Establish today's date
  10. today = Date.today # Will output "year-month-day" if sent to_s
  11.  
  12. # Connect to database
  13. con = Mysql.new('localhost', username, password, database)
  14.  
  15. # Query the database
  16. result = con.query("SELECT * FROM panels")
  17.  
  18. # .each to go through all results from database
  19. result.each do |row|
  20.  
  21. # compare row[2] (date) to 7 days ago
  22. # get the date_made column of current |row|
  23. date_made = row[2]
  24. # change it from a String to a Date
  25. date_made = Date.parse(date_made)
  26. # get how many days ago it was made as an Integer
  27. days_since = (today - date_made).to_i
  28.  
  29. if days_since > 7 then
  30. # delete entry from database
  31. con.query("DELETE FROM panels WHERE id = '#{row[0]}'}")
  32. # grab all Contents and Links with row[0] and store their IDs in an array
  33. links_results = con.query("SELECT * FROM links WHERE panel_id ='#{row[0]}'")
  34. contents_results = con.query("SELECT * FROM contents WHERE panel_id ='#{row[0]}'")
  35. # now delete all Contents and Links with row[0]
  36. con.query("DELETE FROM contents WHERE panel_id ='#{row[0]}'")
  37. con.query("DELETE FROM links WHERE panel_id ='#{row[0]}'")
  38. # delete all Compilations with Contents and Links IDs
  39. links_results.each do |link|
  40. con.query("DELETE FROM compilations WHERE link_id = '#{link[0]}'")
  41. end
  42. contents_results.each do |content|
  43. con.query("DELETE FROM compilations WHERE content_id = '#{content[0]}'")
  44. end
  45. # delete all Visualations with row[0]
  46. con.query("DELETE FROM visualations WHERE panel_id ='#{row[0]}'")
  47.  
  48. end
  49.  
  50. end
  51.  
  52. # Also delete any showdate_links that are over five business days old
Add Comment
Please, Sign In to add comment