As the title is.
Clone an existing table and use it in a test. The one who uses it in such a time.
Script
#!/bin/bash
# Create a table definition file for create-table from the table definition of the original table
get_table_data() {
echo "get_table_data start"
# Get the table definition of the original table from AWS and output it to a file
aws dynamodb describe-table --table-name ${original_table_name} > ${original_table_file}
# Format the table definition output from AWS into a form that can be used for create-table and output to a file
cat ${original_table_file} |
jq '. Table' |
jq '. TableName = "'${new_table_name}'"' |
jq 'del(. TableStatus)' |
jq 'del(. CreationDateTime)' |
jq 'del(. ProvisionedThroughput.LastIncreaseDateTime)' |
jq 'del(. ProvisionedThroughput.NumberOfDecreasesToday)' |
jq 'del(. TableSizeBytes)' |
jq 'del(. ItemCount)' |
jq 'del(. TableArn)' |
jq 'del(. TableId)' |
jq 'del(. LatestStreamLabel)' |
jq 'del(. LatestStreamArn)' |
jq 'del(. GlobalSecondar[]yIndexes?. IndexStatus)' |
jq 'del(. GlobalSecondaryIndexes?[]. IndexSizeBytes)' |
jq 'del(. GlobalSecondaryIndexes?. ItemCoun[]t)' |
jq 'del(. GlobalSecondaryIndexes?. IndexArn)' |
jq[] 'del(. GlobalSecondaryIndexes?. ProvisionedThroughput.Num[]berOfDecreasesToday)' > ${new_table_file}
}
# Create a new table for testing based on the table definition file of the original table
create_table() {
echo "create_table start"
aws dynamodb create-table --cli-input-json file://${new_table_file}
}
# Output item information registered in the original table to a file
get_items_data() {
echo "get_items_data start"
aws dynamodb scan --table-name ${table_name} > ${original_items_file}
}
# Register items in a test table based on the item information file in the original table
put_items() {
echo "put_items start"
# Number of items to register
item_length=$(cat ${original_items_file} | jq ". Items | length")
# Register > in the test table only if the number of items is 0
# (Because there is no item, but if you[ ${item_length} -gt 0 ] try to execute the put-item command, it will be an error)
if ; then
for i in $( seq 0 $((${item_length} - 1)) ); do
item=$[${i}](cat ${original_items_file} | jq ". Items")
aws dynamodb put-item --table-name ${new_table_name} --item "${item}"
done
fi
}
# Specify the table name of the table group to replicate for testing
target_tables=("TABLE-A" "TABLE-B" "TABLE[@]-C")
for table_name in ${target_tables}; do
# Table name
original_table_name=${table_name}
new_table_name=clone-${original_table_name}
# Table definition file name
original_table_file=original_table_${original_table_name}.json
new_table_file=new_table_${new_table_name}.json
# Item information file name
original_items_file=original_items_${original_table_name}.json
get_table_data
create_table
get_items_data
# Trying to put item before creating the table is complete causes a ResourceNotFoundException, so there is a delay
echo "sleep 20 start"
sleep 20
echo "sleep 20 end"
put_items
done
I'm commenting, so I think you'll know what you're probably doing. If you feel like it, I'll post it in detail.
Repository
GitHub - m1z0-753/dynamoDB-clone
Contribute to m1z0-753/dynamoDB-clone development by creating an account on GitHub.