Skip to content

Add sorting function for tables by foreign key dependencies - #64642

Open
depuits wants to merge 5 commits into
nextcloud:masterfrom
depuits:patch-1
Open

depuits wants to merge 5 commits into
nextcloud:masterfrom
depuits:patch-1

Conversation

@depuits

@depuits depuits commented Sep 22, 2026

Copy link
Copy Markdown

Summary

Sort the table order based on foreign keys before starting the copy of tables. The revers logic is also applied when dropping the tables when using --clear-schema.

TODO

  • Tests

Checklist

AI (if applicable)

  • The content of this PR was partly or fully generated using AI

Ai helped writing the sortTablesByForeignKeys method body.

Signed-off-by: Joeri Colman <colmanjoeri@msn.com>
Comment thread core/Command/Db/ConvertType.php Outdated
Comment on lines +412 to +413
* The dependency information is obtained from the target database,
* making this independent of the source/target database vendor.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess this works because the schema is already applied to the target DB?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Correct, this code runs after the createSchema or when running in the clearSchema. Only problem could be if you use the clearSchema option if there never was a migration database setup before, but in that case you should just not add that option.

Comment thread core/Command/Db/ConvertType.php Outdated
*
* @param Connection $connection Target database connection
* @param array<string> $tables Tables to sort
* @param bool $reverse Reverse the sorted tables for dropping tables

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IMO this parameter name should be more expressive, because it's not clear what the "default" order is supposed to be.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

renamed the parameter to dependenciesFirst.

Comment thread core/Command/Db/ConvertType.php Outdated

foreach ($tables as $table) {
foreach ($schemaManager->listTableForeignKeys($table) as $foreignKey) {
$parent = $foreignKey->getForeignTableName();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

parent is not really the correct term, just call it foreignTable.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Renamed as suggested.

Comment on lines +435 to +438
// Ignore references to tables which aren't being converted.
if (!isset($tableSet[$parent])) {
continue;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should never happen, because then something is seriously wrong.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Even thought other things might be seriously wrong isn't it best to catch this in this code?

Comment on lines +440 to +441
// Ignore self-references. They don't impose an ordering
// requirement on the table itself.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure how we copy tables, but could this mean that rows also need to be copied in the correct order by dependency?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

In the case that a table self-references itself yes. But that seemed out of scope for this fix. In my case I didn't have self-referencing tables and I'm not sure if there are apps using those.

Comment thread core/Command/Db/ConvertType.php Outdated
*
* Tables without dependencies can be copied immediately.
*/
$ready = [];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

While reading the code I was expecting this variable to already hold the final table order, so please find a different more descriptive name for it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Renamed to readyTables

Comment thread core/Command/Db/ConvertType.php Outdated
Comment thread core/Command/Db/ConvertType.php Outdated
* the conversion, but the failure will accurately expose the
* cyclic dependency rather than being hidden by this sorter.
*/
if (count($result) !== count($tables)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder if this should even throw an exception to abort the entire process.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This ties in with copying rows in a correct order. For now these tables are just added to the list and if there happens to be no problem in the order of the rows then the convert will just succeed. In the other case an exception will be thrown while doing the copy off data as was now the case for any table with a foreign key and alphabetically incorrect order.

depuits and others added 4 commits September 22, 2026 09:51
Co-authored-by: Kate <26026535+provokateurin@users.noreply.github.com>
Signed-off-by: Joeri Colman <colmanjoeri@msn.com>
Signed-off-by: Joeri Colman <colmanjoeri@msn.com>
Signed-off-by: Joeri Colman <colmanjoeri@msn.com>
Co-authored-by: Kate <26026535+provokateurin@users.noreply.github.com>
Signed-off-by: Joeri Colman <colmanjoeri@msn.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: DB conversion occ db:convert-type fails with "foreign key constraint"

2 participants