Sha256: 13c9d72c26fbc855b144d99bcb8ec922e59cd4ca73c74ea4af44f9f3f4630a21

Contents?: true

Size: 1.32 KB

Versions: 4

Compression:

Stored size: 1.32 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
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ponytail-0.2.0 lib/ponytail/migration.rb
ponytail-0.1.0 lib/ponytail/migration.rb
ponytail-0.0.5 lib/ponytail/migration.rb
ponytail-0.0.4 lib/ponytail/migration.rb