@@ -62,7 +62,22 @@ type mysqlBackend struct {
62
62
options backend.Options
63
63
}
64
64
65
- // CreateWorkflowInstance creates a new workflow instance
65
+ func (b * mysqlBackend ) Logger () log.Logger {
66
+ return b .options .Logger
67
+ }
68
+
69
+ func (b * mysqlBackend ) Tracer () trace.Tracer {
70
+ return b .options .TracerProvider .Tracer (backend .TracerName )
71
+ }
72
+
73
+ func (b * mysqlBackend ) Metrics () metrics.Client {
74
+ return b .options .Metrics .WithTags (metrics.Tags {metrickeys .Backend : "mysql" })
75
+ }
76
+
77
+ func (b * mysqlBackend ) Converter () converter.Converter {
78
+ return b .options .Converter
79
+ }
80
+
66
81
func (b * mysqlBackend ) CreateWorkflowInstance (ctx context.Context , instance * workflow.Instance , event * history.Event ) error {
67
82
tx , err := b .db .BeginTx (ctx , & sql.TxOptions {
68
83
Isolation : sql .LevelReadCommitted ,
@@ -89,20 +104,37 @@ func (b *mysqlBackend) CreateWorkflowInstance(ctx context.Context, instance *wor
89
104
return nil
90
105
}
91
106
92
- func (b * mysqlBackend ) Logger () log.Logger {
93
- return b .options .Logger
94
- }
107
+ func (b * mysqlBackend ) RemoveWorkflowInstance (ctx context.Context , instance * core.WorkflowInstance ) error {
108
+ tx , err := b .db .BeginTx (ctx , nil )
109
+ if err != nil {
110
+ return err
111
+ }
112
+ defer tx .Rollback ()
95
113
96
- func (b * mysqlBackend ) Tracer () trace.Tracer {
97
- return b .options .TracerProvider .Tracer (backend .TracerName )
98
- }
114
+ instanceID := instance .InstanceID
99
115
100
- func (b * mysqlBackend ) Metrics () metrics.Client {
101
- return b .options .Metrics .WithTags (metrics.Tags {metrickeys .Backend : "mysql" })
102
- }
116
+ row := tx .QueryRowContext (ctx , "SELECT completed_at FROM `instances` WHERE instance_id = ? LIMIT 1" , instanceID )
117
+ var completedAt sql.NullTime
118
+ if err := row .Scan (& completedAt ); err != nil {
119
+ if err == sql .ErrNoRows {
120
+ return backend .ErrInstanceNotFound
121
+ }
122
+ }
103
123
104
- func (b * mysqlBackend ) Converter () converter.Converter {
105
- return b .options .Converter
124
+ if ! completedAt .Valid {
125
+ return backend .ErrInstanceNotFinished
126
+ }
127
+
128
+ // Delete from instances and history tables
129
+ if _ , err := tx .ExecContext (ctx , "DELETE FROM `instances` WHERE instance_id = ?" , instanceID ); err != nil {
130
+ return err
131
+ }
132
+
133
+ if _ , err := tx .ExecContext (ctx , "DELETE FROM `history` WHERE instance_id = ?" , instanceID ); err != nil {
134
+ return err
135
+ }
136
+
137
+ return tx .Commit ()
106
138
}
107
139
108
140
func (b * mysqlBackend ) CancelWorkflowInstance (ctx context.Context , instance * workflow.Instance , event * history.Event ) error {
0 commit comments