Class: ROM::SQL::Migration::Runner Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/sql/migration/runner.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(writer) ⇒ Runner

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Runner.


10
11
12
# File 'lib/rom/sql/migration/runner.rb', line 10

def initialize(writer)
  @writer = writer
end

Instance Attribute Details

#writerObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


8
9
10
# File 'lib/rom/sql/migration/runner.rb', line 8

def writer
  @writer
end

Instance Method Details

#alter_foreign_keys(diff, foreign_key_changes) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rom/sql/migration/runner.rb', line 98

def alter_foreign_keys(diff, foreign_key_changes)
  return if foreign_key_changes.empty?

  writer.migration do |connection|
    connection.alter_table(diff.table_name) do
      foreign_key_changes.map do |fk|
        case fk
        when SchemaDiff::ForeignKeyAdded
          add_foreign_key fk.child_keys, fk.parent
        when SchemaDiff::ForeignKeyRemoved
          drop_foreign_key fk.child_keys
        end
      end
    end
  end
end

#alter_table(diff) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


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
# File 'lib/rom/sql/migration/runner.rb', line 57

def alter_table(diff)
  return if diff.meta?

  writer.migration do |connection|
    connection.alter_table(diff.table_name) do
      diff.attribute_changes.each do |attribute|
        case attribute
        when SchemaDiff::AttributeAdded
          add_column attribute.name, attribute.type, null: attribute.null?
        when SchemaDiff::AttributeRemoved
          drop_column attribute.name
        when SchemaDiff::AttributeChanged
          if attribute.type_changed?
            from, to = attribute.current.unwrap, attribute.target.unwrap
            raise UnsupportedConversion.new(
              "Don't know how to convert #{from.inspect} to #{to.inspect}"
            )
          end

          if attribute.nullability_changed?
            if attribute.null?
              set_column_allow_null attribute.name
            else
              set_column_not_null attribute.name
            end
          end
        end
      end

      diff.index_changes.each do |index|
        case index
        when SchemaDiff::IndexAdded
          add_index index.attributes, index.options
        when SchemaDiff::IndexRemoved
          drop_index index.attributes, index.options
        end
      end
    end
  end
end

#apply_constraints(diff) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


30
31
32
33
34
35
36
37
# File 'lib/rom/sql/migration/runner.rb', line 30

def apply_constraints(diff)
  case diff
  when SchemaDiff::TableCreated
    alter_foreign_keys(diff, diff.foreign_keys)
  when SchemaDiff::TableAltered
    alter_foreign_keys(diff, diff.foreign_key_changes)
  end
end

#apply_schema(diff) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


21
22
23
24
25
26
27
28
# File 'lib/rom/sql/migration/runner.rb', line 21

def apply_schema(diff)
  case diff
  when SchemaDiff::TableCreated
    create_table(diff)
  when SchemaDiff::TableAltered
    alter_table(diff)
  end
end

#call(changes) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


14
15
16
17
18
19
# File 'lib/rom/sql/migration/runner.rb', line 14

def call(changes)
  changes.each { |diff| apply_schema(diff) }
  changes.each { |diff| apply_constraints(diff) }

  self
end

#create_table(diff) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rom/sql/migration/runner.rb', line 39

def create_table(diff)
  writer.migration do |connection|
    connection.create_table(diff.table_name) do
      diff.attributes.each do |attribute|
        if attribute.primary_key?
          primary_key attribute.name
        else
          column attribute.name, attribute.type, null: attribute.null?
        end
      end

      diff.indexes.each do |index|
        index index.attributes, index.options
      end
    end
  end
end