vitess.io/vitess@v0.16.2/docker/vttestserver/setup_vschema_folder.sh (about)

     1  #!/bin/bash
     2  
     3  # Copyright 2021 The Vitess Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  # getLength gets the number of elements in a comma separated string
    18  function getLength() {
    19    COUNT=0
    20    for _ in ${1//,/ }
    21    do
    22      ((COUNT++))
    23    done
    24    echo "$COUNT"
    25  }
    26  
    27  # The first argument to the file is a comma separated list of keyspaces and the second argument is the comma separated list of number of shards.
    28  
    29  KEYSPACES="$1"
    30  NUM_SHARDS="$2"
    31  
    32  COUNT_KEYSPACES=$(getLength "$KEYSPACES")
    33  COUNT_NUM_SHARDS=$(getLength "$NUM_SHARDS")
    34  
    35  # Incase the number of keyspaces and num_shards do not match, throw an error
    36  if [ "$COUNT_KEYSPACES" != "$COUNT_NUM_SHARDS" ]; then
    37      echo "Incompatible list of keyspaces and number of shards"
    38      exit 1
    39  fi
    40  
    41  # Convert the strings to lists
    42  read -ra KEYSPACES_LIST <<<"${KEYSPACES//,/ }"
    43  read -ra NUM_SHARDS_LIST <<<"${NUM_SHARDS//,/ }"
    44  
    45  # create the main schema directory
    46  mkdir /vt/schema/
    47  
    48  i=0;
    49  for keyspace in "${KEYSPACES_LIST[@]}";
    50  do
    51    # create a directory for each keyspace
    52    mkdir "/vt/schema/$keyspace"
    53    num_shard=${NUM_SHARDS_LIST[$i]}
    54    # Create a vschema.json file only if the number of shards are more than 1
    55    if [[ $num_shard -gt "1" ]]; then
    56      printf "{\n\t\"sharded\": true\n}" > "/vt/schema/$keyspace/vschema.json"
    57    fi
    58    ((i++))
    59  done