Skip to content

[Postgres] Update styles to match new layer name when layer is renamed #66391

Open
JanCaha wants to merge 8 commits into
qgis:masterfrom
JanCaha:fix-65360
Open

[Postgres] Update styles to match new layer name when layer is renamed #66391
JanCaha wants to merge 8 commits into
qgis:masterfrom
JanCaha:fix-65360

Conversation

@JanCaha

@JanCaha JanCaha commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Description

When PostgreSQL table is renamed (either from browser or from provider connection) the styles associated withe layer need to updated in layer_styles table to match the new layer name.

Funded by: Ocean Winds

Fixes #65360

AI tool usage

  • AI tool(s) (Copilot, Claude, or something similar) supported my development of this PR.

@github-actions github-actions Bot added this to the 4.2.0 milestone Jun 8, 2026
@JanCaha JanCaha changed the title [Postgres] [Postgres] Update styles to match new layer name when layer is renamed Jun 8, 2026
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.

@kewgis

kewgis commented Jun 8, 2026

Copy link
Copy Markdown

Does this also work if a layer is moved from one schema to another?

@JanCaha

JanCaha commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

@kewgis Actually no, but that would be another bug. It is in different part of the code...but I can take care of it.

@JanCaha

JanCaha commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

@elpaso the fix for style being pointed to the correct layer after moving to another schema was so small that i decided to include it here. Could you please reapprove this? Thank you

@elpaso elpaso left a comment

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.

LGTM, there is only one thing you should probably check: executeSql (and private) can throw QgsProviderConnectionException, can you please check that in the unlikely event it happens the whole QGIS doesn't crash with an unhandled exception but the exception is catched somewhere up in the call stack?

Comment thread src/providers/postgres/qgspostgresdataitemguiprovider.cpp Outdated
@JanCaha

JanCaha commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

Fixed!

Comment thread src/providers/postgres/qgspostgresproviderconnection.cpp Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Renaming or moving a layer stored in postgres breaks any associated styles it may have

4 participants