Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions src/providers/postgres/qgspostgresdataitemguiprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,15 @@ void QgsPostgresDataItemGuiProvider::renameLayer( QgsPGLayerItem *layerItem, Qgs

notify( tr( "Rename %1" ).arg( typeName ), tr( "%1 '%2' renamed correctly to '%3'." ).arg( typeName, oldName, newName ), context, Qgis::MessageLevel::Success );

const QString updateStylesSql = u"UPDATE public.layer_styles SET f_table_name=%1 WHERE f_table_schema=%2 AND f_table_name=%3"_s
.arg( QgsPostgresConn::quotedValue( dlg.name() ), QgsPostgresConn::quotedValue( schemaName ), QgsPostgresConn::quotedValue( tableName ) );

QgsPostgresResult stylesResult( conn->LoggedPQexec( "QgsPostgresDataItemGuiProvider", updateStylesSql ) );
if ( stylesResult.PQresultStatus() != PGRES_COMMAND_OK )
{
notify( tr( "Rename %1" ).arg( typeName ), tr( "Unable to update layer styles for '%1'.\n%2" ).arg( dlg.name(), stylesResult.PQresultErrorMessage() ), context, Qgis::MessageLevel::Warning );
}

conn->unref();

if ( layerItem->parent() )
Expand Down
2 changes: 2 additions & 0 deletions src/providers/postgres/qgspostgresproviderconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ void QgsPostgresProviderConnection::dropRasterTable( const QString &schema, cons
void QgsPostgresProviderConnection::renameTablePrivate( const QString &schema, const QString &name, const QString &newName ) const
{
executeSqlPrivate( u"ALTER TABLE %1.%2 RENAME TO %3"_s.arg( QgsPostgresConn::quotedIdentifier( schema ), QgsPostgresConn::quotedIdentifier( name ), QgsPostgresConn::quotedIdentifier( newName ) ) );
executeSqlPrivate( u"UPDATE public.layer_styles SET f_table_name=%1 WHERE f_table_schema=%2 AND f_table_name=%3"_s

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to check if the style exists (e.g. with QgsProviderMetadata::styleExists ) before calling executeSqlPrivate on a not-existing table.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to use QgsPostgresUtils::tableExists with connection acqusition, since the name of style is not known forQgsProviderMetadata::styleExists and i did not want to use QgsPostgresRasterProviderMetadata::listStyles as it seemed like an overkill.

.arg( QgsPostgresConn::quotedValue( newName ), QgsPostgresConn::quotedValue( schema ), QgsPostgresConn::quotedValue( name ) ) );
}

QList<QgsAbstractDatabaseProviderConnection::TableProperty> QgsPostgresProviderConnection::tablesPrivate( const QString &schema, const QString &table, const TableFlags &flags, QgsFeedback *feedback ) const
Expand Down
51 changes: 51 additions & 0 deletions tests/src/python/test_qgsproviderconnection_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,57 @@ def test_move_raster_with_overviews_to_schema(self):
tables,
)

def test_rename_table_updates_layer_styles(self):
"""Test that renaming a table also updates f_table_name in layer_styles."""
md = QgsProviderRegistry.instance().providerMetadata("postgres")
conn = md.createConnection(self.uri, {})

conn.executeSql(
"""
DROP TABLE IF EXISTS qgis_test.rename_style_test CASCADE;
CREATE TABLE qgis_test.rename_style_test (id SERIAL PRIMARY KEY, geom geometry(POINT,4326));
INSERT INTO qgis_test.rename_style_test (geom) VALUES (ST_GeomFromText('POINT(0 0)', 4326));
"""
)

vl = QgsVectorLayer(
conn.tableUri("qgis_test", "rename_style_test"),
"test_rename_with_style",
"postgres",
)
self.assertTrue(vl.isValid())
err = vl.saveStyleToDatabase("test_style", "", False, "")
self.assertFalse(err)

# Verify style was saved under the original name
rows = conn.executeSql(
"SELECT f_table_name FROM public.layer_styles "
"WHERE f_table_schema = 'qgis_test' AND f_table_name = 'rename_style_test'"
)
self.assertEqual(len(rows), 1)

conn.renameVectorTable(
"qgis_test", "rename_style_test", "rename_style_test_renamed"
)

# Style must now reference the new table name
rows = conn.executeSql(
"SELECT f_table_name FROM public.layer_styles "
"WHERE f_table_schema = 'qgis_test' AND f_table_name = 'rename_style_test_renamed'"
)
self.assertEqual(len(rows), 1)

# No stale row for the old name
rows = conn.executeSql(
"SELECT f_table_name FROM public.layer_styles "
"WHERE f_table_schema = 'qgis_test' AND f_table_name = 'rename_style_test'"
)
self.assertEqual(len(rows), 0)

conn.executeSql(
"DROP TABLE IF EXISTS qgis_test.rename_style_test_renamed CASCADE"
)


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