Class: DynamodbModel::Migration::Generator

Inherits:
Object
  • Object
show all
Includes:
DbConfig
Defined in:
lib/dynamodb_model/migration/generator.rb

Overview

jets dynamodb generate posts –partition-key id:string

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DbConfig

#db, included, #namespaced_table_name

Constructor Details

#initialize(migration_name, options) ⇒ Generator

Returns a new instance of Generator.



9
10
11
12
# File 'lib/dynamodb_model/migration/generator.rb', line 9

def initialize(migration_name, options)
  @migration_name = migration_name
  @options = options
end

Instance Attribute Details

#migration_nameObject (readonly)

Returns the value of attribute migration_name.



8
9
10
# File 'lib/dynamodb_model/migration/generator.rb', line 8

def migration_name
  @migration_name
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



8
9
10
# File 'lib/dynamodb_model/migration/generator.rb', line 8

def table_name
  @table_name
end

Instance Method Details

#conventional_table_actionObject



42
43
44
# File 'lib/dynamodb_model/migration/generator.rb', line 42

def conventional_table_action
  @migration_name.include?("update") ? "update_table" : "create_table"
end

#conventional_table_nameObject

create_posts => posts update_posts => posts



52
53
54
# File 'lib/dynamodb_model/migration/generator.rb', line 52

def conventional_table_name
  @migration_name.sub(/^(create|update)_/, '')
end

#create_migrationObject



20
21
22
23
24
25
# File 'lib/dynamodb_model/migration/generator.rb', line 20

def create_migration
  FileUtils.mkdir_p(File.dirname(migration_path))
  IO.write(migration_path, migration_code)
  puts "Migration file created: #{migration_path}. \nTo run:"
  puts "  jets dynamodb migrate #{migration_path}"
end

#generateObject



14
15
16
17
18
# File 'lib/dynamodb_model/migration/generator.rb', line 14

def generate
  puts "Generating migration for #{@table_name}" unless @options[:quiet]
  return if @options[:noop]
  create_migration
end

#migration_class_nameObject



56
57
58
# File 'lib/dynamodb_model/migration/generator.rb', line 56

def migration_class_name
  "#{@migration_name}_migration".classify # doesnt include timestamp
end

#migration_codeObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/dynamodb_model/migration/generator.rb', line 27

def migration_code
  path = File.expand_path("../templates/#{table_action}.rb", __FILE__)
  result = DynamodbModel::Erb.result(path,
    migration_class_name: migration_class_name,
    table_name: table_name,
    partition_key: @options[:partition_key],
    sort_key: @options[:sort_key],
    provisioned_throughput: @options[:provisioned_throughput] || 5,
  )
end

#migration_pathObject



60
61
62
# File 'lib/dynamodb_model/migration/generator.rb', line 60

def migration_path
  "#{DynamodbModel.app_root}dynamodb/migrate/#{timestamp}-#{@migration_name}_migration.rb"
end

#table_actionObject



38
39
40
# File 'lib/dynamodb_model/migration/generator.rb', line 38

def table_action
  @options[:table_action] || conventional_table_action
end

#timestampObject



64
65
66
# File 'lib/dynamodb_model/migration/generator.rb', line 64

def timestamp
  @timestamp ||= Time.now.strftime("%Y%m%d%H%M%S")
end