Module: Torque::PostgreSQL::VersionedCommands::Migration

Defined in:
lib/torque/postgresql/versioned_commands/command_migration.rb

Instance Method Summary collapse

Instance Method Details

#announce(message) ⇒ Object

Better formatting of the output



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/torque/postgresql/versioned_commands/command_migration.rb', line 28

def announce(message)
  action, result = @description

  title = [
    @command.type.capitalize,
    @command.object_name,
    "v#{@command.op_version}"
  ].join(' ')

  timing = message.split(' ', 2).second
  action = "#{result} #{timing}" if timing.present?
  text = "#{@command.version} #{title}: #{action}"
  length = [0, 75 - text.length].max

  write "== %s %s" % [text, "=" * length]
end

#description_for(direction) ⇒ Object

Produces a nice description of what is being done



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/torque/postgresql/versioned_commands/command_migration.rb', line 46

def description_for(direction)
  base = @command.op.chomp('e') if direction == :up
  base ||=
    case @command.op
    when 'create' then 'dropp'
    when 'update' then 'revert'
    when 'remove' then 're-creat'
    end

  ["#{base}ing", "#{base}ed"]
end

#exec_migration(conn, direction) ⇒ Object

Uses the command to execute the proper action



19
20
21
22
23
24
25
# File 'lib/torque/postgresql/versioned_commands/command_migration.rb', line 19

def exec_migration(conn, direction)
  @connection = conn
  direction == :up ? @command.up : @command.down
ensure
  @connection = nil
  @execution_strategy = nil
end

#execute(command) ⇒ Object

Print the command and then execute it



59
60
61
62
# File 'lib/torque/postgresql/versioned_commands/command_migration.rb', line 59

def execute(command)
  write "-- #{command.gsub(/(?<!\A)^/, '   ').gsub(/[\s\n]*\z/, '')}"
  execution_strategy.execute(command)
end

#initialize(*args) ⇒ Object



7
8
9
10
# File 'lib/torque/postgresql/versioned_commands/command_migration.rb', line 7

def initialize(*args)
  @command = args.pop
  super(*args)
end

#migrate(direction) ⇒ Object

Prepare the description based on the direction



13
14
15
16
# File 'lib/torque/postgresql/versioned_commands/command_migration.rb', line 13

def migrate(direction)
  @description = description_for(direction)
  super
end