Skip to content

Commit

Permalink
Regenerate primary key when making layer permanent if layer property set
Browse files Browse the repository at this point in the history
Respect the OnConvertFormatRegeneratePrimaryKey layer property when
making a temporary layer permanent.

This ensures that processing outputs which require primary key
regeneration will correctly get new primary keys when they are
initially run with a memory layer output, and then later that
memory layer is made permanent

Fixes #58942
  • Loading branch information
nyalldawson authored and troopa81 committed Oct 7, 2024
1 parent 594b466 commit 3bc8791
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8328,6 +8328,10 @@ void QgisApp::makeMemoryLayerPermanent( QgsVectorLayer *layer )
source += QStringLiteral( "|layername=%1" ).arg( newLayerName );
vl->setDataSource( source, vl->name(), QStringLiteral( "ogr" ), options );
vl->triggerRepaint();
// we don't want this flag anymore -- the layer has changed format and from now on
// primary keys should be retained if it's exported to a new file
vl->removeCustomProperty( QStringLiteral( "OnConvertFormatRegeneratePrimaryKey" ) );

mLayerTreeView->refreshLayerSymbology( vl->id() );
this->visibleMessageBar()->pushMessage( tr( "Layer Saved" ),
tr( "Successfully saved scratch layer to <a href=\"%1\">%2</a>" ).arg( QUrl::fromLocalFile( newFilename ).toString(), QDir::toNativeSeparators( newFilename ) ),
Expand Down Expand Up @@ -8608,8 +8612,14 @@ QString QgisApp::saveAsVectorFileGeneral( QgsVectorLayer *vlayer, bool symbology
options.saveMetadata = dialog->persistMetadata();
options.layerMetadata = vlayer->metadata();

QgsFeatureSink::SinkFlags sinkFlags;
if ( vlayer->customProperty( QStringLiteral( "OnConvertFormatRegeneratePrimaryKey" ) ).toBool() )
{
sinkFlags.setFlag( QgsFeatureSink::RegeneratePrimaryKey, true );
}

bool addToCanvas = dialog->addToCanvas();
QgsVectorFileWriterTask *writerTask = new QgsVectorFileWriterTask( vlayer, vectorFilename, options );
QgsVectorFileWriterTask *writerTask = new QgsVectorFileWriterTask( vlayer, vectorFilename, options, sinkFlags );

// when writer is successful:
connect( writerTask, &QgsVectorFileWriterTask::completed, this, [onSuccess, addToCanvas, encoding, vectorFilename, format]( const QString & newFilename, const QString & newLayer )
Expand Down

0 comments on commit 3bc8791

Please sign in to comment.