Skip to content

Commit 3d904b0

Browse files
committed
refactor: add rollback to failed schema update call.
1 parent 420eec3 commit 3d904b0

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

core/server/client_handlers/SchemaHandler.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,12 @@ void SchemaHandler::handleUpdate(const std::string &a_uid,
167167
std::string schema_type = "json-schema";
168168
std::string schema_format = "json";
169169

170+
// Get current state of the schema document
171+
libjson::Value sch;
172+
170173
if (a_request.has_def()) {
171174
// Look up existing schema to determine type/format
172175
try {
173-
libjson::Value sch;
174176
m_db_client.schemaView(a_request.id(), sch, log_context);
175177
schema_type =
176178
sch.asArray().begin()->asObject().getValue("type").toString();
@@ -219,8 +221,23 @@ void SchemaHandler::handleUpdate(const std::string &a_uid,
219221
m_schema_factory.getStorage(schema_type).updateContent(
220222
a_request.id(), a_request.def(), desc, log_context);
221223
} catch (exception &e) {
224+
225+
SchemaUpdateRequest rollback_request;
226+
auto & sch_doc = sch.asArray().begin()->asObject();
227+
rollback_request.set_id(sch_doc.getValue("id").toString());
228+
rollback_request.set_desc(sch_doc.getValue("desc").toString());
229+
rollback_request.set_pub(sch_doc.getBool("pub"));
230+
rollback_request.set_sys(sch_doc.getBool("sys"));
231+
222232
DL_ERROR(log_context,
223-
"Schema storage update failed: " << e.what());
233+
"Schema storage update failed attempting rollback: " << e.what());
234+
try {
235+
m_db_client.schemaUpdate(rollback_request, log_context);
236+
} catch (exception &e) {
237+
DL_ERROR(log_context,
238+
"Schema storage update rollback failed: " << e.what());
239+
240+
}
224241
EXCEPT_PARAM(1, "Schema storage update failed: " << e.what());
225242
}
226243
}

0 commit comments

Comments
 (0)