github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/tests/br_clustered_index/run.sh (about)

     1  #!/bin/sh
     2  #
     3  # Copyright 2020 PingCAP, Inc.
     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  # See the License for the specific language governing permissions and
    14  # limitations under the License.
    15  
    16  set -eu
    17  DB="$TEST_NAME"
    18  TABLE="usertable"
    19  
    20  run_sql "CREATE DATABASE $DB;"
    21  
    22  table_names=${cases:-'t0 t1 t2 t_bit t_bool t_tinyint t_smallint t_mediumint t_int t_date t_time t_datetime t_timestamp t_year t_char t_varcher t_text t_binary t_blob t_enum t_set t8 t9 t10 t11 t12'}
    23  
    24  run_sql "
    25  USE $DB;
    26  
    27  CREATE TABLE t0 (
    28      id VARCHAR(255),
    29      data INT,
    30      PRIMARY KEY(id) CLUSTERED
    31  );
    32  INSERT INTO t0 VALUES ('1', 1);
    33  INSERT INTO t0 VALUES ('2', 2);
    34  INSERT INTO t0 VALUES ('3', 3);
    35  INSERT INTO t0 VALUES ('4', 4);
    36  INSERT INTO t0 VALUES ('5', 5);
    37  
    38  CREATE TABLE t1 (
    39      id VARCHAR(255),
    40      a INT,
    41      b CHAR(10),
    42      PRIMARY KEY(id, b) CLUSTERED,
    43      UNIQUE KEY(b),
    44      KEY(a)
    45  );
    46  INSERT INTO t1 VALUES ('111', 111, '111');
    47  INSERT INTO t1 VALUES ('222', 222, '222');
    48  INSERT INTO t1 VALUES ('333', 333, '333');
    49  INSERT INTO t1 VALUES ('444', 444, '444');
    50  INSERT INTO t1 VALUES ('555', 555, '555');
    51  
    52  CREATE TABLE t2 (
    53      id VARCHAR(255),
    54      a INT,
    55      b DECIMAL(5,2),
    56      PRIMARY KEY(id, a) CLUSTERED,
    57      KEY(id, a),
    58      UNIQUE KEY(id, a)
    59  );
    60  INSERT INTO t2 VALUES ('aaaa', 1111, 11.0);
    61  INSERT INTO t2 VALUES ('bbbb', 1111, 12.0);
    62  INSERT INTO t2 VALUES ('cccc', 1111, 13.0);
    63  INSERT INTO t2 VALUES ('dddd', 1111, 14.0);
    64  INSERT INTO t2 VALUES ('eeee', 1111, 15.0);
    65  
    66  create table t_bit(a bit primary key CLUSTERED, b int);
    67  INSERT INTO t_bit VALUES(1,2);
    68  INSERT INTO t_bit VALUES(0,3);
    69  
    70  create table t_bool(a bool primary key CLUSTERED, b int);
    71  INSERT INTO t_bool VALUES(true,2);
    72  INSERT INTO t_bool VALUES(false,3);
    73  
    74  create table t_tinyint(a tinyint primary key CLUSTERED, b int);
    75  INSERT INTO t_tinyint VALUES(6,2);
    76  INSERT INTO t_tinyint VALUES(8,3);
    77  
    78  create table t_smallint(a smallint primary key CLUSTERED, b int);
    79  INSERT INTO t_smallint VALUES(432,2);
    80  INSERT INTO t_smallint VALUES(125,3);
    81  
    82  create table t_mediumint(a mediumint primary key CLUSTERED, b int);
    83  INSERT INTO t_mediumint VALUES(8567,2);
    84  INSERT INTO t_mediumint VALUES(12341,3);
    85  
    86  create table t_int(a int primary key CLUSTERED, b int);
    87  INSERT INTO t_int VALUES(123563,2);
    88  INSERT INTO t_int VALUES(6784356,3);
    89  
    90  create table t_date(a date primary key CLUSTERED, b int);
    91  INSERT INTO t_date VALUES ('2020-02-20', 1);
    92  INSERT INTO t_date VALUES ('2020-02-21', 2);
    93  INSERT INTO t_date VALUES ('2020-02-22', 3);
    94  
    95  create table t_time(a time primary key CLUSTERED, b int);
    96  
    97  INSERT INTO t_time VALUES ('11:22:33', 1);
    98  INSERT INTO t_time VALUES ('11:33:22', 2);
    99  INSERT INTO t_time VALUES ('11:43:11', 3);
   100  
   101  create table t_datetime(a datetime primary key CLUSTERED, b int);
   102  INSERT INTO t_datetime VALUES ('2020-02-20 11:22:33', 1);
   103  INSERT INTO t_datetime VALUES ('2020-02-21 11:33:22', 2);
   104  INSERT INTO t_datetime VALUES ('2020-02-22 11:43:11', 3);
   105  
   106  create table t_timestamp(a timestamp primary key CLUSTERED, b int);
   107  INSERT INTO t_timestamp VALUES ('2020-02-20 11:22:33', 1);
   108  INSERT INTO t_timestamp VALUES ('2020-02-21 11:33:22', 2);
   109  INSERT INTO t_timestamp VALUES ('2020-02-22 11:43:11', 3);
   110  
   111  create table t_year(a year primary key CLUSTERED, b int);
   112  INSERT INTO t_year VALUES ('2020', 1);
   113  INSERT INTO t_year VALUES ('2021', 2);
   114  INSERT INTO t_year VALUES ('2022', 3);
   115  
   116  create table t_char(a char(20) primary key CLUSTERED, b int);
   117  INSERT INTO t_char VALUES ('abcc', 1);
   118  INSERT INTO t_char VALUES ('sdff', 2);
   119  
   120  create table t_varcher(a varchar(255) primary key CLUSTERED, b int);
   121  INSERT INTO t_varcher VALUES ('abcc', 1);
   122  INSERT INTO t_varcher VALUES ('sdff', 2);
   123  
   124  create table t_text (a text, b int, primary key(a(5)) CLUSTERED);
   125  INSERT INTO t_text VALUES ('abcc', 1);
   126  INSERT INTO t_text VALUES ('sdff', 2);
   127  
   128  create table t_binary(a binary(20) primary key CLUSTERED, b int);
   129  INSERT INTO t_binary VALUES (x'89504E470D0A1A0A',1),(x'89504E470D0A1A0B',2),(x'89504E470D0A1A0C',3);
   130  
   131  create table t_blob(a blob, b int, primary key (a(20)) CLUSTERED);
   132  INSERT INTO t_blob VALUES (x'89504E470D0A1A0A',1),(x'89504E470D0A1A0B',2),(x'89504E470D0A1A0C',3);
   133  
   134  create table t_enum(e enum('a', 'b', 'c') primary key CLUSTERED, b int);
   135  INSERT INTO t_enum VALUES ('a',1),('b',2),('c',3);
   136  
   137  create table t_set(s set('a', 'b', 'c') primary key CLUSTERED, b int);
   138  INSERT INTO t_set VALUES ('a',1),('b,c',2),('a,c',3);
   139  
   140  
   141  create table t8(a int, b varchar(255) as (concat(a, 'test')) stored, primary key(b) CLUSTERED);
   142  INSERT INTO t8(a) VALUES (2020);
   143  INSERT INTO t8(a) VALUES (2021);
   144  INSERT INTO t8(a) VALUES (2022);
   145  
   146  create table t9(a int, b varchar(255), c int, primary key(a ,b) CLUSTERED);
   147  insert into t9 values(1, 'aaa', 1),(2, 'bbb', 2),(3, 'ccc', 3);
   148  
   149  create table t10(a int, b int, c int, primary key(a, b) CLUSTERED);
   150  insert into t10 values(1, 1, 1),(2, 2, 2),(3, 3, 3);
   151  
   152  create table t11(a int, b float, c int, primary key(a,b) CLUSTERED);
   153  insert into t11 values(1, 1.1, 1),(2, 2.2, 2),(3, 3.3, 3);
   154  
   155  create table t12(name char(255) primary key CLUSTERED, b int, c int, index idx(name), unique index uidx(name));
   156  insert into t12 values('aaaa', 1, 1), ('bbb', 2, 2), ('ccc', 3, 3);
   157  "
   158  
   159  clustered_table_count=$(run_sql "\
   160      SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES \
   161      WHERE tidb_pk_type = 'CLUSTERED' AND table_schema = '$DB';" \
   162      | awk '/COUNT/{print $2}')
   163  
   164  [ $clustered_table_count -gt 0 ] || { echo No clustered index table; exit 1; }
   165  
   166  # backup table
   167  echo "backup start..."
   168  run_br --pd $PD_ADDR backup db -s "local://$TEST_DIR/$DB" --db $DB
   169  
   170  # count
   171  echo "count rows..."
   172  row_counts=()
   173  for table_name in $table_names; do
   174      row_counts+=($(run_sql "SELECT COUNT(*) FROM $DB.$table_name;" | awk '/COUNT/{print $2}'))
   175  done
   176  
   177  run_sql "DROP DATABASE $DB;"
   178  run_sql "CREATE DATABASE $DB;"
   179  
   180  # restore table
   181  echo "restore start..."
   182  run_br restore db --db $DB -s "local://$TEST_DIR/$DB" --pd $PD_ADDR
   183  
   184  # check count
   185  echo "check count..."
   186  idx=0
   187  for table_name in $table_names; do
   188      row_count=$(run_sql "SELECT COUNT(*) FROM $DB.$table_name;" | awk '/COUNT/{print $2}')
   189      if [[ $row_count -ne ${row_counts[$idx]} ]]; then
   190          echo "Lost some rows in table $table_name. Expect ${row_counts[$idx]}; Get $row_count."
   191          exit 1
   192      fi
   193      idx=$(( $idx + 1 ))
   194  done
   195  
   196  run_sql "DROP DATABASE $DB;"