I'm attempting to run a migration that runs a rake task that destroys old version data generated with paper_trail gem in a Rails 5 app. The migration seemed to work locally in dev env, but it's failing to deploy when pushed to Heroku. Here's the error on Heroku:
...
Deleting records for table: versions...
rake aborted!
NameError: uninitialized constant Version
...
config/initializers/paper_trail.rb
PaperTrail.config.track_associations = false
PaperTrail.config.version_limit = 100
require 'paper_trail/frameworks/active_record/models/paper_trail/version'
require Rails.root.join('./app/controllers/concerns/paper_trail_version_search.rb')
module PaperTrail
class Version < ActiveRecord::Base
extend PaperTrailVersionSearch
end
end
db/migrate/20170530151849_remove_old_versions_table_data.rb
class RemoveOldVersionsTableData < ActiveRecord::Migration[5.0]
def change
Rake::Task.clear
<App Name Redacted>::Application.load_tasks
Rake::Task["remove_old_versions_table_data"].invoke
end
end
lib/tasks/remove_old_versions_table_data.rake
desc 'remove excessive and unnecessary versions data created prior to implementation of version limits'
task remove_old_versions_table_data: :environment do
PaperTrail::Version.where("created_at <= ?", "2017-06-12").delete_all
end
Any thoughts what might be happening?
Given the error message:
NameError: uninitialized constant Version
It's probably because you're re-opening the PaperTrail::Version
class in an initializer. That's no longer the recommended way to re-open Version
. See the changelog for version 4.0.0:
Using a Rails initializer to reopen PaperTrail::Version or otherwise extend PaperTrail is no longer recommended. An alternative is described in the readme. See https://github.com/airblade/paper_trail/pull/557 and https://github.com/airblade/paper_trail/pull/492.
These days, we recommend that people re-open in app/models/paper_trail/version.rb
.
If you re-open in an initializer, Version
might not be defined yet. I don't remember why. Something, something, rails boot order, something, something.