Skip to content

fix(db): skip orphaned sequences in resynchronizeDatabaseSequences - #58762

Open
boris324 wants to merge 1 commit into
nextcloud:masterfrom
boris324:fix/pgsql-orphaned-sequence-resync
Open

fix(db): skip orphaned sequences in resynchronizeDatabaseSequences#58762
boris324 wants to merge 1 commit into
nextcloud:masterfrom
boris324:fix/pgsql-orphaned-sequence-resync

Conversation

@boris324

@boris324 boris324 commented Mar 6, 2026

Copy link
Copy Markdown

Fixes #58715

Orphaned PostgreSQL sequences (left behind after dropAutoincrementColumn in migrations like Version33000Date20251023110529) crash occ db:convert-type with:

SQLSTATE[42601]: Syntax error: ... SELECT setval('oc_preview_locations_id_seq', (SELECT MAX() FROM ))

The information_schema.columns lookup returns no row for these sequences, and the code accessed fields on the false result.

Changes:

  • Skip sequences where no column references them
  • Handle empty tables where MAX() returns NULL
  • Use schema-qualified table names for the MAX query
  • Added unit tests

When converting a database to PostgreSQL via `occ db:convert-type`,
the sequence resynchronization crashes on orphaned sequences that
no longer have an associated column (e.g. after autoincrement was
removed via dropAutoincrementColumn in a migration).

The query against information_schema.columns returns no row for
these sequences, and the code tried to access fields on a `false`
result, producing malformed SQL like:
  SELECT setval('...', (SELECT MAX() FROM ))

Skip sequences with no matching column. Also handle empty tables
where MAX returns NULL, and use schema-qualified table names.

Fixes nextcloud#58715
@github-actions

Copy link
Copy Markdown
Contributor

Hello there,
Thank you so much for taking the time and effort to create a pull request to our Nextcloud project.

We hope that the review process is going smooth and is helpful for you. We want to ensure your pull request is reviewed to your satisfaction. If you have a moment, our community management team would very much appreciate your feedback on your experience with this PR review process.

Your feedback is valuable to us as we continuously strive to improve our community developer experience. Please take a moment to complete our short survey by clicking on the following link: https://cloud.nextcloud.com/apps/forms/s/i9Ago4EQRZ7TWxjfmeEpPkf6

Thank you for contributing to Nextcloud and we hope to hear from you soon!

(If you believe you should not receive this message, you can add yourself to the blocklist.)

@findlayfeng

Copy link
Copy Markdown

Is this PR finished?

@slashrsm

Copy link
Copy Markdown

I can confirm that this fixed the problem for me and allowed me to complete the MariaDB to Postrgres coversion.

@gusdleon

gusdleon commented Jun 4, 2026

Copy link
Copy Markdown

I can confirm this patch solves issues with:

In ExceptionConverter.php line 62:
  An exception occurred while executing a query: SQLSTATE[42601]: Syntax error: 7 ERROR:  syntax error at or near ")"  
  LINE 1: SELECT setval('oc_jobs_id_seq', (SELECT MAX() FROM ))                                                        
                                                             ^                                                         
In Exception.php line 24:
  SQLSTATE[42601]: Syntax error: 7 ERROR:  syntax error at or near ")"  
  LINE 1: SELECT setval('oc_jobs_id_seq', (SELECT MAX() FROM ))         
                                                             ^          
In Connection.php line 71:
  SQLSTATE[42601]: Syntax error: 7 ERROR:  syntax error at or near ")"  
  LINE 1: SELECT setval('oc_jobs_id_seq', (SELECT MAX() FROM ))         
                                                             ^          
db:convert-type [--port PORT] [--password PASSWORD] [--clear-schema] [--all-apps] [--chunk-size CHUNK-SIZE] [--] <type> <username> <hostname> <database>

In a DB conversion from MariaDB to Postrgres

Commands used:

apk add --no-cache patch curl

then

curl -L https://github.com/nextcloud/server/pull/58762.patch | patch -p1 -d /app/www/public/

on a custom install of docker linuxserver/nextcloud

@susnux susnux added the community pull requests from community label Jun 9, 2026
@susnux susnux added this to the Nextcloud 35 milestone Jun 9, 2026
@kale1d0code

Copy link
Copy Markdown

Thanks for the fix!

@nextcloud-bot nextcloud-bot mentioned this pull request Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community pull requests from community feedback-requested

Projects

None yet

7 participants