Sha256: dec69c599248b145b928c5f54973c7ca30124cd0fc02c28ee08045a5e3f3d7c8

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

# TODO: move to dm-more/dm-migrations

module DataMapper
  class AutoMigrator
    ##
    # Destructively automigrates the data-store to match the model
    # REPEAT: THIS IS DESTRUCTIVE
    #
    # @param Symbol repository_name the repository to be migrated
    # @calls DataMapper::Resource#auto_migrate!
    def self.auto_migrate(repository_name = nil)
      DataMapper::Resource.descendants.each do |model|
        model.auto_migrate!(repository_name)
      end
    end

    ##
    # Safely migrates the data-store to match the model
    # preserving data already in the data-store
    #
    # @param Symbol repository_name the repository to be migrated
    # @calls DataMapper::Resource#auto_upgrade!
    def self.auto_upgrade(repository_name = nil)
      DataMapper::Resource.descendants.each do |model|
        model.auto_upgrade!(repository_name)
      end
    end
  end # class AutoMigrator

  module AutoMigrations
    ##
    # Destructively automigrates the data-store to match the model
    # REPEAT: THIS IS DESTRUCTIVE
    #
    # @param Symbol repository_name the repository to be migrated
    def auto_migrate!(repository_name = nil)
      if self.superclass != Object
        self.superclass.auto_migrate!(repository_name)
      else
        repository(repository_name) do |r|
          r.adapter.destroy_model_storage(r, self)
          r.adapter.create_model_storage(r, self)
        end
      end
    end

    ##
    # Safely migrates the data-store to match the model
    # preserving data already in the data-store
    #
    # @param Symbol repository_name the repository to be migrated
    def auto_upgrade!(repository_name = nil)
      repository(repository_name) do |r|
        r.adapter.upgrade_model_storage(r, self)
      end
    end

    Model.send(:include, self)
  end # module AutoMigrations
end # module DataMapper

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dm-core-0.9.3 lib/dm-core/auto_migrations.rb