Class: DynamoDbFramework::MigrationManager

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ MigrationManager

Returns a new instance of MigrationManager.



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

def initialize(store)
  @dynamodb_repository = DynamoDbFramework::Repository.new(store)
  @dynamodb_table_manager = DynamoDbFramework::TableManager.new(store)
end

Instance Attribute Details

#dynamodb_repositoryObject (readonly)

Returns the value of attribute dynamodb_repository.



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

def dynamodb_repository
  @dynamodb_repository
end

#dynamodb_table_managerObject (readonly)

Returns the value of attribute dynamodb_table_manager.



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

def dynamodb_table_manager
  @dynamodb_table_manager
end

Instance Method Details

#applyObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dynamodb_framework/dynamodb_migration_manager.rb', line 42

def apply

  DynamoDbFramework.logger.info '[DynamoDbFramework] - Applying migration scripts'

  executed_scripts = get_executed_scripts()

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

  scripts.sort { |a,b| a.timestamp <=> b.timestamp }.each do |script|
    if executed_scripts == nil || !executed_scripts.include?(script.timestamp)
      DynamoDbFramework.logger.info '[DynamoDbFramework] - Applying script: ' + script.timestamp + '.....'
      script.apply
      @dynamodb_repository.put({ :timestamp => script.timestamp })
    end
  end

  DynamoDbFramework.logger.info '[DynamoDbFramework] - Migration scripts applied.'

end

#connectObject



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

def connect

  DynamoDbFramework.logger.info '[DynamoDbFramework] - Connecting to DynamoDb instance'

  migration_table_name = 'dynamodb_framework_migrations'

  #check if migration table exists
  if !@dynamodb_table_manager.exists?(migration_table_name)
    #migration table not found so create it
    builder = DynamoDbFramework::AttributesBuilder.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

  DynamoDbFramework.logger.info '[DynamoDbFramework] - Connected.'

end

#get_executed_scriptsObject



33
34
35
36
37
38
39
40
# File 'lib/dynamodb_framework/dynamodb_migration_manager.rb', line 33

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

  return nil
end

#rollbackObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dynamodb_framework/dynamodb_migration_manager.rb', line 66

def rollback

  DynamoDbFramework.logger.info '[DynamoDbFramework] - Rolling back started.'

  executed_scripts = get_executed_scripts()

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

  scripts.sort { |a,b| a.timestamp <=> b.timestamp }.each do |script|
    if executed_scripts != nil && executed_scripts.length > 0 && executed_scripts.include?(script.timestamp)
      script.undo
      @dynamodb_repository.delete({ :timestamp => script.timestamp })
      return
    end
  end

  DynamoDbFramework.logger.info '[DynamoDbFramework] - Rollback complete.'

end