Class: Dynomite::Migration::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/dynomite/migration/generator.rb

Overview

jets dynamodb:generate posts –partition-key id:string

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(migration_name, options) ⇒ Generator

Returns a new instance of Generator.



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

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.



6
7
8
# File 'lib/dynomite/migration/generator.rb', line 6

def migration_name
  @migration_name
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



6
7
8
# File 'lib/dynomite/migration/generator.rb', line 6

def table_name
  @table_name
end

Instance Method Details

#actionObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dynomite/migration/generator.rb', line 43

def action
  # optoins[:table_action] is old and deprecated
  action = @options[:action] || @options[:table_action] || conventional_action
  case action
  when /create/
    "create_table"
  when /delete/
    "delete_table"
  else
    "update_table" # default and fallback
  end
end

#conventional_actionObject



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

def conventional_action
  @migration_name.split("_").first
end

#conventional_table_nameObject

create_posts => posts update_posts => posts



66
67
68
# File 'lib/dynomite/migration/generator.rb', line 66

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

#create_migrationObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dynomite/migration/generator.rb', line 18

def create_migration
  FileUtils.mkdir_p(File.dirname(migration_path))
  IO.write(migration_path, migration_code)
  pretty_migration_path = migration_path.sub(/^\.\//,'') # remove leading ./
  puts "Migration file created: #{pretty_migration_path}\nTo run:\n\n"
  command = File.basename($0)
  if command == "jets"
    puts "    #{command} dynamodb:migrate"
  else
    puts "    #{command} migrate"
  end
  puts
end

#generateObject



12
13
14
15
16
# File 'lib/dynomite/migration/generator.rb', line 12

def generate
  puts "Generating migration" unless @options[:quiet]
  return if ENV['NOOP']
  create_migration
end

#migration_class_nameObject



70
71
72
# File 'lib/dynomite/migration/generator.rb', line 70

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

#migration_codeObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/dynomite/migration/generator.rb', line 32

def migration_code
  path = File.expand_path("../templates/#{action}.rb", __FILE__)
  Dynomite::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



74
75
76
# File 'lib/dynomite/migration/generator.rb', line 74

def migration_path
  "#{Dynomite.root}/dynamodb/migrate/#{timestamp}-#{@migration_name}.rb"
end

#timestampObject



78
79
80
# File 'lib/dynomite/migration/generator.rb', line 78

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