Skip to content

Commit 1a515c9

Browse files
authored
Merge pull request #6717 from pblottiere/remove_qgd
Do not save .qgd file alongside .qgs when it's not used
2 parents 5014d95 + ff1bc0b commit 1a515c9

File tree

5 files changed

+71
-2
lines changed

5 files changed

+71
-2
lines changed

python/core/qgsauxiliarystorage.sip.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,15 @@ Duplicates a table and its content.
345345
static QString extension();
346346
%Docstring
347347
Returns the extension used for auxiliary databases.
348+
%End
349+
350+
static bool exists( const QgsProject &project );
351+
%Docstring
352+
Returns true if the auxiliary database yet exists for a project, false otherwise.
353+
354+
:param project: The project for which the database is checked
355+
356+
.. versionadded:: 3.2
348357
%End
349358

350359
};

src/core/qgsauxiliarystorage.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,12 @@ QString QgsAuxiliaryStorage::extension()
647647
return AS_EXTENSION;
648648
}
649649

650+
bool QgsAuxiliaryStorage::exists( const QgsProject &project )
651+
{
652+
const QFileInfo fileinfo( filenameForProject( project ) );
653+
return fileinfo.exists() && fileinfo.isFile();
654+
}
655+
650656
bool QgsAuxiliaryStorage::exec( const QString &sql, sqlite3 *handler )
651657
{
652658
bool rc = false;

src/core/qgsauxiliarystorage.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,15 @@ class CORE_EXPORT QgsAuxiliaryStorage
377377
*/
378378
static QString extension();
379379

380+
/**
381+
* Returns true if the auxiliary database yet exists for a project, false otherwise.
382+
*
383+
* \param project The project for which the database is checked
384+
*
385+
* \since QGIS 3.2
386+
*/
387+
static bool exists( const QgsProject &project );
388+
380389
private:
381390
spatialite_database_unique_ptr open( const QString &filename = QString() );
382391
spatialite_database_unique_ptr open( const QgsProject &project );

src/core/qgsproject.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2505,6 +2505,7 @@ void QgsProject::setTrustLayerMetadata( bool trust )
25052505
bool QgsProject::saveAuxiliaryStorage( const QString &filename )
25062506
{
25072507
const QMap<QString, QgsMapLayer *> layers = mapLayers();
2508+
bool empty = true;
25082509
for ( auto it = layers.constBegin(); it != layers.constEnd(); ++it )
25092510
{
25102511
if ( it.value()->type() != QgsMapLayer::VectorLayer )
@@ -2514,10 +2515,15 @@ bool QgsProject::saveAuxiliaryStorage( const QString &filename )
25142515
if ( vl && vl->auxiliaryLayer() )
25152516
{
25162517
vl->auxiliaryLayer()->save();
2518+
empty &= vl->auxiliaryLayer()->auxiliaryFields().isEmpty();
25172519
}
25182520
}
25192521

2520-
if ( !filename.isEmpty() )
2522+
if ( !mAuxiliaryStorage->exists( *this ) && filename.isEmpty() && empty )
2523+
{
2524+
return true; // it's not an error
2525+
}
2526+
else if ( !filename.isEmpty() )
25212527
{
25222528
return mAuxiliaryStorage->saveAs( filename );
25232529
}

tests/src/python/test_qgsauxiliarystorage.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def tmpPath():
4040
f.close()
4141
os.remove(f.fileName())
4242

43-
return f.fileName()
43+
return f.fileName().replace('.', '_')
4444

4545

4646
def createLayer():
@@ -405,6 +405,45 @@ def testCreateProperty(self):
405405
afIndex = vl.fields().indexOf(afName)
406406
self.assertEqual(index, afIndex)
407407

408+
def testQgdCreation(self):
409+
# New project
410+
p = QgsProject()
411+
self.assertTrue(p.auxiliaryStorage().isValid())
412+
413+
# Save the project
414+
path = tmpPath()
415+
qgs = path + '.qgs'
416+
self.assertTrue(p.write(qgs))
417+
self.assertTrue(os.path.exists(qgs))
418+
419+
# Auxiliary storage is empty so .qgd file should not be saved
420+
qgd = path + '.qgd'
421+
self.assertFalse(os.path.exists(qgd))
422+
423+
# Add a vector layer and an auxiliary layer in the project
424+
vl = createLayer()
425+
self.assertTrue(vl.isValid())
426+
p.addMapLayers([vl])
427+
428+
pkf = vl.fields().field(vl.fields().indexOf('pk'))
429+
al = p.auxiliaryStorage().createAuxiliaryLayer(pkf, vl)
430+
self.assertTrue(al.isValid())
431+
vl.setAuxiliaryLayer(al)
432+
433+
# Add an auxiliary field to have a non empty auxiliary storage
434+
pdef = QgsPropertyDefinition('propname', QgsPropertyDefinition.DataTypeNumeric, '', '', 'ut')
435+
self.assertTrue(al.addAuxiliaryField(pdef))
436+
437+
# Save the project
438+
newpath = tmpPath()
439+
qgs = newpath + '.qgs'
440+
self.assertTrue(p.write(qgs))
441+
self.assertTrue(os.path.exists(qgs))
442+
443+
# Auxiliary storage is NOT empty so .qgd file should be saved now
444+
qgd = newpath + '.qgd'
445+
self.assertTrue(os.path.exists(qgd))
446+
408447

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

0 commit comments

Comments
 (0)