Class: DbSchema::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/db_schema/runner.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(changes) ⇒ Runner

Returns a new instance of Runner.



5
6
7
# File 'lib/db_schema/runner.rb', line 5

def initialize(changes)
  @changes = preprocess_changes(changes)
end

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes.



3
4
5
# File 'lib/db_schema/runner.rb', line 3

def changes
  @changes
end

Class Method Details

.add_value_to_enum(change) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/db_schema/runner.rb', line 176

def add_value_to_enum(change)
  if change.add_to_the_end?
    DbSchema.connection.add_enum_value(change.enum_name, change.new_value)
  else
    DbSchema.connection.add_enum_value(change.enum_name, change.new_value, before: change.before)
  end
end

.alter_table(change) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/db_schema/runner.rb', line 92

def alter_table(change)
  DbSchema.connection.alter_table(change.name) do
    Utils.sort_by_class(
      change.fields + change.indices + change.checks,
      [
        DbSchema::Changes::DropPrimaryKey,
        DbSchema::Changes::DropCheckConstraint,
        DbSchema::Changes::DropIndex,
        DbSchema::Changes::DropColumn,
        DbSchema::Changes::RenameColumn,
        DbSchema::Changes::AlterColumnType,
        DbSchema::Changes::AllowNull,
        DbSchema::Changes::DisallowNull,
        DbSchema::Changes::AlterColumnDefault,
        DbSchema::Changes::CreateColumn,
        DbSchema::Changes::CreateIndex,
        DbSchema::Changes::CreateCheckConstraint,
        DbSchema::Changes::CreatePrimaryKey
      ]
    ).each do |element|
      case element
      when Changes::CreateColumn
        if element.primary_key?
          add_primary_key(element.name)
        else
          options = Runner.map_options(element.type, element.options)
          add_column(element.name, element.type.capitalize, options)
        end
      when Changes::DropColumn
        drop_column(element.name)
      when Changes::RenameColumn
        rename_column(element.old_name, element.new_name)
      when Changes::AlterColumnType
        attributes = Runner.map_options(element.new_type, element.new_attributes)
        set_column_type(element.name, element.new_type.capitalize, attributes)
      when Changes::CreatePrimaryKey
        raise NotImplementedError, 'Converting an existing column to primary key is currently unsupported'
      when Changes::DropPrimaryKey
        raise NotImplementedError, 'Removing a primary key while leaving the column is currently unsupported'
      when Changes::AllowNull
        set_column_allow_null(element.name)
      when Changes::DisallowNull
        set_column_not_null(element.name)
      when Changes::AlterColumnDefault
        set_column_default(element.name, element.new_default)
      when Changes::CreateIndex
        add_index(
          element.columns_to_sequel,
          name:   element.name,
          unique: element.unique?,
          type:   element.type,
          where:  element.condition
        )
      when Changes::DropIndex
        drop_index([], name: element.name)
      when Changes::CreateCheckConstraint
        add_constraint(element.name, element.condition)
      when Changes::DropCheckConstraint
        drop_constraint(element.name)
      end
    end
  end
end

.create_enum(change) ⇒ Object



168
169
170
# File 'lib/db_schema/runner.rb', line 168

def create_enum(change)
  DbSchema.connection.create_enum(change.name, change.values)
end

.create_extension(change) ⇒ Object



184
185
186
# File 'lib/db_schema/runner.rb', line 184

def create_extension(change)
  DbSchema.connection.run(%Q(CREATE EXTENSION "#{change.name}"))
end

.create_foreign_key(change) ⇒ Object



156
157
158
159
160
# File 'lib/db_schema/runner.rb', line 156

def create_foreign_key(change)
  DbSchema.connection.alter_table(change.table_name) do
    add_foreign_key(change.foreign_key.fields, change.foreign_key.table, change.foreign_key.options)
  end
end

.create_table(change) ⇒ Object



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
# File 'lib/db_schema/runner.rb', line 61

def create_table(change)
  DbSchema.connection.create_table(change.name) do
    change.fields.each do |field|
      if field.primary_key?
        primary_key(field.name)
      else
        options = Runner.map_options(field.class.type, field.options)
        column(field.name, field.type.capitalize, options)
      end
    end

    change.indices.each do |index|
      index(
        index.columns_to_sequel,
        name:   index.name,
        unique: index.unique?,
        type:   index.type,
        where:  index.condition
      )
    end

    change.checks.each do |check|
      constraint(check.name, check.condition)
    end
  end
end

.drop_enum(change) ⇒ Object



172
173
174
# File 'lib/db_schema/runner.rb', line 172

def drop_enum(change)
  DbSchema.connection.drop_enum(change.name)
end

.drop_extension(change) ⇒ Object



188
189
190
# File 'lib/db_schema/runner.rb', line 188

def drop_extension(change)
  DbSchema.connection.run(%Q(DROP EXTENSION "#{change.name}"))
end

.drop_foreign_key(change) ⇒ Object



162
163
164
165
166
# File 'lib/db_schema/runner.rb', line 162

def drop_foreign_key(change)
  DbSchema.connection.alter_table(change.table_name) do
    drop_foreign_key([], name: change.fkey_name)
  end
end

.drop_table(change) ⇒ Object



88
89
90
# File 'lib/db_schema/runner.rb', line 88

def drop_table(change)
  DbSchema.connection.drop_table(change.name)
end

.map_options(type, options) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/db_schema/runner.rb', line 192

def map_options(type, options)
  mapping = case type
  when :char, :varchar, :bit, :varbit
    Utils.rename_keys(options, length: :size)
  when :numeric
    Utils.rename_keys(options) do |new_options|
      precision, scale = Utils.delete_at(new_options, :precision, :scale)

      if precision
        if scale
          new_options[:size] = [precision, scale]
        else
          new_options[:size] = precision
        end
      end
    end
  when :interval
    Utils.rename_keys(options, precision: :size) do |new_options|
      new_options[:type] = "INTERVAL #{new_options.delete(:fields).upcase}"
    end
  when :array
    Utils.rename_keys(options) do |new_options|
      new_options[:type] = "#{new_options.delete(:element_type)}[]"
    end
  else
    options
  end
end

Instance Method Details

#run!Object



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
# File 'lib/db_schema/runner.rb', line 9

def run!
  DbSchema.connection.transaction do
    changes.each do |change|
      case change
      when Changes::CreateTable
        self.class.create_table(change)
      when Changes::DropTable
        self.class.drop_table(change)
      when Changes::AlterTable
        self.class.alter_table(change)
      when Changes::CreateForeignKey
        self.class.create_foreign_key(change)
      when Changes::DropForeignKey
        self.class.drop_foreign_key(change)
      when Changes::CreateEnum
        self.class.create_enum(change)
      when Changes::DropEnum
        self.class.drop_enum(change)
      when Changes::CreateExtension
        self.class.create_extension(change)
      when Changes::DropExtension
        self.class.drop_extension(change)
      end
    end
  end

  # Postgres doesn't allow modifying enums inside a transaction
  Utils.filter_by_class(changes, Changes::AddValueToEnum).each do |change|
    self.class.add_value_to_enum(change)
  end
end