shims for running rails 2 with a rails 3 cache

* cache entries are wrapped in rails 3
 * serialized attributes are handled differently
 * stub out a bunch of classes that will just be ignored
   on the returned object

Change-Id: Iff15a0f5c39cd7ceae25144585539acbfc7a62ad
Reviewed-on: https://gerrit.instructure.com/31717
Reviewed-by: James Williams  <jamesw@instructure.com>
Tested-by: Jenkins <jenkins@instructure.com>
Product-Review: Cody Cutrer <cody@instructure.com>
QA-Review: Cody Cutrer <cody@instructure.com>
This commit is contained in:
Cody Cutrer 2014-03-11 10:32:39 -06:00
parent 0216ac2018
commit 89eb243087
2 changed files with 89 additions and 1 deletions

View File

@ -1861,7 +1861,91 @@ ActiveRecord::ConnectionAdapters::SchemaStatements.class_eval do
end
# shims so that AR objects serialized under rails 2 function under rail s3
unless CANVAS_RAILS2
if CANVAS_RAILS2
class ActiveSupport::Cache::Entry
def value
Marshal.load(@compressed ? Zlib::Inflate.inflate(@value) : @value) if @value
end
end
ActiveSupport::Cache::Store.subclasses.map(&:constantize).each do |subclass|
subclass.class_eval do
def read_with_rails3_shim(*args)
result = read_without_rails3_shim(*args)
result = result.value if ActiveSupport::Cache::Entry === result
result
end
alias_method_chain :read, :rails3_shim
end
end
module ActiveRecord::AttributeMethods::Serialization; end
class ActiveRecord::AttributeMethods::Serialization::Attribute < Struct.new(:coder, :value, :state)
def unserialize
return nil if self.value.nil?
self.value = self.coder.load(self.value)
end
end
module ActiveRecord::Coders; end
class ActiveRecord::Coders::YAMLColumn
def load(yaml)
return @object_class.new if @object_class != Object && yaml.nil?
return yaml unless yaml.is_a?(String) && yaml =~ /^---/
begin
obj = YAML.load(yaml)
unless obj.is_a?(@object_class) || obj.nil?
raise SerializationTypeMismatch,
"Attribute was supposed to be a #{object_class}, but was a #{obj.class}"
end
obj ||= @object_class.new if @object_class != Object
obj
rescue ArgumentError
yaml
end
end
end
class ActiveRecord::Base
def unserialize_attribute_with_rails3_shim(attr_name)
@attributes[attr_name] = @attributes[attr_name].unserialize if ActiveRecord::AttributeMethods::Serialization::Attribute === @attributes[attr_name]
unserialize_attribute_without_rails3_shim(attr_name)
end
alias_method_chain :unserialize_attribute, :rails3_shim
end
class ActiveRecord::Relation
end
module Arel; end
class Arel::TreeManager; end
class Arel::SelectManager < Arel::TreeManager; end
class Arel::Table; end
module Arel::Attributes; end
class Arel::Attributes::Attribute < Struct.new :relation, :name; end
module Arel::Nodes; end
class Arel::Nodes::Node; end
class Arel::Nodes::And < Arel::Nodes::Node; end
class Arel::Nodes::Binary < Arel::Nodes::Node; end
class Arel::Nodes::Equality < Arel::Nodes::Binary; end
class Arel::Nodes::JoinSource < Arel::Nodes::Binary; end
class Arel::Nodes::SelectCore < Arel::Nodes::Node; end
class Arel::Nodes::SelectStatement < Arel::Nodes::Node; end
class Arel::Nodes::SqlLiteral < String; end
module Switchman; end
class Switchman::Shard < ActiveRecord::Base
attr_accessible
def self._load(str)
Shard.lookup(str.to_i)
end
end
else
ActiveRecord::AttributeMethods::Serialization::ClassMethods.class_eval do
def attribute_cast_code(attr_name)
if serialized_attributes.include?(attr_name)

View File

@ -46,6 +46,10 @@ class Shard
default
end
def self.lookup(id)
default
end
def self.partition_by_shard(array, partition_proc = nil)
return [] if array.empty?
Array(yield array)