Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[queue_ltr_backport] [OGR provider] Issue syncToDisk() after [add/delete/rename]Attributes() #58769

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/core/providers/ogr/qgsogrprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2035,6 +2035,15 @@ bool QgsOgrProvider::addAttributes( const QList<QgsField> &attributes )
}
}

// We at least want to syncToDisc() for OpenFileGDB, because its AddField
// implementation doesn't update immediately system tables.
// We exclude GeoJSON because leaveUpdateMode() has specific behavior for it.
if ( mGDALDriverName != QLatin1String( "GeoJSON" ) && !syncToDisc() )

{
returnvalue = false;
}

// Backup existing fields. We need them to 'restore' field type, length, precision
QgsFields oldFields = mAttributeFields;

Expand Down Expand Up @@ -2115,6 +2124,15 @@ bool QgsOgrProvider::deleteAttributes( const QgsAttributeIds &attributes )
res = false;
}
}

// We at least want to syncToDisc() for OpenFileGDB, because its DeleteField
// implementation doesn't update immediately system tables.
// We exclude GeoJSON because leaveUpdateMode() has specific behavior for it.
if ( mGDALDriverName != QLatin1String( "GeoJSON" ) && !syncToDisc() )
{
res = false;
}

loadFields();

if ( mTransaction )
Expand Down Expand Up @@ -2169,6 +2187,15 @@ bool QgsOgrProvider::renameAttributes( const QgsFieldNameMap &renamedAttributes
result = false;
}
}

// We at least want to syncToDisc() for OpenFileGDB, because its AlterFieldDefn
// implementation doesn't update immediately system tables.
// We exclude GeoJSON because leaveUpdateMode() has specific behavior for it.
if ( mGDALDriverName != QLatin1String( "GeoJSON" ) && !syncToDisc() )
{
result = false;
}

loadFields();

if ( mTransaction )
Expand Down
33 changes: 33 additions & 0 deletions tests/src/python/test_provider_ogr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3432,6 +3432,39 @@ def test_exporter_capabilities(self):
self.assertFalse(
exporter.attributeEditCapabilities() & Qgis.VectorDataProviderAttributeEditCapability.EditComment)

@unittest.skipIf(int(gdal.VersionInfo('VERSION_NUM')) < GDAL_COMPUTE_VERSION(3, 6, 0), "GDAL 3.6 required")
@unittest.skipIf(gdal.GetDriverByName("OpenFileGDB") is None, "GDAL OpenFileGDB driver required")
def testDeleteFieldFileGeodatabase(self):

with tempfile.TemporaryDirectory() as temp_dir:
dest_file_name = os.path.join(temp_dir, 'testDeleteFieldFileGeodatabase.gdb')
ds = ogr.GetDriverByName("OpenFileGDB").CreateDataSource(dest_file_name)
lyr = ds.CreateLayer("test", geom_type=ogr.wkbNone)
lyr.CreateField(ogr.FieldDefn("fld1"))
lyr.CreateField(ogr.FieldDefn("fld2"))
f = ogr.Feature(lyr.GetLayerDefn())
f["fld1"] = "a"
f["fld2"] = "b"
lyr.CreateFeature(f)
f = ogr.Feature(lyr.GetLayerDefn())
f["fld1"] = "c"
f["fld2"] = "d"
lyr.CreateFeature(f)
ds = None

vl = QgsVectorLayer(dest_file_name, 'vl')
self.assertTrue(vl.startEditing())
self.assertTrue(vl.deleteAttribute(1)) # delete field fld1
self.assertTrue(vl.commitChanges())

# Re-open a connection without explicitly closing the one we've edited
vl2 = QgsVectorLayer(dest_file_name, 'vl2')
features = {f.id(): f.attributes() for f in vl2.getFeatures()}
self.assertEqual(features, {
1: [1, 'b'],
2: [2, 'd']
})


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