Sha256: db2b789c0f235d09452309a5091b3e380a183f02b08ad4c99443874871448414

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

module Ponytail
  class Migration
    include ActiveModel::Model
    attr_accessor :name, :filename, :version, :raw_content

    validates :name, presence: true
    validates :raw_content, presence: true

    class << self
      def check_pending?
        ActiveRecord::VERSION::MAJOR >= 4 && Rails.application.config.middleware.include?(ActiveRecord::Migration::CheckPending)
      end

      def all
        proxys = ActiveRecord::Migrator.migrations(migrations_paths)
        proxys.map { |p| new(name: p.name, filename: p.filename, version: p.version) }
      end
      delegate :migrations_paths, :migrations_path, :current_version, to: ActiveRecord::Migrator

      def migrate
        ActiveRecord::Migrator.migrate(migrations_paths)
      end

      def rollback
        ActiveRecord::Migrator.rollback(migrations_paths)
      end

      def next_version
        last = all.last
        ActiveRecord::Migration.next_migration_number(last ? last.version + 1 : 0).to_i
      end
    end

    def raw_content
      @raw_content ||= (filename != nil ? open(filename).read : nil)
    end

    def save
      if valid?
        next_filename = "#{Migration.migrations_path}/#{Migration.next_version}_#{name.underscore}.rb"
        open(next_filename, 'w') { |f| f.write(raw_content) }
        true
      else
        false
      end
    end

    def destroy
      File.delete(filename)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ponytail-0.4.0 lib/ponytail/migration.rb
ponytail-0.3.0 lib/ponytail/migration.rb