Class: Dynomite::Migration::Generator
- Inherits:
-
Object
- Object
- Dynomite::Migration::Generator
- Defined in:
- lib/dynomite/migration/generator.rb
Overview
jets dynamodb:generate posts –partition-key id:string
Instance Attribute Summary collapse
-
#migration_name ⇒ Object
readonly
Returns the value of attribute migration_name.
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
Instance Method Summary collapse
- #action ⇒ Object
- #conventional_action ⇒ Object
-
#conventional_table_name ⇒ Object
create_posts => posts update_posts => posts.
- #create_migration ⇒ Object
- #generate ⇒ Object
-
#initialize(migration_name, options) ⇒ Generator
constructor
A new instance of Generator.
- #migration_class_name ⇒ Object
- #migration_code ⇒ Object
- #migration_path ⇒ Object
- #timestamp ⇒ Object
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, ) @migration_name = migration_name @options = end |
Instance Attribute Details
#migration_name ⇒ Object (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_name ⇒ Object (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
#action ⇒ Object
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_action ⇒ Object
56 57 58 |
# File 'lib/dynomite/migration/generator.rb', line 56 def conventional_action @migration_name.split("_").first end |
#conventional_table_name ⇒ Object
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_migration ⇒ Object
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 |
#generate ⇒ Object
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_name ⇒ Object
70 71 72 |
# File 'lib/dynomite/migration/generator.rb', line 70 def migration_class_name "#{@migration_name}".camelize # doesnt include timestamp end |
#migration_code ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/dynomite/migration/generator.rb', line 32 def migration_code path = File.("../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_path ⇒ Object
74 75 76 |
# File 'lib/dynomite/migration/generator.rb', line 74 def migration_path "#{Dynomite.root}/dynamodb/migrate/#{}-#{@migration_name}.rb" end |
#timestamp ⇒ Object
78 79 80 |
# File 'lib/dynomite/migration/generator.rb', line 78 def @timestamp ||= Time.now.strftime("%Y%m%d%H%M%S") end |