Skip to content

Instantly share code, notes, and snippets.

@abrom
Created January 31, 2018 03:14
Show Gist options
  • Save abrom/31fdd46413eae6aed559c00edf2f8b60 to your computer and use it in GitHub Desktop.
Save abrom/31fdd46413eae6aed559c00edf2f8b60 to your computer and use it in GitHub Desktop.
PT 4 -> PT 5+ serialized attributes problem
# frozen_string_literal: true
require 'securerandom'
$test_db_name = "test_#{SecureRandom.rand(10_000)}.db"
puts "Using #{$test_db_name} database"
# Let's make sure it definitely doesn't exist
File.delete $test_db_name if File.exist? $test_db_name
def setup_test(paper_trail_version:)
puts "\n\n**************************"
puts "Testing paper trail v#{paper_trail_version}"
puts "**************************\n\n"
# Use this template to report PaperTrail bugs.
# Please include only the minimum code necessary to reproduce your issue.
require "bundler/inline"
# STEP ONE: What versions are you using?
gemfile(true) do
ruby "2.4.2"
source "https://rubygems.org"
gem "activerecord", "4.2.10"
gem "minitest", "5.10.3"
gem "paper_trail", paper_trail_version, require: false
gem "sqlite3"
end
require "active_record"
require "minitest/autorun"
require "logger"
# Please use sqlite for your bug reports, if possible.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: $test_db_name)
yield if block_given?
require "paper_trail/config"
# STEP THREE: Configure PaperTrail as you would in your initializer
PaperTrail::Config.instance.track_associations = true
require "paper_trail"
end
fork do
setup_test paper_trail_version: '4.0.0' do
ActiveRecord::Base.logger = nil
ActiveRecord::Schema.define do
# STEP TWO: Define your tables here.
create_table :users, force: true do |t|
t.text :props
t.timestamps null: false
end
create_table :versions do |t|
t.string :item_type, null: false
t.integer :item_id, null: false
t.string :event, null: false
t.string :whodunnit
t.text :object, limit: 1_073_741_823
t.text :object_changes, limit: 1_073_741_823
t.integer :transaction_id
t.datetime :created_at
end
add_index :versions, %i[item_type item_id]
add_index :versions, [:transaction_id]
create_table :version_associations do |t|
t.integer :version_id
t.string :foreign_key_name, null: false
t.integer :foreign_key_id
end
add_index :version_associations, [:version_id]
add_index :version_associations, %i[foreign_key_name foreign_key_id],
name: "index_version_associations_on_foreign_key"
end
ActiveRecord::Base.logger = Logger.new(STDOUT)
end
# STEP FOUR: Define your AR models here.
class User < ActiveRecord::Base
has_paper_trail
serialize :props, Hash
end
# STEP FIVE: Please write a test that demonstrates your issue.
class BugTest < ActiveSupport::TestCase
def test_1
# Create a version record (containing a serialized column) with PT 4
user = User.create props: { foo: 'bar' }
user.update props: { bar: 'baz' }
assert_equal({ 'foo' => 'bar' }, user.previous_version.props)
end
end
end
Process.wait
fork do
setup_test paper_trail_version: '5.0.0'
# STEP FOUR: Define your AR models here.
class User < ActiveRecord::Base
has_paper_trail
serialize :props, Hash
end
# STEP FIVE: Please write a test that demonstrates your issue.
class BugTest < ActiveSupport::TestCase
def test_1
# Load a version record (containing a serialized column) with PT 5 (created with PT 4)
assert_raises(ActiveRecord::SerializationTypeMismatch) { User.last.previous_version }
end
end
end
Process.wait
# Let's clean up after ourselves
File.delete $test_db_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment