github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/tests/mq_protocol_tests/cases/case_alter.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package cases
    15  
    16  import (
    17  	"fmt"
    18  	"math/rand"
    19  
    20  	"github.com/pingcap/errors"
    21  	"github.com/pingcap/tiflow/tests/mq_protocol_tests/framework"
    22  )
    23  
    24  // AlterCase is base impl of test case for alter operation
    25  type AlterCase struct {
    26  	framework.Task
    27  }
    28  
    29  // NewAlterCase create a test case which contains alter ddls
    30  func NewAlterCase(task framework.Task) *AlterCase {
    31  	return &AlterCase{
    32  		Task: task,
    33  	}
    34  }
    35  
    36  // Name impl framework.Task interface
    37  func (c *AlterCase) Name() string {
    38  	return "Alter"
    39  }
    40  
    41  // Run impl framework.Task interface
    42  func (c *AlterCase) Run(ctx *framework.TaskContext) error {
    43  	_, err := ctx.Upstream.ExecContext(ctx.Ctx, "create table test (id int primary key)")
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	for i := 0; i < 20; i++ {
    49  		_, err := ctx.Upstream.ExecContext(ctx.Ctx, fmt.Sprintf("alter table test add column (value%d int)", i))
    50  		if err != nil {
    51  			return err
    52  		}
    53  
    54  		table := ctx.SQLHelper().GetTable("test")
    55  		reqs := make([]framework.Awaitable, 0)
    56  		for j := 0; j < 1000; j++ {
    57  			rowData := make(map[string]interface{}, i+1)
    58  			rowData["id"] = i*1000 + j
    59  			for k := 0; k <= i; k++ {
    60  				rowData[fmt.Sprintf("value%d", k)] = rand.Int31()
    61  			}
    62  			awaitable := table.Insert(rowData).Send()
    63  			reqs = append(reqs, awaitable)
    64  		}
    65  
    66  		err = framework.All(ctx.SQLHelper(), reqs).Wait().Check()
    67  		if err != nil {
    68  			return errors.AddStack(err)
    69  		}
    70  	}
    71  
    72  	return nil
    73  }