Sha256: 266103a3d3a4b8c6718242ec413263ca94689909c1ecdb03c86f6baef93b1200

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

module DataType
  class DangerousMigration < Exception; end;

  class Base
    attr_accessor :aliases
  
    # Pass the developer's ActiveRecord::Base structure and we'll
    # decide the best structure
    def initialize(options={})
      @options = options
      @value = options.delete(:value)
      @field = options.delete(:field)
      @aliases = options.delete(:was) || Array.new
    end
    
    # Default is 'ye good ol varchar(255)
    def column
      {:type => :string}.merge(@options)
    end
    
    def mock
      @value || self.class.default_mock
    end
    
    def self.default_mock
      "Some string"
    end
    
    # Decides if and how a column will be changed
    # Provide the details of a previously column, or simply nil to create a new column
    def structure_changes_from(current_structure = nil)    
      new_structure = column
    
      if current_structure
        # General RDBMS data loss scenarios
        raise DataType::DangerousMigration if (new_structure[:type] != :text && [:string, :text].include?(current_structure[:type]) && new_structure[:type] != current_structure[:type])

        if new_structure[:limit] && current_structure[:limit].to_i != new_structure[:limit].to_i ||
           new_structure[:default] && current_structure[:default].to_s != new_structure[:default].to_s ||
           new_structure[:type] != current_structure[:type]
           column
        else
          nil # No changes
        end
      else
        column
      end
    end
    
    def self.migrant_data_type?; true; end
  end
end

# And all the data types we offer...
require 'datatype/boolean'
require 'datatype/currency'
require 'datatype/date'
require 'datatype/fixnum'
require 'datatype/float'
require 'datatype/foreign_key'
require 'datatype/hash'
require 'datatype/polymorphic'
require 'datatype/range'
require 'datatype/string'
require 'datatype/symbol'
require 'datatype/time'

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
migrant-0.1.5 lib/datatype/base.rb
migrant-0.1.4 lib/datatype/base.rb
migrant-0.1.3 lib/datatype/base.rb