Skip to content

Commit

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

Fix #45100
  • Loading branch information
elpaso authored and github-actions[bot] committed Oct 6, 2021
1 parent e96f912 commit 33e3fde
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -3462,7 +3462,9 @@ bool QgsPostgresProvider::changeFeatures( const QgsChangedAttributesMap &attr_ma

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

conn->PQexecNR( QStringLiteral( "DEALLOCATE updatefeature" ) );
}
Expand Down Expand Up @@ -3494,6 +3496,7 @@ bool QgsPostgresProvider::changeFeatures( const QgsChangedAttributesMap &attr_ma
{
pushError( tr( "PostGIS error while changing attributes: %1" ).arg( e.errorMessage() ) );
conn->rollback();
conn->PQexecNR( QStringLiteral( "DEALLOCATE updatefeature" ) );
returnvalue = false;
}

Expand Down
12 changes: 12 additions & 0 deletions tests/src/python/test_provider_postgres.py
Expand Up @@ -3199,6 +3199,18 @@ def testPkeyIntArray(self):
self.assertTrue(feat.isValid())
self.assertEqual(feat["name"], "test")

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())


if __name__ == '__main__':
unittest.main()

0 comments on commit 33e3fde

Please sign in to comment.