Class: Spider::Migrations::RenameElement

Inherits:
Migration show all
Defined in:
lib/spiderfw/model/migrations/rename_element.rb

Instance Method Summary collapse

Constructor Details

#initialize(model, element, new_element, options = {}) ⇒ RenameElement

Returns a new instance of RenameElement.



5
6
7
8
9
10
# File 'lib/spiderfw/model/migrations/rename_element.rb', line 5

def initialize(model, element, new_element, options={})
    @model = model
    @element = element
    @new_element = new_element
    @options = {}
end

Instance Method Details

#runObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/spiderfw/model/migrations/rename_element.rb', line 12

def run
    field = @options[:field_name]
    schema_field = nil
    new_schema_field = nil
    unless field
        schema_field = @model.mapper.schema.field(@element)
        field = schema_field.name if schema_field
    end
    field ||= @model.mapper.storage.column_name(@element)
    new_field = @options[:new_field_name]
    unless new_field
        new_schema_field = @model.mapper.schema.field(@new_element)
        new_field = new_schema_field.name if new_schema_field
    end
    new_field ||= @model.mapper.storage.column_name(@new_element)
    f = new_schema_field || schema_field
    raise "Neither #{@element} nor #{@new_element} were found in schema" unless f

    desc = @model.mapper.storage.describe_table(@model.mapper.schema.table)
    if desc[:columns][field] && !desc[:columns][new_field]
        @model.mapper.storage.change_field(@model.mapper.schema.table, field, new_field, f.type, f.attributes)
    end
end

#undoObject



36
37
38
# File 'lib/spiderfw/model/migrations/rename_element.rb', line 36

def undo
    self.class.new(@model, @new_element, @element).run
end