Class: Belt::CLI::IndexCommand
- Inherits:
-
Object
- Object
- Belt::CLI::IndexCommand
- Defined in:
- lib/belt/cli/index_command.rb
Constant Summary collapse
- MODULE_DIR =
'infrastructure/modules/app'- DYNAMODB_TF =
File.join(MODULE_DIR, 'dynamodb.tf')
Class Method Summary collapse
- .add(args) ⇒ Object
- .parse_add_args(args) ⇒ Object
- .remove(args) ⇒ Object
- .run(args) ⇒ Object
- .usage ⇒ Object
Instance Method Summary collapse
- #add ⇒ Object
-
#initialize(table, index_name, partition_key: nil, sort_key: nil) ⇒ IndexCommand
constructor
A new instance of IndexCommand.
- #remove ⇒ Object
Constructor Details
#initialize(table, index_name, partition_key: nil, sort_key: nil) ⇒ IndexCommand
Returns a new instance of IndexCommand.
89 90 91 92 93 94 |
# File 'lib/belt/cli/index_command.rb', line 89 def initialize(table, index_name, partition_key: nil, sort_key: nil) @table = table @index_name = index_name @partition_key = partition_key @sort_key = sort_key end |
Class Method Details
.add(args) ⇒ Object
45 46 47 48 |
# File 'lib/belt/cli/index_command.rb', line 45 def self.add(args) table, index_name, partition_key, sort_key = parse_add_args(args) new(table, index_name, partition_key: partition_key, sort_key: sort_key).add end |
.parse_add_args(args) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/belt/cli/index_command.rb', line 62 def self.parse_add_args(args) table = args.shift index_name = args.shift partition_key = nil sort_key = nil i = 0 while i < args.length case args[i] when '--partition-key', '-p' i += 1 partition_key = args[i] when '--sort-key', '-s' i += 1 sort_key = args[i] end i += 1 end if table.nil? || index_name.nil? || partition_key.nil? abort "Usage: belt generate index <table> <IndexName> --partition-key <key> [--sort-key <key>]\n\n" \ 'Example: belt generate index messages ConversationIndex --partition-key conversation_id' end [table, index_name, partition_key, sort_key] end |
.remove(args) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/belt/cli/index_command.rb', line 50 def self.remove(args) table = args.shift index_name = args.shift if table.nil? || index_name.nil? abort "Usage: belt destroy index <table> <IndexName>\n\n" \ 'Example: belt destroy index messages ConversationIndex' end new(table, index_name).remove end |
.run(args) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/belt/cli/index_command.rb', line 12 def self.run(args) action = args.shift case action when 'add', nil add(args) when 'remove', 'rm' remove(args) when '--help', '-h' puts usage else # Treat first arg as table name if no subcommand add([action] + args) end end |
.usage ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/belt/cli/index_command.rb', line 28 def self.usage <<~USAGE Usage: belt generate index <table> <IndexName> --partition-key <key> [--sort-key <key>] belt destroy index <table> <IndexName> Add or remove a Global Secondary Index (GSI) from dynamodb.tf. Examples: belt generate index messages ConversationIndex --partition-key conversation_id belt generate index messages RecentByUserIndex --partition-key user_id --sort-key created_at belt destroy index messages ConversationIndex Note: After modifying indexes, run `belt deploy` to apply changes to AWS. Adding a GSI to an existing table takes ~5-10 minutes (AWS limitation). USAGE end |
Instance Method Details
#add ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/belt/cli/index_command.rb', line 96 def add validate_tf_exists! content = File.read(DYNAMODB_TF) table_resource = "aws_dynamodb_table\" \"#{@table}\"" unless content.include?(table_resource) abort "Error: Table '#{@table}' not found in #{DYNAMODB_TF}.\n" \ 'Run `belt setup tables` first to generate the table.' end if content.include?("name = \"#{@index_name}\"") puts " skip #{@index_name} (already exists on #{@table})" return end dynamo_pk = Belt::Inflector.camelize_lower(@partition_key) dynamo_sk = @sort_key ? Belt::Inflector.camelize_lower(@sort_key) : nil # Build the GSI block gsi_block = build_gsi_block(dynamo_pk, dynamo_sk) # Build attribute blocks for new keys attr_blocks = build_attribute_blocks(content, dynamo_pk, dynamo_sk) # Insert into the table resource insert_gsi(content, gsi_block, attr_blocks) puts " create #{@index_name} on #{@table} (partition: #{dynamo_pk}#{", sort: #{dynamo_sk}" if dynamo_sk})" puts "\n Run `belt deploy` to apply. Adding a GSI to an existing table takes ~5-10 min." end |
#remove ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/belt/cli/index_command.rb', line 128 def remove validate_tf_exists! content = File.read(DYNAMODB_TF) unless content.include?("name = \"#{@index_name}\"") abort "Error: Index '#{@index_name}' not found in #{DYNAMODB_TF}." end # Remove the GSI block content.sub!(/\n\s*global_secondary_index \{\n\s*name\s*=\s*"#{Regexp.escape(@index_name)}".*?\n\s*\}/m, '') File.write(DYNAMODB_TF, content) puts " remove #{@index_name} from #{@table}" puts "\n Run `belt deploy` to apply." end |