Skip to content

Instantly share code, notes, and snippets.

@matthieuprat
Last active August 10, 2022 16:54
Show Gist options
  • Save matthieuprat/bc164339e8d6066bf8d598ec508cc0c6 to your computer and use it in GitHub Desktop.
Save matthieuprat/bc164339e8d6066bf8d598ec508cc0c6 to your computer and use it in GitHub Desktop.
version = 3.months.ago.utc.strftime("%Y%m%d%H%M%S")
puts "Deleting migrations before version #{version.inspect}"
migrations =
ActiveRecord::MigrationContext.new(['db/migrate'])
.migrations
.take_while { |migration| migration.version < version }
File.delete(*migrations.map(&:filename))
# frozen_string_literal: true
class DeleteOldSchemaMigrationVersions < ActiveRecord::Migration[5.2]
def up
ActiveRecord::SchemaMigration.where("version < ?", <version>).delete_all
end
def down; end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment