-
Notifications
You must be signed in to change notification settings - Fork 0
Chore/paper trail migration to jsonb columns #592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| class PaperTrailToJson < ActiveRecord::Migration[7.2] | ||
| def change | ||
| rename_column :versions, :object, :old_object | ||
| rename_column :versions, :object_changes, :old_object_changes | ||
|
|
||
| change_table :versions, bulk: true do |t| | ||
| t.jsonb :object | ||
| t.jsonb :object_changes | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,4 +50,37 @@ namespace :data_migrations do | |
| end | ||
| puts "Wrote tmp/data_migrations_report_mission_type_report.csv" | ||
| end | ||
|
|
||
| desc "Move PaperTrail versions from YAML to JSON columns." | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh.. it was YAML. Then text field made sense. |
||
| task paper_trail_to_json: :environment do | ||
| for_real = ENV["FOR_REAL"] == "true" | ||
| batch_size = (ENV["BATCH_SIZE"] || 2000).to_i | ||
| puts "DRY RUN" unless for_real | ||
|
|
||
| scope = PaperTrail::Version.all | ||
| total = scope.count | ||
| processed = 0 | ||
| errors = 0 | ||
| puts "Migrating #{total} PaperTrail versions to JSON (batch_size=#{batch_size})..." | ||
|
|
||
| scope.find_in_batches(batch_size: batch_size) do |batch| | ||
| ActiveRecord::Base.transaction do | ||
| batch.each do |version| | ||
| attrs = {} | ||
| attrs[:object] = PaperTrail::Serializers::YAML.load(version.old_object) if version.old_object.present? | ||
| attrs[:object_changes] = PaperTrail::Serializers::YAML.load(version.old_object_changes) if version.old_object_changes.present? | ||
| version.update_columns(attrs) if for_real | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mmmm.. ok... but this just keeps the old data in a new field.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. YAML loading will load to hash, and that will be saved in new object and object_changes fields as JSON.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then in the next PR, I will remove old fields. |
||
| rescue => e | ||
| errors += 1 | ||
| warn "\n Error parsing version #{version.id}: #{e.message}" | ||
| end | ||
| end | ||
|
|
||
| processed += batch.size | ||
| print "\r #{processed}/#{total} (#{"%.1f" % (processed.to_f / total * 100)}%)" | ||
| end | ||
|
|
||
| warn "\n #{errors} version(s) failed to parse and were skipped." if errors > 0 | ||
| puts "\nFinished migrating PaperTrail versions to JSON columns. You can now remove the old YAML columns with a separate migration." | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a text field? 🤯