Skip to content

Commit 8254ccf

Browse files
authored
Use cancelled status for statements following error (sqlpad#919)
1 parent a4f1c52 commit 8254ccf

5 files changed

Lines changed: 21 additions & 5 deletions

File tree

server/lib/execute-batch.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,29 @@ async function executeBatch(config, models, webhooks, batchId) {
7373
durationMs: stopTime - batchStartTime,
7474
});
7575
webhooks.statementFinished(user, connection, updatedBatch, statement.id);
76-
webhooks.batchFinished(user, connection, updatedBatch);
7776
break;
7877
}
7978
}
8079
stopTime = new Date();
8180

81+
if (statementError) {
82+
await models.statements.updateErrorQueuedToCancelled(batchId);
83+
}
84+
8285
if (!statementError) {
83-
const updatedBatch = await models.batches.update(batch.id, {
86+
await models.batches.update(batch.id, {
8487
status: 'finished',
8588
stopTime,
8689
durationMs: stopTime - batchStartTime,
8790
});
88-
webhooks.batchFinished(user, connection, updatedBatch);
8991
}
9092

9193
if (disconnectOnFinish) {
9294
await connectionClient.disconnect();
9395
}
96+
97+
const finalBatch = await models.batches.findOneById(batchId);
98+
webhooks.batchFinished(user, connection, finalBatch);
9499
}
95100

96101
module.exports = executeBatch;

server/models/statements.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,17 @@ class Statements {
138138
error,
139139
};
140140
await this.sequelizeDb.Statements.update(update, { where: { id } });
141+
141142
return this.findOneById(id);
142143
}
143144

145+
async updateErrorQueuedToCancelled(batchId) {
146+
await this.sequelizeDb.Statements.update(
147+
{ status: 'cancelled' },
148+
{ where: { batchId, status: 'queued' } }
149+
);
150+
}
151+
144152
async updateFinished(id, queryResult, stopTime, durationMs) {
145153
const { config } = this;
146154
const dbPath = config.get('dbPath');

server/sequelize-db/batches.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module.exports = function (sequelize) {
2828
validate: {
2929
// Potentially add 'cancelled'?
3030
// We have no way of canceling current statement but future could be
31+
// If a batch errors, any statements following error will be "cancelled" status
3132
isIn: [['started', 'finished', 'error']],
3233
},
3334
defaultValue: 'started',

server/sequelize-db/statements.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ module.exports = function (sequelize) {
2121
type: Sequelize.TEXT,
2222
allowNull: false,
2323
},
24+
// If a batch errors, any statements following error will be "cancelled" status
25+
// At this time statements/batches cannot be cancelled otherwise
2426
status: {
2527
type: Sequelize.STRING,
2628
allowNull: false,
2729
validate: {
28-
isIn: [['queued', 'started', 'finished', 'error']],
30+
isIn: [['queued', 'started', 'finished', 'error', 'cancelled']],
2931
},
3032
defaultValue: 'queued',
3133
},

server/test/api/batches.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ describe('api/batches', function () {
199199
assert.deepEqual(b.statements[0].error, {
200200
title: 'SQLITE_ERROR: incomplete input',
201201
});
202-
assert.equal(b.statements[1].status, 'queued');
202+
assert.equal(b.statements[1].status, 'cancelled');
203203
assert.equal(b.statements[1].rowCount, null, 'no rowCount');
204204
assert.equal(b.statements[1].resultsPath, null, 'no resultpath');
205205
assert.equal(b.statements[1].startTime, null, 'no startTime');

0 commit comments

Comments
 (0)