Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove event handling from migrations #1256

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ import (
"github.com/teamkeel/keel/auditing"
"github.com/teamkeel/keel/casing"
"github.com/teamkeel/keel/db"
"github.com/teamkeel/keel/events"
"github.com/teamkeel/keel/proto"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"google.golang.org/protobuf/encoding/protojson"
)

Expand Down Expand Up @@ -83,12 +81,6 @@ func (m *Migrations) Apply(ctx context.Context) error {
ctx, span := tracer.Start(ctx, "Apply Migrations")
defer span.End()

// Tracing is required because there may be data mutations which
// should log audits and fire events, if necessary.
if !span.SpanContext().IsValid() {
return errors.New("migrations cannot continue without active tracing enabled")
}

sql := strings.Builder{}

// Enable extensions
Expand Down Expand Up @@ -129,15 +121,6 @@ func (m *Migrations) Apply(ctx context.Context) error {
return err
}

// Generate and send any events for this context.
// This must run regardless of the migration succeeding or failing.
// Failure to generate events fail silently.
eventsErr := events.SendEvents(ctx, m.Schema)
if eventsErr != nil {
span.RecordError(eventsErr)
span.SetStatus(codes.Error, eventsErr.Error())
}

return nil
}

Expand Down
89 changes: 0 additions & 89 deletions runtime/runtime_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/karlseguin/typed"
"github.com/stretchr/testify/require"
"github.com/teamkeel/keel/events"
"github.com/teamkeel/keel/migrations"
"github.com/teamkeel/keel/proto"
"github.com/teamkeel/keel/runtime/actions"
)
Expand Down Expand Up @@ -403,91 +402,3 @@ func TestFailedEventHandling(t *testing.T) {
_, ok := result.(map[string]any)
require.True(t, ok)
}

func TestEventsDatabaseMigration(t *testing.T) {
var keelSchema = `
model Person {
fields {
name Text
age Number?
isActive Boolean?
}
actions {
create createPerson() with (name)
}
@on([update], doSomething)
@permission(expression: true, actions: [create])
}`

ctx, database, pSchema := newContext(t, keelSchema, true)

create := proto.FindAction(pSchema, "createPerson")
result, _, err := actions.Execute(
actions.NewScope(ctx, create, pSchema),
map[string]any{"name": "Dave"})
require.NoError(t, err)

person, ok := result.(map[string]any)
require.True(t, ok)

var updatedSchema = `
model Person {
fields {
name Text
age Number @default(0)
isActive Boolean @default(true)
}
actions {
create createPerson() with (name)
}
@on([update], doSomething)
@permission(expression: true, actions: [create])
}`

database.Close()
ctx, database, pSchema = newContext(t, updatedSchema, false)
defer database.Close()

handler := NewEventHandler(t)
ctx, err = events.WithEventHandler(ctx, handler.HandleEvent)
require.NoError(t, err)

// Migrate the database to the new schema.
m, err := migrations.New(ctx, pSchema, database)
require.NoError(t, err)

err = m.Apply(ctx)
require.NoError(t, err)

require.Len(t, handler.handledEvents, 1)

events, ok := handler.handledEvents["doSomething"]
require.True(t, ok)
require.Len(t, events, 2)

require.NotEmpty(t, events[0])
require.Equal(t, "person.updated", events[0].EventName)
require.Empty(t, events[0].IdentityId)
require.NotEmpty(t, events[0].OccurredAt)
require.NotNil(t, events[0].Target)
require.Equal(t, person["id"], events[0].Target.Id)
require.Equal(t, "Person", events[0].Target.Type)

data := typed.New(events[0].Target.Data)
require.Equal(t, person["id"], data.String("id"))
require.Equal(t, 0, data.IntMust("age"))
require.Empty(t, data.String("isActive"))

require.NotEmpty(t, events[1])
require.Equal(t, "person.updated", events[0].EventName)
require.Empty(t, events[1].IdentityId)
require.NotEmpty(t, events[1].OccurredAt)
require.NotNil(t, events[1].Target)
require.Equal(t, person["id"], events[1].Target.Id)
require.Equal(t, "Person", events[1].Target.Type)

data = typed.New(events[1].Target.Data)
require.Equal(t, person["id"], data.String("id"))
require.Equal(t, 0, data.IntMust("age"))
require.Equal(t, true, data.BoolMust("isActive"))
}
Loading