diff --git a/backend/postgres/postgres.go b/backend/postgres/postgres.go index 10d8336..7d03000 100644 --- a/backend/postgres/postgres.go +++ b/backend/postgres/postgres.go @@ -140,7 +140,7 @@ func (be *postgresBackend) AbandonOrchestrationWorkItem(ctx context.Context, wi } defer tx.Rollback(ctx) //nolint:errcheck // rollback after commit is a no-op - var visibleTime*time.Time = nil + var visibleTime *time.Time = nil if delay := wi.GetAbandonDelay(); delay > 0 { t := time.Now().UTC().Add(delay) visibleTime = &t @@ -497,6 +497,12 @@ func (be *postgresBackend) createOrchestrationInstanceInternal(ctx context.Conte } func insertOrIgnoreInstanceTableInternal(ctx context.Context, tx pgx.Tx, e *backend.HistoryEvent, startEvent *protos.ExecutionStartedEvent) (int64, error) { + var parentInstanceID *string + if pi := startEvent.GetParentInstance(); pi != nil { + if instanceID := pi.GetOrchestrationInstance().GetInstanceId(); instanceID != "" { + parentInstanceID = &instanceID + } + } res, err := tx.Exec( ctx, `INSERT INTO Instances ( @@ -506,8 +512,9 @@ func insertOrIgnoreInstanceTableInternal(ctx context.Context, tx pgx.Tx, e *back ExecutionID, Input, RuntimeStatus, - CreatedTime - ) VALUES ($1, $2, $3, $4, $5, $6, $7) ON CONFLICT DO NOTHING`, + CreatedTime, + ParentInstanceID + ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) ON CONFLICT DO NOTHING`, startEvent.Name, startEvent.Version.GetValue(), startEvent.OrchestrationInstance.InstanceId, @@ -515,6 +522,7 @@ func insertOrIgnoreInstanceTableInternal(ctx context.Context, tx pgx.Tx, e *back startEvent.Input.GetValue(), "PENDING", e.Timestamp.AsTime(), + parentInstanceID, ) if err != nil { return -1, fmt.Errorf("failed to insert into Instances table: %w", err) @@ -769,7 +777,7 @@ func (be *postgresBackend) GetOrchestrationWorkItem(ctx context.Context) (*backe defer tx.Rollback(ctx) //nolint:errcheck // rollback after commit is a no-op now := time.Now().UTC() - newLockExpiration:= now.Add(be.options.OrchestrationLockTimeout) + newLockExpiration := now.Add(be.options.OrchestrationLockTimeout) // Place a lock on an orchestration instance that has new events that are ready to be executed. row := tx.QueryRow( diff --git a/backend/sqlite/sqlite.go b/backend/sqlite/sqlite.go index 1e1d1ea..86b62e9 100644 --- a/backend/sqlite/sqlite.go +++ b/backend/sqlite/sqlite.go @@ -470,6 +470,12 @@ func (be *sqliteBackend) createOrchestrationInstanceInternal(ctx context.Context } func insertOrIgnoreInstanceTableInternal(ctx context.Context, tx *sql.Tx, e *backend.HistoryEvent, startEvent *protos.ExecutionStartedEvent) (int64, error) { + var parentInstanceID *string + if pi := startEvent.GetParentInstance(); pi != nil { + if instanceID := pi.GetOrchestrationInstance().GetInstanceId(); instanceID != "" { + parentInstanceID = &instanceID + } + } res, err := tx.ExecContext( ctx, `INSERT OR IGNORE INTO [Instances] ( @@ -479,8 +485,9 @@ func insertOrIgnoreInstanceTableInternal(ctx context.Context, tx *sql.Tx, e *bac [ExecutionID], [Input], [RuntimeStatus], - [CreatedTime] - ) VALUES (?, ?, ?, ?, ?, ?, ?)`, + [CreatedTime], + [ParentInstanceID] + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, startEvent.Name, startEvent.Version.GetValue(), startEvent.OrchestrationInstance.InstanceId, @@ -488,6 +495,7 @@ func insertOrIgnoreInstanceTableInternal(ctx context.Context, tx *sql.Tx, e *bac startEvent.Input.GetValue(), "PENDING", e.Timestamp.AsTime(), + parentInstanceID, ) if err != nil { return -1, fmt.Errorf("failed to insert into [Instances] table: %w", err)