github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/cdc/entry/schema_test_helper.go (about)

     1  // Copyright 2021 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 entry
    15  
    16  import (
    17  	"github.com/pingcap/check"
    18  	timodel "github.com/pingcap/parser/model"
    19  	ticonfig "github.com/pingcap/tidb/config"
    20  	"github.com/pingcap/tidb/domain"
    21  	"github.com/pingcap/tidb/kv"
    22  	timeta "github.com/pingcap/tidb/meta"
    23  	"github.com/pingcap/tidb/session"
    24  	"github.com/pingcap/tidb/store/mockstore"
    25  	"github.com/pingcap/tidb/store/tikv/oracle"
    26  	"github.com/pingcap/tidb/util/testkit"
    27  )
    28  
    29  // SchemaTestHelper is a test helper for schema which creates an internal tidb instance to generate DDL jobs with meta information
    30  type SchemaTestHelper struct {
    31  	c       *check.C
    32  	tk      *testkit.TestKit
    33  	storage kv.Storage
    34  	domain  *domain.Domain
    35  }
    36  
    37  // NewSchemaTestHelper creates a SchemaTestHelper
    38  func NewSchemaTestHelper(c *check.C) *SchemaTestHelper {
    39  	store, err := mockstore.NewMockStore()
    40  	c.Assert(err, check.IsNil)
    41  	ticonfig.UpdateGlobal(func(conf *ticonfig.Config) {
    42  		conf.AlterPrimaryKey = true
    43  	})
    44  	session.SetSchemaLease(0)
    45  	session.DisableStats4Test()
    46  	domain, err := session.BootstrapSession(store)
    47  	c.Assert(err, check.IsNil)
    48  	domain.SetStatsUpdating(true)
    49  	tk := testkit.NewTestKit(c, store)
    50  	return &SchemaTestHelper{
    51  		c:       c,
    52  		tk:      tk,
    53  		storage: store,
    54  		domain:  domain,
    55  	}
    56  }
    57  
    58  // DDL2Job executes the DDL stmt and returns the DDL job
    59  func (s *SchemaTestHelper) DDL2Job(ddl string) *timodel.Job {
    60  	s.tk.MustExec(ddl)
    61  	jobs, err := s.GetCurrentMeta().GetLastNHistoryDDLJobs(1)
    62  	s.c.Assert(err, check.IsNil)
    63  	s.c.Assert(jobs, check.HasLen, 1)
    64  	return jobs[0]
    65  }
    66  
    67  // Storage return the tikv storage
    68  func (s *SchemaTestHelper) Storage() kv.Storage {
    69  	return s.storage
    70  }
    71  
    72  // GetCurrentMeta return the current meta snapshot
    73  func (s *SchemaTestHelper) GetCurrentMeta() *timeta.Meta {
    74  	ver, err := s.storage.CurrentVersion(oracle.GlobalTxnScope)
    75  	s.c.Assert(err, check.IsNil)
    76  	return timeta.NewSnapshotMeta(s.storage.GetSnapshot(ver))
    77  }
    78  
    79  // Close closes the helper
    80  func (s *SchemaTestHelper) Close() {
    81  	s.domain.Close()
    82  	s.storage.Close() //nolint:errcheck
    83  }