Module: Trains::Utils::MigrationTailor

Defined in:
lib/trains/utils/migration_tailor.rb

Overview

Combine multiple migrations into models

Class Method Summary collapse

Class Method Details

.stitch(models = {}, migrations) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/trains/utils/migration_tailor.rb', line 7

def self.stitch(models = {}, migrations)
  migrations.each do |mig|
    case mig.modifier
    when :create_table, :create_join_table
      if models.key?(mig.table_name)
        models[mig.table_name].fields.push(*mig.fields)
      else
        models[mig.table_name] = Trains::DTO::Model.new(
          name: mig.table_name,
          fields: mig.fields,
          version: mig.version
        )
      end
    when :add_column, :add_column_with_default, :add_reference
      models[mig.table_name].fields.push(*mig.fields)
    when :ignore_column
      models[mig.table_name].ignored_columns.push(*mig.fields.map(&:name))
    when :remove_column
      column =
        models[mig.table_name].fields.find do |field|
          field.name == mig.fields.first.name
        end
      models[mig.table_name].fields.delete(column)
      models[mig.table_name].removed_columns.push(mig.fields.first.name)
    when :rename_column
      column =
        models[mig.table_name].fields.find do |field|
          field.name == mig.fields.first.name
        end
      models[mig.table_name].fields.push(
        Trains::DTO::Field.new(
          name: mig.fields.first.type.to_sym,
          type: column.type
        )
      )
      models[mig.table_name].fields.delete(column)
      models[mig.table_name].renamed_columns.push(
        Trains::DTO::Rename.new(
          from: mig.fields.first.name.to_sym, to: mig.fields.first.type.to_sym
        )
      )
    when :change_table
      mig.fields.each do |field|
        case field.type
        when :remove
          column =
            models[mig.table_name].fields.find do |mod_field|
              mod_field.name == field.name
            end
          models[mig.table_name].fields.delete(column)
          models[mig.table_name].removed_columns.push(field.name)
        when :rename
          # find the field and store temporarily
          column =
            models[mig.table_name].fields.find do |mod_field|
              mod_field.name == field.name[0]
            end
          # Create new field from temp with new name
          models[mig.table_name].fields.push(
            Trains::DTO::Field.new(
              name: field.name[1].to_sym,
              type: column.type
            )
          )
          # Delete the field
          models[mig.table_name].fields.delete(column)
          models[mig.table_name].renamed_columns.push(
            Trains::DTO::Rename.new(
              from: field.name[0], to: field.name[1]
            )
          )
        else
          models[mig.table_name].fields.push(field)
        end
      end
    when :change_column
      # get column
      column =
        models[mig.table_name].fields.find do |field|
          field.name == mig.fields.first.name
        end
      # replace it with new column object
      models[mig.table_name].fields.delete(column)

      models[mig.table_name].fields << mig.fields.first
    else
      next
    end

  rescue NoMethodError
    next
  end

  models
end