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, connection) ⇒ Runner

Returns a new instance of Runner.



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

def initialize(changes, connection)
  @changes    = changes
  @connection = connection
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

#connectionObject (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

Class Method Details

.default_to_sequel(default) ⇒ Object



239
240
241
242
243
244
245
# File 'lib/db_schema/runner.rb', line 239

def default_to_sequel(default)
  if default.is_a?(Symbol)
    Sequel.lit(default.to_s)
  else
    default
  end
end

.map_options(type, options) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/db_schema/runner.rb', line 204

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

  if mapping.key?(:default)
    mapping.merge(default: default_to_sequel(mapping[:default]))
  else
    mapping
  end
end

Instance Method Details

#run!Object



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

def run!
  changes.each do |change|
    case change
    when Operations::CreateTable
      create_table(change)
    when Operations::DropTable
      drop_table(change)
    when Operations::RenameTable
      rename_table(change)
    when Operations::AlterTable
      alter_table(change)
    when Operations::CreateForeignKey
      create_foreign_key(change)
    when Operations::DropForeignKey
      drop_foreign_key(change)
    when Operations::CreateEnum
      create_enum(change)
    when Operations::DropEnum
      drop_enum(change)
    when Operations::RenameEnum
      rename_enum(change)
    when Operations::AlterEnumValues
      alter_enum_values(change)
    when Operations::CreateExtension
      create_extension(change)
    when Operations::DropExtension
      drop_extension(change)
    when Operations::ExecuteQuery
      execute_query(change)
    end
  end
end