Hi all,
I'm trying to import a csv-based textfile into a PostgreSQL database with Swift, Vapor and Fluent.
While importing data, several distinct errors might occur, some of them are acceptable, others aren't. Because of the latter, I'm running the import within a transaction, so I'm able to rollback all inserts, updates and deletes in case one of those non-acceptable errors occurs.
My code catches the acceptable errors, for example if a file is reimported and a duplicate key leads to a constraint violation. At least, the code should catch them.
Unfortunately I'm currently facing the problem, that - after one of those constraint violation errors - the transaction seems to be aborted although I've caught the error.
Here is a shortened code snippet:
…
do {
try await productColorSize.save(on: transaction)
} catch let databaseError as DatabaseError {
if databaseError.isConstraintFailure {
print("CAUTION: Constraint error: productColorSize already exists.")
} else {
print(databaseError)
}
}
do {
try await gtin.save(on: transaction)
} catch let databaseError as DatabaseError {
if databaseError.isConstraintFailure {
print("error while saving gtin")
} else {
print("error: " + String(reflecting: databaseError))
}
}
…
The call of gtin.save results in the following message, when the preceding productColorSize.save has led to a contraint violation:
error: PSQLError(code: server, serverInfo: [sqlState: 25P02, file: postgres.c, line: 1498, message: current transaction is aborted, commands ignored until end of transaction block,…
My expectation is, that the transaction should still be available as long as I catch an error. Am I wrong?
Thanks for hints
Lars