Skip to content

Commit

Permalink
Fix PG error when failing to update geom & constrained field (#45266)
Browse files Browse the repository at this point in the history
The problem was that throwing an exception the prepared statement
was not deallocated.

Fix #45100
  • Loading branch information
elpaso committed Oct 6, 2021
1 parent 5573fa2 commit 77c831e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -3479,7 +3479,11 @@ bool QgsPostgresProvider::changeFeatures( const QgsChangedAttributesMap &attr_ma

result = conn->PQexecPrepared( QStringLiteral( "updatefeature" ), params );
if ( result.PQresultStatus() != PGRES_COMMAND_OK && result.PQresultStatus() != PGRES_TUPLES_OK )
{
conn->rollback();
conn->PQexecNR( QStringLiteral( "DEALLOCATE updatefeature" ) );
throw PGException( result );
}

conn->PQexecNR( QStringLiteral( "DEALLOCATE updatefeature" ) );
}
Expand Down
12 changes: 12 additions & 0 deletions tests/src/python/test_provider_postgres.py
Expand Up @@ -2065,6 +2065,18 @@ def testReadExtentOnTable(self):
vl2.readLayerXml(elem, QgsReadWriteContext())
self.assertEqual(vl2.extent(), originalExtent)

def testPreparedFailure(self):
"""Test error from issue GH #45100"""

layer = self.getEditableLayerWithCheckConstraint()
self.assertTrue(layer.startEditing())
old_value = layer.getFeature(1).attribute('i_will_fail_on_no_name')
layer.changeAttributeValue(1, 1, 'no name')
layer.changeGeometry(1, QgsGeometry.fromWkt('point(7 45)'))
self.assertFalse(layer.commitChanges())
layer.changeAttributeValue(1, 1, old_value)
self.assertTrue(layer.commitChanges())

def testDeterminePkey(self):
"""Test primary key auto-determination"""

Expand Down

0 comments on commit 77c831e

Please sign in to comment.