github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/integration/tests/case_composite_pkey.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 tests
    15  
    16  import (
    17  	"github.com/pingcap/errors"
    18  	"github.com/pingcap/ticdc/integration/framework"
    19  )
    20  
    21  // CompositePKeyCase is base impl of test case for composite primary keys
    22  type CompositePKeyCase struct {
    23  	framework.Task
    24  }
    25  
    26  // NewCompositePKeyCase create a test case which have composite primary key
    27  func NewCompositePKeyCase(task framework.Task) *CompositePKeyCase {
    28  	return &CompositePKeyCase{
    29  		Task: task,
    30  	}
    31  }
    32  
    33  // Name impl framework.Task interface
    34  func (s *CompositePKeyCase) Name() string {
    35  	return "Composite Primary Key"
    36  }
    37  
    38  // Run impl framework.Task interface
    39  func (s *CompositePKeyCase) Run(ctx *framework.TaskContext) error {
    40  	_, err := ctx.Upstream.ExecContext(ctx.Ctx, "create table test (id1 int, id2 int, value int, primary key (id1, id2))")
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	// Get a handle of an existing table
    46  	table := ctx.SQLHelper().GetTable("test")
    47  	// Create an SQL request, send it to the upstream, wait for completion and check the correctness of replication
    48  	err = table.Insert(map[string]interface{}{
    49  		"id1":   0,
    50  		"id2":   1,
    51  		"value": 0,
    52  	}).Send().Wait().Check()
    53  	if err != nil {
    54  		return errors.AddStack(err)
    55  	}
    56  
    57  	err = table.Upsert(map[string]interface{}{
    58  		"id1":   0,
    59  		"id2":   1,
    60  		"value": 1,
    61  	}).Send().Wait().Check()
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	err = table.Delete(map[string]interface{}{
    67  		"id1": 0,
    68  		"id2": 1,
    69  	}).Send().Wait().Check()
    70  	return err
    71  }