Class: DynamoDbFramework::MigrationManager
- Inherits:
-
Object
- Object
- DynamoDbFramework::MigrationManager
- Defined in:
- lib/dynamodb_framework/dynamodb_migration_manager.rb
Instance Attribute Summary collapse
-
#dynamodb_repository ⇒ Object
readonly
Returns the value of attribute dynamodb_repository.
-
#dynamodb_table_manager ⇒ Object
readonly
Returns the value of attribute dynamodb_table_manager.
Instance Method Summary collapse
- #apply ⇒ Object
- #connect ⇒ Object
- #get_executed_scripts ⇒ Object
-
#initialize(store) ⇒ MigrationManager
constructor
A new instance of MigrationManager.
- #rollback ⇒ Object
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_repository ⇒ Object (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_manager ⇒ Object (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
#apply ⇒ Object
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. <=> b. }.each do |script| if executed_scripts == nil || !executed_scripts.include?(script.) DynamoDbFramework::LOGGER.info '[DynamoDbFramework] - Applying script: ' + script. + '.....' script.apply @dynamodb_repository.put({ :timestamp => script. }) end end DynamoDbFramework::LOGGER.info '[DynamoDbFramework] - Migration scripts applied.' end |
#connect ⇒ Object
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_scripts ⇒ Object
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 |
#rollback ⇒ Object
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. <=> b. }.each do |script| if executed_scripts != nil && executed_scripts.length > 0 && executed_scripts.include?(script.) script.undo @dynamodb_repository.delete({ :timestamp => script. }) return end end DynamoDbFramework::LOGGER.info '[DynamoDbFramework] - Rollback complete.' end |