Skip to content

Commit

Permalink
/{go,integration-tests}: add workflow event triggers table
Browse files Browse the repository at this point in the history
  • Loading branch information
coffeegoddd committed Oct 23, 2024
1 parent 4b9ff43 commit 953f8b1
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 1 deletion.
12 changes: 12 additions & 0 deletions go/libraries/doltcore/doltdb/system_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,18 @@ const (

// WorkflowEventsEventTypeColName is the name of the event type column in the workflow events table.
WorkflowEventsEventTypeColName = "event_type"

// WorkflowEventTriggersTableName is the dolt CI workflow event triggers system table name
WorkflowEventTriggersTableName = "dolt_ci_workflow_event_triggers"

// WorkflowEventTriggersIdPkColName is the name of the primary key id column on the workflow event triggers table.
WorkflowEventTriggersIdPkColName = "id"

// WorkflowEventTriggersWorkflowEventsIdFkColName is the name of the workflow event id foreign key in the workflow event triggers table.
WorkflowEventTriggersWorkflowEventsIdFkColName = "workflow_event_id_fk"

// WorkflowEventTriggerEventTriggerTypeColName is the type of the event trigger on the workflow event triggers table.
WorkflowEventTriggersEventTriggerTypeColName = "event_trigger_type"
)

const (
Expand Down
7 changes: 6 additions & 1 deletion go/libraries/doltcore/env/actions/dolt_ci/dolt_ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
var ExpectedDoltCITables = []doltdb.TableName{
doltdb.TableName{Name: doltdb.WorkflowsTableName},
doltdb.TableName{Name: doltdb.WorkflowEventsTableName},
doltdb.TableName{Name: doltdb.WorkflowEventTriggersTableName},
}

func HasDoltCITables(ctx *sql.Context) (bool, error) {
Expand Down Expand Up @@ -145,5 +146,9 @@ func createDoltCITables(ctx *sql.Context) error {
if err != nil {
return err
}
return createWorkflowEventsTable(ctx)
err = createWorkflowEventsTable(ctx)
if err != nil {
return err
}
return createWorkflowEventTriggersTable(ctx)
}
168 changes: 168 additions & 0 deletions go/libraries/doltcore/env/actions/dolt_ci/workflow_event_triggers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// Copyright 2024 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dolt_ci

import (
"fmt"
"github.com/dolthub/dolt/go/cmd/dolt/errhand"
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb/durable"
"github.com/dolthub/dolt/go/libraries/doltcore/schema"
"github.com/dolthub/dolt/go/libraries/doltcore/schema/typeinfo"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess"
stypes "github.com/dolthub/dolt/go/store/types"
"github.com/dolthub/go-mysql-server/sql"
)

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

root := ws.WorkingRoot()

found, err := root.HasTable(ctx, doltdb.TableName{Name: doltdb.WorkflowEventTriggersTableName})
if err != nil {
return err
}
if found {
return nil
}

colCollection := schema.NewColCollection(
schema.Column{
Name: doltdb.WorkflowEventTriggersIdPkColName,
Tag: schema.WorkflowEventTriggersIdTag,
Kind: stypes.StringKind,
IsPartOfPK: true,
TypeInfo: typeinfo.FromKind(stypes.StringKind),
Default: "",
AutoIncrement: false,
Comment: "",
Constraints: []schema.ColConstraint{schema.NotNullConstraint{}},
},
schema.Column{
Name: doltdb.WorkflowEventTriggersWorkflowEventsIdFkColName,
Tag: schema.WorkflowEventTriggerWorkflowEventIdFkTag,
Kind: stypes.StringKind,
IsPartOfPK: false,
TypeInfo: typeinfo.FromKind(stypes.StringKind),
Default: "",
AutoIncrement: false,
Comment: "",
Constraints: []schema.ColConstraint{schema.NotNullConstraint{}},
},
schema.Column{
Name: doltdb.WorkflowEventTriggersEventTriggerTypeColName,
Tag: schema.WorkflowEventTriggerEventTriggerTypeTag,
Kind: stypes.IntKind,
IsPartOfPK: false,
TypeInfo: typeinfo.FromKind(stypes.IntKind),
Default: "",
AutoIncrement: false,
Comment: "",
Constraints: []schema.ColConstraint{schema.NotNullConstraint{}},
},
)

sch, err := schema.NewSchema(colCollection, nil, schema.Collation_Default, nil, nil)
if err != nil {
return err
}

// underlying table doesn't exist. Record this, then create the table.
nrv, err := doltdb.CreateEmptyTable(ctx, root, doltdb.TableName{Name: doltdb.WorkflowEventTriggersTableName}, sch)
if err != nil {
return err
}

sfkc := sql.ForeignKeyConstraint{
Name: fmt.Sprintf("%s_%s", doltdb.WorkflowEventTriggersTableName, doltdb.WorkflowEventTriggersWorkflowEventsIdFkColName),
Database: dbName,
Table: doltdb.WorkflowEventTriggersTableName,
Columns: []string{doltdb.WorkflowEventTriggersWorkflowEventsIdFkColName},
ParentDatabase: dbName,
ParentTable: doltdb.WorkflowEventTriggersTableName,
ParentColumns: []string{doltdb.WorkflowEventsIdPkColName},
OnDelete: sql.ForeignKeyReferentialAction_Cascade,
OnUpdate: sql.ForeignKeyReferentialAction_DefaultAction,
IsResolved: false,
}

onUpdateRefAction, err := sqle.ParseFkReferentialAction(sfkc.OnUpdate)
if err != nil {
return err
}

onDeleteRefAction, err := sqle.ParseFkReferentialAction(sfkc.OnDelete)
if err != nil {
return err
}

vrw := nrv.VRW()
ns := nrv.NodeStore()

empty, err := durable.NewEmptyIndex(ctx, vrw, ns, sch)
if err != nil {
return errhand.BuildDError("error: failed to get table.").AddCause(err).Build()
}

indexSet, err := durable.NewIndexSetWithEmptyIndexes(ctx, vrw, ns, sch)
if err != nil {
return errhand.BuildDError("error: failed to get table.").AddCause(err).Build()
}

tbl, err := doltdb.NewTable(ctx, vrw, ns, sch, empty, indexSet, nil)
if err != nil {
return err
}

nrv, err = nrv.PutTable(ctx, doltdb.TableName{Name: doltdb.WorkflowEventTriggersTableName}, tbl)
if err != nil {
return err
}

doltFk, err := CreateDoltCITableForeignKey(ctx, nrv, tbl, sch, sfkc, onUpdateRefAction, onDeleteRefAction, dbName)
if err != nil {
return err
}

fkc, err := nrv.GetForeignKeyCollection(ctx)
if err != nil {
return err
}

err = fkc.AddKeys(doltFk)
if err != nil {
return err
}

nrv, err = nrv.PutForeignKeyCollection(ctx, fkc)
if err != nil {
return err
}

newWorkingSet := ws.WithWorkingRoot(nrv)
err = dSess.SetWorkingSet(ctx, dbName, newWorkingSet)
if err != nil {
return err
}

return dSess.SetWorkingRoot(ctx, dbName, nrv)
}
9 changes: 9 additions & 0 deletions go/libraries/doltcore/schema/reserved_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,13 @@ const (

// WorkflowEventsEventTypeTag is the tag of the events typ column in the workflow events table
WorkflowEventsEventTypeTag

// WorkflowEventTriggersIdTag is the tag of the id column of the workflow event triggers table
WorkflowEventTriggersIdTag

// WorkflowEventTriggerWorkflowEventIdFkTag is the tag of the workflow events id fk column in the workflow event triggers table
WorkflowEventTriggerWorkflowEventIdFkTag

// WorkflowEventTriggerEventTriggerTypeTag is the tag of the event trigger type column on the workflow event triggers table
WorkflowEventTriggerEventTriggerTypeTag
)
6 changes: 6 additions & 0 deletions integration-tests/bats/ci.bats
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ teardown() {

run dolt sql -q "select * from dolt_ci_workflows;"
[ "$status" -eq 0 ]

run dolt sql -q "select * from dolt_ci_workflow_events;"
[ "$status" -eq 0 ]

run dolt sql -q "select * from dolt_ci_workflow_event_triggers;"
[ "$status" -eq 0 ]
}

@test "ci: should not allow users to alter the rows or schema of dolt ci workflow tables directly" {
Expand Down

0 comments on commit 953f8b1

Please sign in to comment.