github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/tests/_utils/run_sql_file_online_ddl (about)

     1  #!/bin/bash
     2  # parameter 1: sql file
     3  # parameter 2: host
     4  # parameter 3: port
     5  # parameter 4: password
     6  # parameter 5: db
     7  # parameter 6: online ddl tool, pt or gh-ost
     8  
     9  set -eu
    10  
    11  sql_file=$1
    12  host=$2
    13  port=$3
    14  password=$4
    15  schema=$5
    16  ghost_bin=${GHOST_BINARY:-gh-ost}
    17  ptosc_bin=${PTOSC_BINARY:-pt-online-schema-change}
    18  
    19  echo "[$(date)] Executing SQL: $sql_file" >"$TEST_DIR/sql_res.$TEST_NAME.txt"
    20  
    21  # we use lower case `alter table` in test sql, if want case insensetive,
    22  # just set `shopt -s nocasematch`
    23  ddl_regex="^alter table.*"
    24  while IFS= read -r line; do
    25  	if [[ "$line" =~ $ddl_regex ]]; then
    26  		table=$(echo $line | cut -d " " -f3)
    27  		alter=$(echo "$line" | cut -d " " -f4-)
    28  		echo "alter: $alter"
    29  		# gh-ost check connection port whether equals to `select @@global.port`.
    30  		# if we have test MySQL in container and port mapping, these two ports
    31  		# may different. So we cheat gh-ost that we are running on aliyun rds,
    32  		# on which will disable the port check.
    33  		if [ "$6" == "gh-ost" ]; then
    34  			$ghost_bin -version
    35  			$ghost_bin --user=root --host=$host --port=$port --password=$password \
    36  				--database=$schema --table=$table --alter="$alter" \
    37  				--serve-socket-file="$TEST_DIR/gh-ost.$schema.$table.$port.sock" \
    38  				--allow-on-master --allow-master-master --initially-drop-ghost-table \
    39  				--initially-drop-old-table -ok-to-drop-table -aliyun-rds -execute \
    40  				>>$TEST_DIR/gh-ost.log
    41  		elif [ "$6" == "pt" ]; then
    42  			$ptosc_bin --version
    43  			$ptosc_bin --user=root --host=$host --port=$port --password=$password \
    44  				--alter="$alter" D=$schema,t=$table \
    45  				--recursion-method=none --print --execute \
    46  				>>$TEST_DIR/pt-osc.log
    47  			$ptosc_bin --user=root --host=$host --port=$port --password=$password \
    48  				--alter "COMMENT='string' CHARACTER SET='utf8mb4'" D=$schema,t=$table \
    49  				--recursion-method=none --print --execute \
    50  				>>$TEST_DIR/pt-osc.log
    51  		else
    52  			mysql -uroot -h$host -P$port -p$password --default-character-set utf8 -E -e "use $schema; $line" >>"$TEST_DIR/sql_res.$TEST_NAME.txt"
    53  		fi
    54  	else
    55  		mysql -uroot -h$host -P$port -p$password --default-character-set utf8 -E -e "use $schema; $line" >>"$TEST_DIR/sql_res.$TEST_NAME.txt"
    56  	fi
    57  done <"$sql_file"