Skip to content

Commit a624b45

Browse files
authored
[Postgres] When renaming QgsProject in PostgreSQL copy also comment (#64705)
* if comment column exist, copy value when renaming project * add renameProject() * update trigger to handle renames * simplify the renaming project command * if current project was renamed, apply it also in the session * fix precommit * remove unused variable
1 parent dbb73c4 commit a624b45

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

src/providers/postgres/qgspostgresdataitemguiprovider.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -935,29 +935,26 @@ void QgsPostgresDataItemGuiProvider::renameProject( QgsPGProjectItem *projectIte
935935
return;
936936
}
937937

938+
QgsProject *project = QgsProject::instance();
939+
938940
QgsNewNameDialog dlg( tr( "project “%1”" ).arg( projectItem->name() ), projectItem->name(), QStringList(), QgsPostgresUtils::projectNamesInSchema( conn, projectItem->schemaName() ) );
939941
dlg.setWindowTitle( tr( "Rename Project" ) );
940942
if ( dlg.exec() != QDialog::Accepted || dlg.name() == projectItem->name() )
941943
return;
942944

943-
const QString newUri = projectItem->uriWithNewName( dlg.name() );
944-
945-
// read the project, set title and new filename
946-
QgsProject project;
947-
project.read( projectItem->path() );
948-
project.setTitle( dlg.name() );
949-
project.setFileName( newUri );
945+
QgsPostgresProjectUri pgProjectUri;
946+
pgProjectUri.connInfo = conn->uri();
947+
pgProjectUri.schemaName = projectItem->schemaName();
948+
pgProjectUri.projectName = projectItem->name();
950949

951-
// write project to the database
952-
const bool success = project.write();
953-
if ( !success )
950+
// if project fileName is same as the selected project encoded uri, then update current project with new fileName
951+
if ( project->fileName() == QgsPostgresProjectStorage::encodeUri( pgProjectUri ) )
954952
{
955-
notify( tr( "Rename Project" ), tr( "Unable to rename project “%1” to “%2”" ).arg( projectItem->name(), dlg.name() ), context, Qgis::MessageLevel::Warning );
956-
conn->unref();
957-
return;
953+
pgProjectUri.projectName = dlg.name();
954+
project->setFileName( QgsPostgresProjectStorage::encodeUri( pgProjectUri ) );
958955
}
959956

960-
if ( !QgsPostgresUtils::deleteProjectFromSchema( conn, projectItem->name(), projectItem->schemaName() ) )
957+
if ( !QgsPostgresUtils::renameProject( conn, projectItem->schemaName(), projectItem->name(), dlg.name() ) )
961958
{
962959
notify( tr( "Rename Project" ), tr( "Unable to rename project “%1” to “%2”" ).arg( projectItem->name(), dlg.name() ), context, Qgis::MessageLevel::Warning );
963960
conn->unref();

src/providers/postgres/qgspostgresutils.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,9 @@ BEGIN
750750
WHERE name = OLD.NAME;
751751
752752
ELSIF TG_OP = 'UPDATE' THEN
753+
IF NEW.name IS DISTINCT FROM OLD.name THEN
754+
UPDATE %1.qgis_projects_versions SET name = NEW.name WHERE name = OLD.name;
755+
END IF;
753756
IF NEW.content IS DISTINCT FROM OLD.content THEN
754757
INSERT INTO %1.qgis_projects_versions ( name, metadata, content, comment, date_saved )
755758
VALUES (OLD.name, OLD.metadata, OLD.content, OLD.comment,
@@ -839,6 +842,22 @@ bool QgsPostgresUtils::moveProjectVersions( QgsPostgresConn *conn, const QString
839842
return true;
840843
}
841844

845+
bool QgsPostgresUtils::renameProject( QgsPostgresConn *conn, const QString &schemaName, const QString &oldProjectName, const QString &newProjectName )
846+
{
847+
const QString sql = u"UPDATE %1.qgis_projects SET name=%2 WHERE name=%3"_s
848+
.arg( QgsPostgresConn::quotedIdentifier( schemaName ) )
849+
.arg( QgsPostgresConn::quotedValue( newProjectName ) )
850+
.arg( QgsPostgresConn::quotedValue( oldProjectName ) );
851+
852+
QgsPostgresResult result( conn->PQexec( sql ) );
853+
if ( result.PQresultStatus() != PGRES_COMMAND_OK )
854+
{
855+
return false;
856+
}
857+
858+
return true;
859+
}
860+
842861
QStringList QgsPostgresUtils::projectNamesInSchema( QgsPostgresConn *conn, const QString &schema )
843862
{
844863
QStringList projects;

src/providers/postgres/qgspostgresutils.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ class QgsPostgresUtils
208208
*/
209209
static bool moveProjectVersions( QgsPostgresConn *conn, const QString &originalSchema, const QString &project, const QString &targetSchema );
210210

211+
/**
212+
* Renames a project in the specified schema.
213+
*
214+
* \since QGIS 4.0
215+
*/
216+
static bool renameProject( QgsPostgresConn *conn, const QString &schemaName, const QString &oldProjectName, const QString &newProjectName );
217+
211218
/**
212219
* List projects in the specified \a schema
213220
*

0 commit comments

Comments
 (0)