github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/testutils/sqlutils/scrub.go (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package sqlutils
    12  
    13  import (
    14  	gosql "database/sql"
    15  	"fmt"
    16  	"time"
    17  
    18  	"github.com/cockroachdb/errors"
    19  )
    20  
    21  // ScrubResult is the go struct for the row results for an
    22  // EXPERIMENTAL SCRUB query.
    23  type ScrubResult struct {
    24  	ErrorType  string
    25  	Database   string
    26  	Table      string
    27  	PrimaryKey string
    28  	Timestamp  time.Time
    29  	Repaired   bool
    30  	Details    string
    31  }
    32  
    33  // GetScrubResultRows will scan and unmarshal ScrubResults from a Rows
    34  // iterator. The Rows iterate must from an EXPERIMENTAL SCRUB query.
    35  func GetScrubResultRows(rows *gosql.Rows) (results []ScrubResult, err error) {
    36  	defer rows.Close()
    37  
    38  	var unused *string
    39  	for rows.Next() {
    40  		result := ScrubResult{}
    41  		if err := rows.Scan(
    42  			// TODO(joey): In the future, SCRUB will run as a job during execution.
    43  			&unused, /* job_uuid */
    44  			&result.ErrorType,
    45  			&result.Database,
    46  			&result.Table,
    47  			&result.PrimaryKey,
    48  			&result.Timestamp,
    49  			&result.Repaired,
    50  			&result.Details,
    51  		); err != nil {
    52  			return nil, err
    53  		}
    54  		results = append(results, result)
    55  	}
    56  
    57  	if rows.Err() != nil {
    58  		return nil, err
    59  	}
    60  
    61  	return results, nil
    62  }
    63  
    64  // RunScrub will run execute an exhaustive scrub check for a table.
    65  func RunScrub(sqlDB *gosql.DB, database string, table string) error {
    66  	return RunScrubWithOptions(sqlDB, database, table, "")
    67  }
    68  
    69  // RunScrubWithOptions will run a SCRUB check for a table with the specified options string.
    70  func RunScrubWithOptions(sqlDB *gosql.DB, database string, table string, options string) error {
    71  	rows, err := sqlDB.Query(fmt.Sprintf(`EXPERIMENTAL SCRUB TABLE %s.%s %s`,
    72  		database, table, options))
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	results, err := GetScrubResultRows(rows)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	if len(results) > 0 {
    83  		return errors.Errorf("expected no scrub results instead got: %#v", results)
    84  	}
    85  	return nil
    86  }