// Equal tests whether two database have same data and schema.
Equal tests whether two database have same data and schema.
// Equal tests whether two database have same data and schema .
func (df *Diff) Equal() (err error) {
	defer func() {
		df.fixSQLFile.Close()
		df.db1.Close()
		df.db2.Close()
	}()
	df.wg.Add(1)
	go func() {
		df.WriteSqls()
		df.wg.Done()
	}()
	tbls1, err := dbutil.GetTables(df.ctx, df.db1, df.schema)
	if err != nil {
		err = errors.Trace(err)
		return
	}
	tbls2, err := dbutil.GetTables(df.ctx, df.db2, df.schema)
	if err != nil {
		err = errors.Trace(err)
		return
	}
	eq := equalStrings(tbls1, tbls2)
	if !eq && len(df.tables) == 0 {
		log.Errorf("show tables get different table. [source db tables] %v [target db tables] %v", tbls1, tbls2)
		df.report.Result = Fail
	}
	if len(df.tables) == 0 {
		df.tables = make([]*TableCheckCfg, 0, len(tbls1))
		for _, name := range tbls1 {
			table := &TableCheckCfg{Name: name, Schema: df.schema}
			table.Info, err = dbutil.GetTableInfoWithRowID(df.ctx, df.db1, df.schema, name, df.useRowID)
			if err != nil {
				return errors.Trace(err)
			}
			table.Schema = df.schema
			df.tables = append(df.tables, table)
		}
	}
	for _, table := range df.tables {
		tableInfo1 := table.Info
		tableInfo2, err := dbutil.GetTableInfoWithRowID(df.ctx, df.db2, df.schema, table.Name, df.useRowID)
		if err != nil {
			return errors.Trace(err)
		}
		eq1, err := df.EqualTableStruct(tableInfo1, tableInfo2)
		if err != nil {
			return errors.Trace(err)
		}
		if !eq1 {
			log.Errorf("table have different struct: %s\n", table.Name)
		}
		df.report.SetTableStructCheckResult(table.Name, eq1)
		eq2, err := df.EqualTableData(table)
		if err != nil {
			log.Errorf("equal table error %v", err)
			return errors.Trace(err)
		}
		if !eq2 {
			log.Errorf("table %s's data is not equal", table.Name)
		}
		df.report.SetTableDataCheckResult(table.Name, eq2)
		if eq1 && eq2 {
			df.report.PassNum++
		} else {
			df.report.FailedNum++
		}
	}
	df.sqlCh <- "end"
	df.wg.Wait()
	return
}
func ( df * Diff ) Equal ( ) ( err error ) { defer func ( ) { df . fixSQLFile . Close ( ) df . db1 . Close ( ) df . db2 . Close ( ) } ( ) df . wg . Add ( 1 ) go func ( ) { df . WriteSqls ( ) df . wg . Done ( ) } ( ) tbls1 , err := dbutil . GetTables ( df . ctx , df . db1 , df . schema ) if err != nil { err = errors . Trace ( err ) return } tbls2 , err := dbutil . GetTables ( df . ctx , df . db2 , df . schema ) if err != nil { err = errors . Trace ( err ) return } eq := equalStrings ( tbls1 , tbls2 ) if ! eq && len ( df . tables ) == 0 { log . Errorf ( " " , tbls1 , tbls2 ) df . report . Result = Fail } if len ( df . tables ) == 0 { df . tables = make ( [ ] * TableCheckCfg , 0 , len ( tbls1 ) ) for _ , name := range tbls1 { table := & TableCheckCfg { Name : name , Schema : df . schema } table . Info , err = dbutil . GetTableInfoWithRowID ( df . ctx , df . db1 , df . schema , name , df . useRowID ) if err != nil { return errors . Trace ( err ) } table . Schema = df . schema df . tables = append ( df . tables , table ) } } for _ , table := range df . tables { tableInfo1 := table . Info tableInfo2 , err := dbutil . GetTableInfoWithRowID ( df . ctx , df . db2 , df . schema , table . Name , df . useRowID ) if err != nil { return errors . Trace ( err ) } eq1 , err := df . EqualTableStruct ( tableInfo1 , tableInfo2 ) if err != nil { return errors . Trace ( err ) } if ! eq1 { log . Errorf ( " \n " , table . Name ) } df . report . SetTableStructCheckResult ( table . Name , eq1 ) eq2 , err := df . EqualTableData ( table ) if err != nil { log . Errorf ( " " , err ) return errors . Trace ( err ) } if ! eq2 { log . Errorf ( " " , table . Name ) } df . report . SetTableDataCheckResult ( table . Name , eq2 ) if eq1 && eq2 { df . report . PassNum ++ } else { df . report . FailedNum ++ } } df . sqlCh <- " " df . wg . Wait ( ) return }
Equal tests whether two database have same data and schema.
Equal tests whether two database have same data and schema .
// len(df.tables) == 0 means check all tables