Skip to content

Commit

Permalink
/go/{cmd,libraries}: add check for ci tables during init
Browse files Browse the repository at this point in the history
  • Loading branch information
coffeegoddd committed Oct 22, 2024
1 parent fed2cb6 commit 864ef39
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
14 changes: 13 additions & 1 deletion go/cmd/dolt/commands/ci/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package ci

import (
"context"
"fmt"
"github.com/dolthub/dolt/go/cmd/dolt/cli"
"github.com/dolthub/dolt/go/cmd/dolt/commands"
"github.com/dolthub/dolt/go/cmd/dolt/errhand"
Expand Down Expand Up @@ -89,8 +90,19 @@ func (cmd InitCmd) Exec(ctx context.Context, commandStr string, args []string, d
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
}

tc := sqle.NewDoltCITablesCreator(sqlCtx, db)
var verr errhand.VerboseError
tc := sqle.NewDoltCITablesCreator(sqlCtx, db)

hasTables, err := tc.HasTables(sqlCtx)
if err != nil {
verr = errhand.VerboseErrorFromError(err)
}

if hasTables {
verr = errhand.VerboseErrorFromError(fmt.Errorf("dolt ci has already been initialized"))
return commands.HandleVErrAndExitCode(verr, usage)
}

if err = tc.CreateTables(sqlCtx); err != nil {
verr = errhand.VerboseErrorFromError(err)
}
Expand Down
34 changes: 34 additions & 0 deletions go/libraries/doltcore/sqle/dolt_ci_tables_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,18 @@ import (
"github.com/dolthub/go-mysql-server/sql"
)

var ExpectedDoltCITables = []string{
doltdb.WorkflowsTableName,
doltdb.WorkflowEventsTableName,
}

type DoltCITablesCreator interface {
// HasTables is used to check whether the database
// already contains dolt ci tables. If any expected tables are missing,
// an error is returned
HasTables(ctx *sql.Context) (bool, error)

// CreateTables creates all tables required for dolt ci
CreateTables(ctx *sql.Context) error
}

Expand Down Expand Up @@ -51,6 +62,29 @@ func (d *doltCITablesCreator) createTables(ctx *sql.Context) error {
return d.workflowEventsTC.CreateTable(ctx)
}

func (d *doltCITablesCreator) HasTables(ctx *sql.Context) (bool, error) {
dbName := ctx.GetCurrentDatabase()
dSess := dsess.DSessFromSess(ctx.Session)
ws, err := dSess.WorkingSet(ctx, dbName)
if err != nil {
return false, err
}

root := ws.WorkingRoot()

for _, tableName := range ExpectedDoltCITables {
found, err := root.HasTable(ctx, doltdb.TableName{Name: tableName})
if err != nil {
return false, err
}
if !found {
return false, fmt.Errorf("required dolt ci table `%s` not found", doltdb.WorkflowsTableName)
}
}

return true, nil
}

func (d *doltCITablesCreator) CreateTables(ctx *sql.Context) error {
if err := dsess.CheckAccessForDb(d.ctx, d.db, branch_control.Permissions_Write); err != nil {
return err
Expand Down

0 comments on commit 864ef39

Please sign in to comment.