Sha256: 20f7ca1a24e1db2f4c9994cdc6560d99ca82b7f2e2943fee0768bf43c16fc234

Contents?: true

Size: 1.85 KB

Versions: 6

Compression:

Stored size: 1.85 KB

Contents

module CassandraObject
  module Migrations
    extend ActiveSupport::Concern
    extend ActiveSupport::Autoload

    included do
      class_inheritable_array :migrations
      class_inheritable_accessor :current_schema_version
      self.current_schema_version = 0
    end

    autoload :Migration
    
    class MigrationNotFoundError < StandardError
      def initialize(record_version, migrations)
        super("Cannot migrate a record from #{record_version.inspect}.  Migrations exist for #{migrations.map(&:version)}")
      end
    end
    
    module InstanceMethods
      def schema_version
        Integer(@schema_version || self.class.current_schema_version)
      end
    end
    
    module ClassMethods
      def migrate(version, &blk)
        write_inheritable_array(:migrations, [Migration.new(version, blk)])
        
        if version > self.current_schema_version 
          self.current_schema_version = version
        end
      end
      
      def instantiate(key, attributes)
        version = attributes.delete('schema_version')
        original_attributes = attributes.dup
        if version == current_schema_version
          return super(key, attributes)
        end
        
        versions_to_migrate = ((version.to_i + 1)..current_schema_version)
        
        migrations_to_run = versions_to_migrate.map do |v|
          migrations.find {|m| m.version == v}
        end
        
        if migrations_to_run.any?(&:nil?)
          raise MigrationNotFoundError.new(version, migrations)
        end
        
        migrations_to_run.inject(attributes) do |attrs, migration|
          migration.run(attrs)
          @schema_version = migration.version.to_s
          attrs
        end
        
        super(key, attributes).tap do |record|
          record.attributes_changed!(original_attributes.diff(attributes).keys)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
gotime-cassandra_object-0.9.1 lib/cassandra_object/migrations.rb
gotime-cassandra_object-0.9.0 lib/cassandra_object/migrations.rb
gotime-cassandra_object-0.8.7 lib/cassandra_object/migrations.rb
gotime-cassandra_object-0.8.6 lib/cassandra_object/migrations.rb
gotime-cassandra_object-0.8.5 lib/cassandra_object/migrations.rb
gotime-cassandra_object-0.8.4 lib/cassandra_object/migrations.rb