Skip to content

Commit

Permalink
Merge pull request #8570 from dolthub/db/ci-tests
Browse files Browse the repository at this point in the history
[no-release-notes] dolt ci bats tests
  • Loading branch information
coffeegoddd authored Nov 19, 2024
2 parents 5f96410 + 8b31a25 commit 79c5ec3
Show file tree
Hide file tree
Showing 4 changed files with 265 additions and 40 deletions.
8 changes: 4 additions & 4 deletions go/libraries/doltcore/env/actions/dolt_ci/column_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import (
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/sqlutil"
)

type ColumnValue struct {
type columnValue struct {
ColumnName string
Value string
}

const utf8RuneError = string(utf8.RuneError)

type ColumnValues []*ColumnValue
type columnValues []*columnValue

func toUtf8StringValue(col *sql.Column, val interface{}) (string, error) {
if val == nil {
Expand Down Expand Up @@ -61,7 +61,7 @@ func toUtf8StringValue(col *sql.Column, val interface{}) (string, error) {
}
}

func NewColumnValue(col *sql.Column, val interface{}) (*ColumnValue, error) {
func newColumnValue(col *sql.Column, val interface{}) (*columnValue, error) {
utf8Value, err := toUtf8StringValue(col, val)
if err != nil {
return nil, err
Expand All @@ -71,7 +71,7 @@ func NewColumnValue(col *sql.Column, val interface{}) (*ColumnValue, error) {
return nil, nil
}

return &ColumnValue{
return &columnValue{
ColumnName: col.Name,
Value: utf8Value,
}, nil
Expand Down
12 changes: 8 additions & 4 deletions go/libraries/doltcore/env/actions/dolt_ci/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ var ExpectedDoltCITablesOrdered = []doltdb.TableName{

type queryFunc func(ctx *sql.Context, query string) (sql.Schema, sql.RowIter, *sql.QueryFlags, error)

// HasDoltCITables reports whether a database has all expected dolt_ci tables which store continuous integration config.
// If the database has only some of the expected tables, an error is returned.
func HasDoltCITables(ctx *sql.Context) (bool, error) {
dbName := ctx.GetCurrentDatabase()
dSess := dsess.DSessFromSess(ctx.Session)
Expand Down Expand Up @@ -134,13 +136,14 @@ func commitCIInit(ctx *sql.Context, queryFunc queryFunc, commiterName, commiterE
return sqlWriteQuery(ctx, queryFunc, fmt.Sprintf("CALL DOLT_COMMIT('-m' 'Successfully initialized Dolt CI', '--author', '%s <%s>');", commiterName, commiterEmail))
}

// DestroyDoltCITables drops all dolt_ci tables and creates a new Dolt commit.
func DestroyDoltCITables(ctx *sql.Context, db sqle.Database, queryFunc queryFunc, commiterName, commiterEmail string) error {
if err := dsess.CheckAccessForDb(ctx, db, branch_control.Permissions_Write); err != nil {
return err
}

// disable foreign key checks
err := SqlWriteQuery(ctx, queryFunc, "SET FOREIGN_KEY_CHECKS=0;")
err := sqlWriteQuery(ctx, queryFunc, "SET FOREIGN_KEY_CHECKS=0;")
if err != nil {
return err
}
Expand All @@ -151,21 +154,22 @@ func DestroyDoltCITables(ctx *sql.Context, db sqle.Database, queryFunc queryFunc
}

for _, tableName := range existing {
err = SqlWriteQuery(ctx, queryFunc, fmt.Sprintf("DROP TABLE IF EXISTS %s;", tableName.Name))
err = sqlWriteQuery(ctx, queryFunc, fmt.Sprintf("DROP TABLE IF EXISTS %s;", tableName.Name))
if err != nil {
return err
}
}

// enable foreign keys again
err = SqlWriteQuery(ctx, queryFunc, "SET FOREIGN_KEY_CHECKS=1;")
err = sqlWriteQuery(ctx, queryFunc, "SET FOREIGN_KEY_CHECKS=1;")
if err != nil {
return err
}

return commitCIDestroy(ctx, queryFunc, commiterName, commiterEmail)
}

// CreateDoltCITables creates all dolt_ci tables and creates a new Dolt commit.
func CreateDoltCITables(ctx *sql.Context, db sqle.Database, queryFunc queryFunc, commiterName, commiterEmail string) error {
if err := dsess.CheckAccessForDb(ctx, db, branch_control.Permissions_Write); err != nil {
return err
Expand All @@ -187,7 +191,7 @@ func CreateDoltCITables(ctx *sql.Context, db sqle.Database, queryFunc queryFunc,
newCtx := doltdb.ContextWithDoltCICreateBypassKey(ctx)

for _, query := range orderedCreateTableQueries {
err := SqlWriteQuery(newCtx, queryFunc, query)
err := sqlWriteQuery(newCtx, queryFunc, query)
if err != nil {
return err
}
Expand Down
57 changes: 26 additions & 31 deletions go/libraries/doltcore/env/actions/dolt_ci/workflow_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ var ErrWorkflowNotFound = errors.New("workflow not found")
var ErrMultipleWorkflowsFound = errors.New("multiple workflows found")

type WorkflowManager interface {
// RemoveWorkflow deletes a workflow from the database and creates a Dolt commit
RemoveWorkflow(ctx *sql.Context, db sqle.Database, workflowName string) error
// ListWorkflows lists all workflows in the database.
ListWorkflows(ctx *sql.Context, db sqle.Database) ([]string, error)
// GetWorkflowConfig returns the WorkflowConfig for a workflow by name.
GetWorkflowConfig(ctx *sql.Context, db sqle.Database, workflowName string) (*WorkflowConfig, error)
// StoreAndCommit creates or updates a workflow and creates a Dolt commit
StoreAndCommit(ctx *sql.Context, db sqle.Database, config *WorkflowConfig) error
}

Expand Down Expand Up @@ -238,7 +242,7 @@ func (d *doltWorkflowManager) deleteFromSavedQueryStepExpectedRowColumnResultsTa
return fmt.Sprintf("delete from %s where `%s` = '%s';", doltdb.WorkflowSavedQueryStepExpectedRowColumnResultsTableName, doltdb.WorkflowSavedQueryStepExpectedRowColumnResultsSavedQueryStepIdFkColName, savedQueryStepID)
}

func (d *doltWorkflowManager) newWorkflow(cvs ColumnValues) (*Workflow, error) {
func (d *doltWorkflowManager) newWorkflow(cvs columnValues) (*Workflow, error) {
wf := &Workflow{}

for _, cv := range cvs {
Expand Down Expand Up @@ -266,7 +270,7 @@ func (d *doltWorkflowManager) newWorkflow(cvs ColumnValues) (*Workflow, error) {
return wf, nil
}

func (d *doltWorkflowManager) newWorkflowEvent(cvs ColumnValues) (*WorkflowEvent, error) {
func (d *doltWorkflowManager) newWorkflowEvent(cvs columnValues) (*WorkflowEvent, error) {
we := &WorkflowEvent{}

for _, cv := range cvs {
Expand Down Expand Up @@ -295,7 +299,7 @@ func (d *doltWorkflowManager) newWorkflowEvent(cvs ColumnValues) (*WorkflowEvent
return we, nil
}

func (d *doltWorkflowManager) newWorkflowJob(cvs ColumnValues) (*WorkflowJob, error) {
func (d *doltWorkflowManager) newWorkflowJob(cvs columnValues) (*WorkflowJob, error) {
wj := &WorkflowJob{}

for _, cv := range cvs {
Expand Down Expand Up @@ -328,7 +332,7 @@ func (d *doltWorkflowManager) newWorkflowJob(cvs ColumnValues) (*WorkflowJob, er
return wj, nil
}

func (d *doltWorkflowManager) newWorkflowSavedQueryStepExpectedRowColumnResult(cvs ColumnValues) (*WorkflowSavedQueryExpectedRowColumnResult, error) {
func (d *doltWorkflowManager) newWorkflowSavedQueryStepExpectedRowColumnResult(cvs columnValues) (*WorkflowSavedQueryExpectedRowColumnResult, error) {
r := &WorkflowSavedQueryExpectedRowColumnResult{}

for _, cv := range cvs {
Expand Down Expand Up @@ -391,7 +395,7 @@ func (d *doltWorkflowManager) newWorkflowSavedQueryStepExpectedRowColumnResult(c
return r, nil
}

func (d *doltWorkflowManager) newWorkflowSavedQueryStep(cvs ColumnValues) (*WorkflowSavedQueryStep, error) {
func (d *doltWorkflowManager) newWorkflowSavedQueryStep(cvs columnValues) (*WorkflowSavedQueryStep, error) {
sq := &WorkflowSavedQueryStep{}

for _, cv := range cvs {
Expand Down Expand Up @@ -424,7 +428,7 @@ func (d *doltWorkflowManager) newWorkflowSavedQueryStep(cvs ColumnValues) (*Work
return sq, nil
}

func (d *doltWorkflowManager) newWorkflowStep(cvs ColumnValues) (*WorkflowStep, error) {
func (d *doltWorkflowManager) newWorkflowStep(cvs columnValues) (*WorkflowStep, error) {
ws := &WorkflowStep{}

for _, cv := range cvs {
Expand Down Expand Up @@ -476,7 +480,7 @@ func (d *doltWorkflowManager) newWorkflowStep(cvs ColumnValues) (*WorkflowStep,
return ws, nil
}

func (d *doltWorkflowManager) newWorkflowEventTrigger(cvs ColumnValues) (*WorkflowEventTrigger, error) {
func (d *doltWorkflowManager) newWorkflowEventTrigger(cvs columnValues) (*WorkflowEventTrigger, error) {
et := &WorkflowEventTrigger{}

for _, cv := range cvs {
Expand Down Expand Up @@ -505,7 +509,7 @@ func (d *doltWorkflowManager) newWorkflowEventTrigger(cvs ColumnValues) (*Workfl
return et, nil
}

func (d *doltWorkflowManager) newWorkflowEventTriggerBranch(cvs ColumnValues) (*WorkflowEventTriggerBranch, error) {
func (d *doltWorkflowManager) newWorkflowEventTriggerBranch(cvs columnValues) (*WorkflowEventTriggerBranch, error) {
tb := &WorkflowEventTriggerBranch{}

for _, cv := range cvs {
Expand All @@ -526,7 +530,7 @@ func (d *doltWorkflowManager) newWorkflowEventTriggerBranch(cvs ColumnValues) (*
return tb, nil
}

func (d *doltWorkflowManager) newWorkflowEventTriggerActivity(cvs ColumnValues) (*WorkflowEventTriggerActivity, error) {
func (d *doltWorkflowManager) newWorkflowEventTriggerActivity(cvs columnValues) (*WorkflowEventTriggerActivity, error) {
ta := &WorkflowEventTriggerActivity{}

for _, cv := range cvs {
Expand Down Expand Up @@ -612,10 +616,10 @@ func (d *doltWorkflowManager) commitRemoveWorkflow(ctx *sql.Context, workflowNam
}

func (d *doltWorkflowManager) sqlWriteQuery(ctx *sql.Context, query string) error {
return SqlWriteQuery(ctx, d.queryFunc, query)
return sqlWriteQuery(ctx, d.queryFunc, query)
}

func (d *doltWorkflowManager) sqlReadQuery(ctx *sql.Context, query string, cb func(ctx *sql.Context, cvs ColumnValues) error) error {
func (d *doltWorkflowManager) sqlReadQuery(ctx *sql.Context, query string, cb func(ctx *sql.Context, cvs columnValues) error) error {
sch, rowIter, _, err := d.queryFunc(ctx, query)
if err != nil {
return err
Expand All @@ -629,12 +633,12 @@ func (d *doltWorkflowManager) sqlReadQuery(ctx *sql.Context, query string, cb fu
size := len(sch)
for _, row := range rows {

cvs := make(ColumnValues, size)
cvs := make(columnValues, size)

for i := range size {
col := sch[i]
val := row[i]
cv, err := NewColumnValue(col, val)
cv, err := newColumnValue(col, val)
if err != nil {
return err
}
Expand Down Expand Up @@ -738,7 +742,7 @@ func (d *doltWorkflowManager) listWorkflowEventTriggerBranchesByEventTriggerId(c
func (d *doltWorkflowManager) retrieveWorkflowSavedQueryExpectedRowColumnResults(ctx *sql.Context, query string) ([]*WorkflowSavedQueryExpectedRowColumnResult, error) {
workflowSavedQueryExpectedResults := make([]*WorkflowSavedQueryExpectedRowColumnResult, 0)

cb := func(cbCtx *sql.Context, cvs ColumnValues) error {
cb := func(cbCtx *sql.Context, cvs columnValues) error {
er, rerr := d.newWorkflowSavedQueryStepExpectedRowColumnResult(cvs)
if rerr != nil {
return rerr
Expand All @@ -759,7 +763,7 @@ func (d *doltWorkflowManager) retrieveWorkflowSavedQueryExpectedRowColumnResults
func (d *doltWorkflowManager) retrieveWorkflowSavedQuerySteps(ctx *sql.Context, query string) ([]*WorkflowSavedQueryStep, error) {
workflowSavedQuerySteps := make([]*WorkflowSavedQueryStep, 0)

cb := func(cbCtx *sql.Context, cvs ColumnValues) error {
cb := func(cbCtx *sql.Context, cvs columnValues) error {
sq, rerr := d.newWorkflowSavedQueryStep(cvs)
if rerr != nil {
return rerr
Expand All @@ -780,7 +784,7 @@ func (d *doltWorkflowManager) retrieveWorkflowSavedQuerySteps(ctx *sql.Context,
func (d *doltWorkflowManager) retrieveWorkflowSteps(ctx *sql.Context, query string) ([]*WorkflowStep, error) {
workflowSteps := make([]*WorkflowStep, 0)

cb := func(cbCtx *sql.Context, cvs ColumnValues) error {
cb := func(cbCtx *sql.Context, cvs columnValues) error {
s, rerr := d.newWorkflowStep(cvs)
if rerr != nil {
return rerr
Expand All @@ -801,7 +805,7 @@ func (d *doltWorkflowManager) retrieveWorkflowSteps(ctx *sql.Context, query stri
func (d *doltWorkflowManager) retrieveWorkflowJobs(ctx *sql.Context, query string) ([]*WorkflowJob, error) {
workflowJobs := make([]*WorkflowJob, 0)

cb := func(cbCtx *sql.Context, cvs ColumnValues) error {
cb := func(cbCtx *sql.Context, cvs columnValues) error {
j, rerr := d.newWorkflowJob(cvs)
if rerr != nil {
return rerr
Expand All @@ -821,7 +825,7 @@ func (d *doltWorkflowManager) retrieveWorkflowJobs(ctx *sql.Context, query strin
func (d *doltWorkflowManager) retrieveWorkflowEventTriggerActivities(ctx *sql.Context, query string) ([]*WorkflowEventTriggerActivity, error) {
workflowEventTriggerActivities := make([]*WorkflowEventTriggerActivity, 0)

cb := func(cbCtx *sql.Context, cvs ColumnValues) error {
cb := func(cbCtx *sql.Context, cvs columnValues) error {
a, rerr := d.newWorkflowEventTriggerActivity(cvs)
if rerr != nil {
return rerr
Expand All @@ -841,7 +845,7 @@ func (d *doltWorkflowManager) retrieveWorkflowEventTriggerActivities(ctx *sql.Co
func (d *doltWorkflowManager) retrieveWorkflowEventTriggerBranches(ctx *sql.Context, query string) ([]*WorkflowEventTriggerBranch, error) {
workflowEventTriggerBranches := make([]*WorkflowEventTriggerBranch, 0)

cb := func(cbCtx *sql.Context, cvs ColumnValues) error {
cb := func(cbCtx *sql.Context, cvs columnValues) error {
b, rerr := d.newWorkflowEventTriggerBranch(cvs)
if rerr != nil {
return rerr
Expand All @@ -862,7 +866,7 @@ func (d *doltWorkflowManager) retrieveWorkflowEventTriggerBranches(ctx *sql.Cont
func (d *doltWorkflowManager) retrieveWorkflowEventTriggers(ctx *sql.Context, query string) ([]*WorkflowEventTrigger, error) {
workflowEventTriggers := make([]*WorkflowEventTrigger, 0)

cb := func(cbCtx *sql.Context, cvs ColumnValues) error {
cb := func(cbCtx *sql.Context, cvs columnValues) error {
wet, rerr := d.newWorkflowEventTrigger(cvs)
if rerr != nil {
return rerr
Expand All @@ -882,7 +886,7 @@ func (d *doltWorkflowManager) retrieveWorkflowEventTriggers(ctx *sql.Context, qu
func (d *doltWorkflowManager) retrieveWorkflowEvents(ctx *sql.Context, query string) ([]*WorkflowEvent, error) {
workflowEvents := make([]*WorkflowEvent, 0)

cb := func(cbCtx *sql.Context, cvs ColumnValues) error {
cb := func(cbCtx *sql.Context, cvs columnValues) error {
we, rerr := d.newWorkflowEvent(cvs)
if rerr != nil {
return rerr
Expand All @@ -902,7 +906,7 @@ func (d *doltWorkflowManager) retrieveWorkflowEvents(ctx *sql.Context, query str

func (d *doltWorkflowManager) retrieveWorkflows(ctx *sql.Context, query string) ([]*Workflow, error) {
workflows := make([]*Workflow, 0)
cb := func(cbCtx *sql.Context, cvs ColumnValues) error {
cb := func(cbCtx *sql.Context, cvs columnValues) error {
wf, rerr := d.newWorkflow(cvs)
if rerr != nil {
return rerr
Expand Down Expand Up @@ -2053,15 +2057,6 @@ func (d *doltWorkflowManager) StoreAndCommit(ctx *sql.Context, db sqle.Database,
return d.commitWorkflow(ctx, config.Name.Value)
}

func SqlWriteQuery(ctx *sql.Context, qf queryFunc, query string) error {
_, rowIter, _, err := qf(ctx, query)
if err != nil {
return err
}
_, err = sql.RowIterToRows(ctx, rowIter)
return err
}

func newScalarDoubleQuotedYamlNode(value string) yaml.Node {
return yaml.Node{
Kind: yaml.ScalarNode,
Expand Down
Loading

0 comments on commit 79c5ec3

Please sign in to comment.