Class: Torque::PostgreSQL::VersionedCommands::CommandMigration

Inherits:
Struct
  • Object
show all
Defined in:
lib/torque/postgresql/versioned_commands/command_migration.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename, *args) ⇒ CommandMigration

Returns a new instance of CommandMigration.



68
69
70
71
# File 'lib/torque/postgresql/versioned_commands/command_migration.rb', line 68

def initialize(filename, *args)
  super(File.expand_path(filename), *args)
  @migration = nil
end

Instance Method Details

#disable_ddl_transactionObject

There is no way to setup this, so it is always false



79
80
81
# File 'lib/torque/postgresql/versioned_commands/command_migration.rb', line 79

def disable_ddl_transaction
  false
end

#downObject

Find the previous command and executes it



101
102
103
104
105
106
# File 'lib/torque/postgresql/versioned_commands/command_migration.rb', line 101

def down
  return drop if op_version == 1
  dirs = @migration.pool.migrations_paths
  version = op_version - (op == 'remove' ? 0 : 1)
  execute VersionedCommands.fetch_command(dirs, type, object_name, version)
end

#dropObject

Drops the type created

Raises:

  • (ArgumentError)


109
110
111
112
113
# File 'lib/torque/postgresql/versioned_commands/command_migration.rb', line 109

def drop
  method_name = :"drop_#{type}"
  return send(method_name) if VersionedCommands.valid_type?(type)
  raise ArgumentError, "Unknown versioned command type: #{type}"
end

#migrate(direction) ⇒ Object

Down is more complicated, then this just starts separating the logic



84
85
86
87
88
89
90
91
# File 'lib/torque/postgresql/versioned_commands/command_migration.rb', line 84

def migrate(direction)
  @migration = ActiveRecord::Migration.allocate
  @migration.extend(Migration)
  @migration.send(:initialize, name, version, self)
  @migration.migrate(direction)
ensure
  @migration = nil
end

#nameObject

Rails uses this to avoid duplicate migrations



74
75
76
# File 'lib/torque/postgresql/versioned_commands/command_migration.rb', line 74

def name
  "#{op}_#{type}_#{object_name}_v#{op_version}"
end

#upObject

Simply executes the underlying command



94
95
96
97
98
# File 'lib/torque/postgresql/versioned_commands/command_migration.rb', line 94

def up
  content = File.read(filename)
  VersionedCommands.validate!(type, content, object_name)
  execute content
end