Class: DynamoDbMigrationManager

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamodb_framework/dynamodb_migration_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDynamoDbMigrationManager

Returns a new instance of DynamoDbMigrationManager.



6
7
8
9
# File 'lib/dynamodb_framework/dynamodb_migration_manager.rb', line 6

def initialize
  @dynamodb_repository = DynamoDbRepository.new
  @dynamodb_table_manager = DynamoDbTableManager.new
end

Instance Attribute Details

#dynamodb_repositoryObject (readonly)

Returns the value of attribute dynamodb_repository.



4
5
6
# File 'lib/dynamodb_framework/dynamodb_migration_manager.rb', line 4

def dynamodb_repository
  @dynamodb_repository
end

#dynamodb_table_managerObject (readonly)

Returns the value of attribute dynamodb_table_manager.



3
4
5
# File 'lib/dynamodb_framework/dynamodb_migration_manager.rb', line 3

def dynamodb_table_manager
  @dynamodb_table_manager
end

Instance Method Details

#applyObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dynamodb_framework/dynamodb_migration_manager.rb', line 37

def apply

  last_executed_script = get_last_executed_script()

  scripts = []
  MigrationScript.descendants.each do |ms|
    script = ms.new
    scripts.push(script)
  end

  scripts.sort { |a,b| a.timestamp <=> b.timestamp }.each do |script|
    if last_executed_script == nil || script.timestamp > last_executed_script
      script.apply
      @dynamodb_repository.put({ :timestamp => script.timestamp })
    end
  end

end

#connectObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dynamodb_framework/dynamodb_migration_manager.rb', line 11

def connect

  migration_table_name = 'dynamodb_migrations'

  #check if migration table exists
  if !@dynamodb_table_manager.exists?(migration_table_name)
    #migration table not found so create it
    builder = DynamoDbAttributesBuilder.new
    builder.add(:timestamp, :S)
    @dynamodb_table_manager.create(migration_table_name, builder.attributes, :timestamp)
  end

  #set the table name for the repository
  @dynamodb_repository.table_name = migration_table_name

end

#get_last_executed_scriptObject



28
29
30
31
32
33
34
35
# File 'lib/dynamodb_framework/dynamodb_migration_manager.rb', line 28

def get_last_executed_script
  scripts = @dynamodb_repository.all()
  if scripts.length > 0
    return scripts.sort { |a,b| b['timestamp'] <=> a['timestamp'] }[0]['timestamp']
  end

  return nil
end

#rollbackObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dynamodb_framework/dynamodb_migration_manager.rb', line 56

def rollback

  last_executed_script = get_last_executed_script()

  scripts = []
  MigrationScript.descendants.each do |ms|
    script = ms.new
    scripts.push(script)
  end

  scripts.sort { |a,b| a.timestamp <=> b.timestamp }.each do |script|
    if script.timestamp == last_executed_script
      script.undo
      @dynamodb_repository.delete({ :timestamp => script.timestamp })
      return
    end
  end
end