Search code examples
ruby-on-railsrubyruby-on-rails-4rails-activerecordrails-migrations

Ruby on Rails purge old rows from database after a given amount of time


I have a ruby on rails server that maintains images for visual automation tests for a mobile application. A developer runs the visual automation on each pull request so he or she knows exactly what is going into the repo.

It has been over a month of production use and I am starting to think about how to remove old builds or old images so I can save space (and time for some of the each loops). The active record is as follows:

class Project < ActiveRecord::Base
  has_many :test_suites
  has_many :builds
end

The migration looks like this:

class AddProjectToBuild < ActiveRecord::Migration
  def change
    add_reference :builds, :project, index: true
  end
end

I want to be able to remove builds if they are over one month old. How might I go about executing this in a production environment without touching the builds that have run in the last month?

Any help would be much appreciated.


Solution

  • I would follow the following steps to do this:

    • Create a rake task which contains the logic and query to delete your old records.

    • Schedule the task with a gem such as whenever or rufus-scheduler on your production server.