Sha256: fe8dde1737bd1be94d122d78982f9687fcbe56c02dce65202fac535362aebdc6

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

module DataMigrations
  class Instruction
    class Base
      attr_accessor :columns, :options

      delegate :connection, :to => :'ActiveRecord::Base'
      delegate :condition, :to => :migration

      attr_reader :migration

      def initialize(migration)
        @migration = migration
      end

      def columns=(columns)
        @columns = normalize_columns(columns).map do |column, alias_|
          Column.new(migration.source, column, alias_)
        end
      end

      def execute
        statements.each do |statement|
          puts "Executing: #{statement}"
          connection.execute(statement)
        end
      end

      protected

        def source
          migration.source
        end

        def target
          migration.target
        end

        def alias_names
          columns.map(&:quoted_alias_name).join(', ')
        end

        def alias_setters
          columns.map { |column| "#{column.quoted_alias_name} = source.#{column.quoted_name}" }.join(', ')
        end

        def quoted_column_names
          columns.map(&:quoted_name).join(', ')
        end

        def aliased_column_names
          columns.map(&:aliased_name).join(', ')
        end

        def column_definitions
          columns.map(&:definition).join(', ')
        end

        def normalize_columns(columns)
          columns = columns.map(&:to_s)
          columns = source.column_names - instructed_columns if columns.include?('all')
          columns -= Array(options[:except]).map(&:to_s) if options[:except]
          columns.zip(Array(options[:to]).map(&:to_s))
        end

        def instructed_columns
          migration.instructions.map(&:columns).flatten.map(&:name)
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
data_migrations-0.0.1 lib/data_migrations/instruction/base.rb