Skip to content

Commit

Permalink
Merge pull request #8583 from dolthub/db/rm-ta
Browse files Browse the repository at this point in the history
[no-release-notes] /{go,integration-tests}: remove trigger activities table in favor of typed activities
  • Loading branch information
coffeegoddd authored Nov 26, 2024
2 parents b5d8263 + 340f11b commit 6200a33
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 243 deletions.
16 changes: 3 additions & 13 deletions go/libraries/doltcore/doltdb/system_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ var getWriteableSystemTables = func() []string {
WorkflowEventsTableName,
WorkflowEventTriggersTableName,
WorkflowEventTriggerBranchesTableName,
WorkflowEventTriggerActivitiesTableName,
WorkflowJobsTableName,
WorkflowStepsTableName,
WorkflowSavedQueryStepsTableName,
Expand Down Expand Up @@ -474,6 +473,9 @@ const (
// WorkflowEventTriggerBranchesTableName is the name of the workflow event trigger branches table name
WorkflowEventTriggerBranchesTableName = "dolt_ci_workflow_event_trigger_branches"

// WorkflowEventTriggerActivitiesTableName is then name of a now removed dolt_ci table
WorkflowEventTriggerActivitiesTableName = "dolt_ci_workflow_event_trigger_activities"

// WorkflowEventTriggerBranchesIdPkColName is the name of the id column on the workflow event trigger branches table.
WorkflowEventTriggerBranchesIdPkColName = "id"

Expand All @@ -483,18 +485,6 @@ const (
// WorkflowEventTriggerBranchesBranch is the name of the branch column on the workflow event trigger branches table.
WorkflowEventTriggerBranchesBranchColName = "branch"

// WorkflowEventTriggerActivitiesTableName is the name of the workflow event trigger activities table name
WorkflowEventTriggerActivitiesTableName = "dolt_ci_workflow_event_trigger_activities"

// WorkflowEventTriggerActivitiesIdPkColName is the name of the id column on the workflow event trigger activities table.
WorkflowEventTriggerActivitiesIdPkColName = "id"

// WorkflowEventTriggerActivitiesWorkflowEventTriggersIdFkColName is the name of the workflow event triggers id foreign key column on the workflow event trigger activities table
WorkflowEventTriggerActivitiesWorkflowEventTriggersIdFkColName = "workflow_event_triggers_id_fk"

// WorkflowEventTriggerActivitiesActivity is the name of the activity column on the workflow event trigger activities table.
WorkflowEventTriggerActivitiesActivityColName = "activity"

// WorkflowJobsTableName is the name of the workflow jobs table name
WorkflowJobsTableName = "dolt_ci_workflow_jobs"

Expand Down
78 changes: 47 additions & 31 deletions go/libraries/doltcore/env/actions/dolt_ci/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,38 @@ import (
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess"
)

// WrappedTableName is a struct that wraps a doltdb.TableName
// and specifies whether the tables should still be created.
// Deprecated tables will have Deprecated: true
type WrappedTableName struct {
TableName doltdb.TableName
Deprecated bool
}

type WrappedTableNameSlice []WrappedTableName

func (w WrappedTableNameSlice) ActiveTableNames() []doltdb.TableName {
tableNames := make([]doltdb.TableName, 0)
for _, wrapt := range w {
if !wrapt.Deprecated {
tableNames = append(tableNames, wrapt.TableName)
}
}
return tableNames
}

// ExpectedDoltCITablesOrdered contains the tables names for the dolt ci workflow tables, in parent to child table order.
// This is exported for use in DoltHub/DoltLab.
var ExpectedDoltCITablesOrdered = []doltdb.TableName{
{Name: doltdb.WorkflowsTableName},
{Name: doltdb.WorkflowEventsTableName},
{Name: doltdb.WorkflowEventTriggersTableName},
{Name: doltdb.WorkflowEventTriggerBranchesTableName},
{Name: doltdb.WorkflowEventTriggerActivitiesTableName},
{Name: doltdb.WorkflowJobsTableName},
{Name: doltdb.WorkflowStepsTableName},
{Name: doltdb.WorkflowSavedQueryStepsTableName},
{Name: doltdb.WorkflowSavedQueryStepExpectedRowColumnResultsTableName},
var ExpectedDoltCITablesOrdered = WrappedTableNameSlice{
{TableName: doltdb.TableName{Name: doltdb.WorkflowsTableName}},
{TableName: doltdb.TableName{Name: doltdb.WorkflowEventsTableName}},
{TableName: doltdb.TableName{Name: doltdb.WorkflowEventTriggersTableName}},
{TableName: doltdb.TableName{Name: doltdb.WorkflowEventTriggerBranchesTableName}},
{TableName: doltdb.TableName{Name: doltdb.WorkflowEventTriggerActivitiesTableName}, Deprecated: true},
{TableName: doltdb.TableName{Name: doltdb.WorkflowJobsTableName}},
{TableName: doltdb.TableName{Name: doltdb.WorkflowStepsTableName}},
{TableName: doltdb.TableName{Name: doltdb.WorkflowSavedQueryStepsTableName}},
{TableName: doltdb.TableName{Name: doltdb.WorkflowSavedQueryStepExpectedRowColumnResultsTableName}},
}

type queryFunc func(ctx *sql.Context, query string) (sql.Schema, sql.RowIter, *sql.QueryFlags, error)
Expand All @@ -52,11 +72,12 @@ func HasDoltCITables(ctx *sql.Context) (bool, error) {
}

root := ws.WorkingRoot()
activeOnly := ExpectedDoltCITablesOrdered.ActiveTableNames()

exists := 0
var hasSome bool
var hasAll bool
for _, tableName := range ExpectedDoltCITablesOrdered {
for _, tableName := range activeOnly {
found, err := root.HasTable(ctx, tableName)
if err != nil {
return false, err
Expand All @@ -66,8 +87,8 @@ func HasDoltCITables(ctx *sql.Context) (bool, error) {
}
}

hasSome = exists > 0 && exists < len(ExpectedDoltCITablesOrdered)
hasAll = exists == len(ExpectedDoltCITablesOrdered)
hasSome = exists > 0 && exists < len(activeOnly)
hasAll = exists == len(activeOnly)
if !hasSome && !hasAll {
return false, nil
}
Expand All @@ -88,13 +109,13 @@ func getExistingDoltCITables(ctx *sql.Context) ([]doltdb.TableName, error) {

root := ws.WorkingRoot()

for _, tableName := range ExpectedDoltCITablesOrdered {
found, err := root.HasTable(ctx, tableName)
for _, wrapt := range ExpectedDoltCITablesOrdered {
found, err := root.HasTable(ctx, wrapt.TableName)
if err != nil {
return nil, err
}
if found {
existing = append(existing, tableName)
existing = append(existing, wrapt.TableName)
}
}

Expand All @@ -110,25 +131,25 @@ func sqlWriteQuery(ctx *sql.Context, queryFunc queryFunc, query string) error {
return err
}

func commitCIDestroy(ctx *sql.Context, queryFunc queryFunc, commiterName, commiterEmail string) error {
func commitCIDestroy(ctx *sql.Context, queryFunc queryFunc, tableNames []doltdb.TableName, commiterName, commiterEmail string) error {
// stage table in reverse order so child tables
// are staged before parent tables
for i := len(ExpectedDoltCITablesOrdered) - 1; i >= 0; i-- {
tableName := ExpectedDoltCITablesOrdered[i]
err := sqlWriteQuery(ctx, queryFunc, fmt.Sprintf("CALL DOLT_ADD('%s');", tableName))
for i := len(tableNames) - 1; i >= 0; i-- {
tn := tableNames[i]
err := sqlWriteQuery(ctx, queryFunc, fmt.Sprintf("CALL DOLT_ADD('%s');", tn.Name))
if err != nil {
return err
}
}
return sqlWriteQuery(ctx, queryFunc, fmt.Sprintf("CALL DOLT_COMMIT('-m' 'Successfully destroyed Dolt CI', '--author', '%s <%s>');", commiterName, commiterEmail))
}

func commitCIInit(ctx *sql.Context, queryFunc queryFunc, commiterName, commiterEmail string) error {
func commitCIInit(ctx *sql.Context, queryFunc queryFunc, tableNames []doltdb.TableName, commiterName, commiterEmail string) error {
// stage table in reverse order so child tables
// are staged before parent tables
for i := len(ExpectedDoltCITablesOrdered) - 1; i >= 0; i-- {
tableName := ExpectedDoltCITablesOrdered[i]
err := sqlWriteQuery(ctx, queryFunc, fmt.Sprintf("CALL DOLT_ADD('%s');", tableName))
for i := len(tableNames) - 1; i >= 0; i-- {
tn := tableNames[i]
err := sqlWriteQuery(ctx, queryFunc, fmt.Sprintf("CALL DOLT_ADD('%s');", tn.Name))
if err != nil {
return err
}
Expand Down Expand Up @@ -166,7 +187,7 @@ func DestroyDoltCITables(ctx *sql.Context, db sqle.Database, queryFunc queryFunc
return err
}

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

// CreateDoltCITables creates all dolt_ci tables and creates a new Dolt commit.
Expand All @@ -180,7 +201,6 @@ func CreateDoltCITables(ctx *sql.Context, db sqle.Database, queryFunc queryFunc,
createWorkflowEventsTableQuery(),
createWorkflowEventTriggersTableQuery(),
createWorkflowEventTriggerBranchesTableQuery(),
createWorkflowEventTriggerActivitiesTableQuery(),
createWorkflowJobsTableQuery(),
createWorkflowStepsTableQuery(),
createWorkflowSavedQueryStepsTableQuery(),
Expand All @@ -197,7 +217,7 @@ func CreateDoltCITables(ctx *sql.Context, db sqle.Database, queryFunc queryFunc,
}
}

return commitCIInit(newCtx, queryFunc, commiterName, commiterEmail)
return commitCIInit(newCtx, queryFunc, ExpectedDoltCITablesOrdered.ActiveTableNames(), commiterName, commiterEmail)
}

func createWorkflowsTableQuery() string {
Expand All @@ -216,10 +236,6 @@ func createWorkflowEventTriggerBranchesTableQuery() string {
return fmt.Sprintf("create table %s (`%s` varchar(36) primary key, `%s` varchar(1024) collate utf8mb4_0900_ai_ci not null, `%s` varchar(36) not null, foreign key (`%s`) references %s (`%s`) on delete cascade);", doltdb.WorkflowEventTriggerBranchesTableName, doltdb.WorkflowEventTriggerBranchesIdPkColName, doltdb.WorkflowEventTriggerBranchesBranchColName, doltdb.WorkflowEventTriggerBranchesWorkflowEventTriggersIdFkColName, doltdb.WorkflowEventTriggerBranchesWorkflowEventTriggersIdFkColName, doltdb.WorkflowEventTriggersTableName, doltdb.WorkflowEventTriggersIdPkColName)
}

func createWorkflowEventTriggerActivitiesTableQuery() string {
return fmt.Sprintf("create table %s (`%s` varchar(36) primary key, `%s` varchar(1024) collate utf8mb4_0900_ai_ci not null, `%s` varchar(36) not null, foreign key (`%s`) references %s (`%s`) on delete cascade);", doltdb.WorkflowEventTriggerActivitiesTableName, doltdb.WorkflowEventTriggerActivitiesIdPkColName, doltdb.WorkflowEventTriggerActivitiesActivityColName, doltdb.WorkflowEventTriggerActivitiesWorkflowEventTriggersIdFkColName, doltdb.WorkflowEventTriggerActivitiesWorkflowEventTriggersIdFkColName, doltdb.WorkflowEventTriggersTableName, doltdb.WorkflowEventTriggersIdPkColName)
}

func createWorkflowJobsTableQuery() string {
return fmt.Sprintf("create table %s (`%s` varchar(36) primary key, `%s` varchar(1024) collate utf8mb4_0900_ai_ci not null, `%s` datetime(6) not null, `%s` datetime(6) not null, `%s` varchar(2048) collate utf8mb4_0900_ai_ci not null, foreign key (`%s`) references %s (`%s`) on delete cascade);", doltdb.WorkflowJobsTableName, doltdb.WorkflowJobsIdPkColName, doltdb.WorkflowJobsNameColName, doltdb.WorkflowJobsCreatedAtColName, doltdb.WorkflowJobsUpdatedAtColName, doltdb.WorkflowJobsWorkflowNameFkColName, doltdb.WorkflowJobsWorkflowNameFkColName, doltdb.WorkflowsTableName, doltdb.WorkflowsNameColName)
}
Expand Down
4 changes: 4 additions & 0 deletions go/libraries/doltcore/env/actions/dolt_ci/workflow_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ func ValidateWorkflowConfig(workflow *WorkflowConfig) error {

activities := make(map[string]bool)
for _, activity := range workflow.On.PullRequest.Activities {
_, err := ToWorkflowEventTriggerActivityType(activity.Value)
if err != nil {
return fmt.Errorf("invalid config: unknown activity type: %s", activity.Value)
}
_, ok := activities[activity.Value]
if ok {
return fmt.Errorf("invalid config: on pull request activities duplicated: %s", activity.Value)
Expand Down
18 changes: 17 additions & 1 deletion go/libraries/doltcore/env/actions/dolt_ci/workflow_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ type WorkflowEvent struct {
EventType WorkflowEventType `db:"event_type"`
}

func toWorkflowEventType(t int) (WorkflowEventType, error) {
// ToWorkflowEventType is used to convert an int to a valid WorkflowEventType
func ToWorkflowEventType(t int) (WorkflowEventType, error) {
switch t {
case int(WorkflowEventTypePush):
return WorkflowEventTypePush, nil
Expand All @@ -49,3 +50,18 @@ func toWorkflowEventType(t int) (WorkflowEventType, error) {
return WorkflowEventTypeUnspecified, ErrUnknownWorkflowEventType
}
}

// WorkflowEventTypeToString is used to change a valid WorkflowEventType to a string.
// This is used by DoltHub/DoltLab.
func WorkflowEventTypeToString(t WorkflowEventType) (string, error) {
switch t {
case WorkflowEventTypePush:
return "push", nil
case WorkflowEventTypePullRequest:
return "pull request", nil
case WorkflowEventTypeWorkflowDispatch:
return "workflow dispatch", nil
default:
return "", ErrUnknownWorkflowEventType
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@ package dolt_ci

import (
"errors"
"strings"
)

var ErrUnknownWorkflowEventTriggerType = errors.New("unknown workflow event trigger type")
var ErrUnknownWorkflowEventTriggerActivityType = errors.New("unknown workflow event trigger activity type")

type WorkflowEventTriggerType int

const (
WorkflowEventTriggerTypeUnspecified WorkflowEventTriggerType = iota
WorkflowEventTriggerTypeBranches
WorkflowEventTriggerTypeActivities
WorkflowEventTriggerTypeWorkflowDispatch
WorkflowEventTriggerTypeActivityOpened
WorkflowEventTriggerTypeActivityClosed
WorkflowEventTriggerTypeActivityReopened
WorkflowEventTriggerTypeActivitySynchronized
)

type WorkflowEventTriggerId string
Expand All @@ -37,14 +41,51 @@ type WorkflowEventTrigger struct {
EventTriggerType WorkflowEventTriggerType `db:"event_trigger_type"`
}

func toWorkflowEventTriggerType(t int) (WorkflowEventTriggerType, error) {
// ToWorkflowEventTriggerType is used to change an in to a valid WorkflowEventTriggerType
func ToWorkflowEventTriggerType(t int) (WorkflowEventTriggerType, error) {
switch t {
case int(WorkflowEventTriggerTypeBranches):
return WorkflowEventTriggerTypeBranches, nil
case int(WorkflowEventTriggerTypeActivities):
return WorkflowEventTriggerTypeActivities, nil
case int(WorkflowEventTriggerTypeWorkflowDispatch):
return WorkflowEventTriggerTypeWorkflowDispatch, nil
case int(WorkflowEventTriggerTypeActivityOpened):
return WorkflowEventTriggerTypeActivityOpened, nil
case int(WorkflowEventTriggerTypeActivityClosed):
return WorkflowEventTriggerTypeActivityClosed, nil
case int(WorkflowEventTriggerTypeActivityReopened):
return WorkflowEventTriggerTypeActivityReopened, nil
case int(WorkflowEventTriggerTypeActivitySynchronized):
return WorkflowEventTriggerTypeActivitySynchronized, nil
default:
return WorkflowEventTriggerTypeUnspecified, ErrUnknownWorkflowEventTriggerType
}
}

// WorkflowEventTriggerActivityTypeToString is used to change a valid WorkflowEventTriggerType to a string
func WorkflowEventTriggerActivityTypeToString(t WorkflowEventTriggerType) (string, error) {
switch t {
case WorkflowEventTriggerTypeActivityOpened:
return "opened", nil
case WorkflowEventTriggerTypeActivityClosed:
return "closed", nil
case WorkflowEventTriggerTypeActivityReopened:
return "reopened", nil
case WorkflowEventTriggerTypeActivitySynchronized:
return "synchronized", nil
default:
return "", ErrUnknownWorkflowEventTriggerType
}
}

// ToWorkflowEventTriggerActivityType is used to change a string to a valid WorkflowEventTriggerType
func ToWorkflowEventTriggerActivityType(str string) (WorkflowEventTriggerType, error) {
switch strings.ToLower(str) {
case "opened":
return WorkflowEventTriggerTypeActivityOpened, nil
case "closed":
return WorkflowEventTriggerTypeActivityClosed, nil
case "reopened":
return WorkflowEventTriggerTypeActivityReopened, nil
case "synchronized":
return WorkflowEventTriggerTypeActivitySynchronized, nil
default:
return WorkflowEventTriggerTypeUnspecified, ErrUnknownWorkflowEventTriggerType
}
Expand Down

This file was deleted.

Loading

0 comments on commit 6200a33

Please sign in to comment.