// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package testcases

import (
	"time"

	"github.com/pingcap/test-infra/caselib/pkg/model/workload"
	"github.com/pingcap/test-infra/caselib/pkg/spec"
	"github.com/pingcap/test-infra/caselib/pkg/steps"
)

func init() {
	var (
		name           = "dm_tolerance_unsupport_ddl"
		env            = newCommonEnv()
		taskName       = "test"
		sourceNameList = []string{"source-0"}
	)
	var _ = AddTestCase(spec.CaseName(name), func() steps.Step {
		return env.
			init().
			// cleanUpEnv(name).
			initDbsAndSourcesAndTasks(name).
			toBuilder().
			DmRunWorkload(env.UpStreams[0], env.Dm, env.Workload, Spec.GetWorkloadOption(workload.CommandTypePrepare)).
			Parallel(
				steps.NewRunDMWorkload(env.UpStreams[0], env.Workload, Spec.GetWorkloadOption(workload.CommandTypeRun)),
				steps.NewSerial(
					steps.NewSleepTask(1*time.Minute),
					steps.NewExecuteSQL(env.UpStreams[0], name, "alter table sbtest1 add column d int unique;"),
					steps.NewMatchQueryStatusStep(env.DmHost, taskName, "cannot track DDL: ALTER TABLE", 1),
					steps.NewDmctlCommandStep(env.DmHost, `handle-error test replace "alter table dm_tolerance_unsupport_ddl.sbtest1 add column d int; alter table dm_tolerance_unsupport_ddl.sbtest1 add unique(d);"`),
				),
			).
			DmWaitTaskSynced(env.DmHost, taskName, sourceNameList).
			DmCheckSyncDiff(env.Dm, env.SyncDiff, env.DmHost, Path.DiffPaths[0]).
			Build()
	})
}
