Class: DynamoDbFramework::Namespace::MigrationManager
- Inherits:
-
Object
- Object
- DynamoDbFramework::Namespace::MigrationManager
- Defined in:
- lib/dynamodb_framework/dynamodb_namespace_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(namespace) ⇒ Object
- #connect ⇒ Object
- #get_executed_scripts(namespace) ⇒ Object
-
#initialize(store) ⇒ MigrationManager
constructor
A new instance of MigrationManager.
- #rollback(namespace) ⇒ Object
Constructor Details
#initialize(store) ⇒ MigrationManager
Returns a new instance of MigrationManager.
8 9 10 11 |
# File 'lib/dynamodb_framework/dynamodb_namespace_migration_manager.rb', line 8 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.
6 7 8 |
# File 'lib/dynamodb_framework/dynamodb_namespace_migration_manager.rb', line 6 def dynamodb_repository @dynamodb_repository end |
#dynamodb_table_manager ⇒ Object (readonly)
Returns the value of attribute dynamodb_table_manager.
5 6 7 |
# File 'lib/dynamodb_framework/dynamodb_namespace_migration_manager.rb', line 5 def dynamodb_table_manager @dynamodb_table_manager end |
Instance Method Details
#apply(namespace) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/dynamodb_framework/dynamodb_namespace_migration_manager.rb', line 44 def apply(namespace) DynamoDbFramework.logger.info "[#{self.class}] - Applying migration scripts" executed_scripts = get_executed_scripts(namespace) 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 "[#{self.class}] - Applying script: #{script.}....." script.apply @dynamodb_repository.put({ :timestamp => script., :namespace => script.namespace }) end end DynamoDbFramework.logger.info "[#{self.class}] - Migration scripts applied." end |
#connect ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/dynamodb_framework/dynamodb_namespace_migration_manager.rb', line 13 def connect DynamoDbFramework.logger.info "[#{self.class}] - Connecting to DynamoDb instance" migration_table_name = 'dynamodb_framework_migration_history' #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(:namespace, :S, :partition) builder.add(:timestamp, :S, :range) @dynamodb_table_manager.create_table(name: migration_table_name, attributes: builder.attributes) end #set the table name for the repository @dynamodb_repository.table_name = migration_table_name DynamoDbFramework.logger.info "[#{self.class}] - Connected." end |
#get_executed_scripts(namespace) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/dynamodb_framework/dynamodb_namespace_migration_manager.rb', line 35 def get_executed_scripts(namespace) scripts = @dynamodb_repository.query(:namespace, namespace) if scripts.length > 0 return scripts.sort { |a,b| b['timestamp'] <=> a['timestamp'] }.map { |i| i['timestamp'] } end return nil end |
#rollback(namespace) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/dynamodb_framework/dynamodb_namespace_migration_manager.rb', line 68 def rollback(namespace) DynamoDbFramework.logger.info "[#{self.class}] - Rolling back started." executed_scripts = get_executed_scripts(namespace) 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., :namespace => script.namespace }) return end end DynamoDbFramework.logger.info "[#{self.class}] - Rollback complete." end |