Bugfix constraint migration fix by objecttothis · Pull Request #4230 · opensourcepos/opensourcepos · GitHub
Skip to content

Bugfix constraint migration fix#4230

Merged
objecttothis merged 11 commits into
masterfrom
bugfix-constraint-migration-fix
May 29, 2025
Merged

Bugfix constraint migration fix#4230
objecttothis merged 11 commits into
masterfrom
bugfix-constraint-migration-fix

Conversation

@objecttothis

@objecttothis objecttothis commented Apr 21, 2025

Copy link
Copy Markdown
Member
  • Migrate Sessions table to prevent problems with migrating populated databases.
  • Refactor a few function names for PSR-12 compliance.
  • Fix bug in migration causing failure due to trying to drop indexes with associated foreign key constraints. This bug was causing 3.3.2 databases to fail migration. [Bug]: Migration fails adding foreign key constraint #4202
  • migration to change constraint back to ON DELETE RESTRICT
    20250213000000_fix_attributes_cascading_delete.php reverted a change on the ospos_attribute_links_ibfk_1 and ospos_attribute_links_ibfk_2 constraints on the ospos_attribute_links table to fix a bug with not being able to delete a dropdown value. The problem is that these need to be ON DELETE RESTRICT in order to use generated_unique_column and attribute_links_uq3 to enforce no duplication of attribute links. The solution to this (in this PR) is to change them back to ON DELETE RESTRICT and programmatically cascade delete before trying to remove an entry. This way we can enforce uniqueness AND deleting dropdown rows.
  • Add indices to ospos_inventory and ospos_attribute_links which were causing the items view queries to time out when attribute_links was large and the hardware wasn't fast.

closing #4237
closing #4251

@objecttothis objecttothis self-assigned this Apr 21, 2025
@objecttothis objecttothis linked an issue Apr 25, 2025 that may be closed by this pull request
1 task
Comment thread app/Database/Migrations/20240630000001_fix_keys_for_db_upgrade.php Outdated
Comment thread app/Database/Migrations/20240630000001_fix_keys_for_db_upgrade.php Outdated
@jekkos jekkos added this to the 3.4.1 milestone Apr 29, 2025
@objecttothis objecttothis requested a review from jekkos May 1, 2025 13:04
@objecttothis objecttothis marked this pull request as ready for review May 1, 2025 13:04
Comment thread app/Controllers/Attributes.php
Comment thread app/Database/Migrations/20210422000001_remove_duplicate_links.php
@objecttothis objecttothis marked this pull request as draft May 1, 2025 13:20
Comment thread app/Controllers/Attributes.php
Comment thread .editorconfig
Comment thread app/Views/attributes/form.php Outdated
- Function to delete attribute links with one or more attribute definitions.
- Call function to implement an effective cascading delete.
- Refactor function naming to meet PSR-12 conventions

Signed-off-by: objecttothis <objecttothis@gmail.com>
- Add drop of Generated Column to prevent failure of migration on MySQL databases.

Signed-off-by: objecttothis <objecttothis@gmail.com>
- Removed blank lines
- Refactored function naming for PSR compliance
- Reformatted code for PSR compliance
- Added logic to drop dependent foreign key constraints before deleting an index then recreating them.

Signed-off-by: objecttothis <objecttothis@gmail.com>
- DROP and CREATE session table to prevent migration problems on populated databases

Signed-off-by: objecttothis <objecttothis@gmail.com>
- In the event that item_id = null (e.g., it's a dropdown) it should not be included in the results.

Signed-off-by: objecttothis <objecttothis@gmail.com>
- Removed delete_value function in Attributes Controller as it is unused.
- Renamed postDelete_attribute_value function for PSR-12 compliance.
- Renamed delete_value Attribute model function for PSR-12 compliance.
- Refactored out function to getAttributeIdByValue
- Replaced == with === to prevent type juggling
- Reorganized parts of model to make it easier to find CRUD functions.

Signed-off-by: objecttothis <objecttothis@gmail.com>
- PSR-12 Compliance formatting changes
- Refactored several generic functions into the migration_helper.php
- First check if primary key exists before attempting to create it.
- Grouped functions together in migration_helper.php
- phpdoc commenting functions

Signed-off-by: objecttothis <objecttothis@gmail.com>
- There are two queries run while opening the Items view which time out on large databases with weak hardware. These indices cut the query execution in half or better.

Signed-off-by: objecttothis <objecttothis@gmail.com>
- This migration reverts ospos_attribute_links_ibfk_1 and 2 to ON DELETE RESTRICT. Cascade delete is done programmatically. This is needed to have a unique column on the attribute_links table which prevents duplicate attributes from begin created with the same item_id-attribute_id-definition_id combination

Signed-off-by: objecttothis <objecttothis@gmail.com>
@objecttothis objecttothis force-pushed the bugfix-constraint-migration-fix branch from 9c1dbce to b24dee4 Compare May 22, 2025 10:45
@objecttothis objecttothis marked this pull request as ready for review May 22, 2025 11:21
@objecttothis objecttothis requested review from BudsieBuds and jekkos May 22, 2025 11:21
Signed-off-by: objecttothis <objecttothis@gmail.com>
Comment thread app/Models/Attribute.php Outdated
Comment thread app/Models/Attribute.php Outdated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This heading would be for private Model functions.

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'd try to code to structure things logically.. having to add comments might be a code smell (according to clean code guidelines). It might just indicate naming is not good or classes are not sticking to single resonsibility principle.

@objecttothis

Copy link
Copy Markdown
Member Author

@jekkos, I think the commit history is a little rough after rebasing on the master. When I merge, per usual I'm planning to do a squash and merge with a commit message of this to clean everything up:

- Refactored function names for PSR-12 compliance
- Programmatically cascade delete attribute_link rows when a dropdown attribute is deleted but leave attribute_link rows associated with transactions.
- Added `WHERE item_id IS NOT NULL` to migration to prevent failure on MySQL databases during migration
- Retroactive correction of migration to prevent MySQL databases from failing.
- Refactored generic functions to helper
- Reverted attribute_links foreign key to ON DELETE RESTRICT which is required for a unique constraint on this table. Cascading deletes are now handled programmatically.
- Migration Session table to match Code Igniter 4.6
- Add index to attribute_links to prevent query timeout in items view on large databases
- Added overridePrefix() function to the migration_helper. Any time QueryBuilder is adding a prefix to the query when we don't want it to, this query can be used to override the prefix then set it back after you're done.
- Added dropAllForeignKeyConstraints() helper function.
- Added deleteIndex() helper function.
- Added indexExists() helper function.
- Added primaryKeyExists() helper function.
- Added recreateForeignKeyConstraints() helper function.
- Added CRUD section headings to the Attribute model.
- Replaced `==` with `===` to prevent type juggling.
- Removed unused delete_value function.
- Reworked deleteDefinition() and deleteDefinitionList() functions to delete rows from the attribute_links table which are associated.
- Added deleteAttributeLinksByDefinitionId() function

This PR is now ready for review.

@objecttothis

Copy link
Copy Markdown
Member Author

@jekkos the PHPLinter is failing on PHP 8.4 deprecations (https://github.com/opensourcepos/opensourcepos/actions/runs/15184079188/job/42700305258). I do think we need to make these changes to remove deprecated code, but that needs to be a separate PR from this one.

@jekkos

jekkos commented May 27, 2025

Copy link
Copy Markdown
Member

We can keep the full history in the squashed commit message body. So typically a good commit message has a short first line describing what is is about, and then more details can be added after the first newline.

I find it very useful to add info to the history this way, it's a bit of an ongoing documentation with every change that is done.

Comment thread app/Models/Attribute.php Outdated
jekkos
jekkos previously approved these changes May 28, 2025

@jekkos jekkos left a comment

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 going to block the PR here at this point, I know these things take time to test. But I do have some minor remarks on the usage of comments and eventual removal of a duplicated log line. Beside that I think it looks good.

Comment thread app/Models/Attribute.php
Comment thread app/Models/Attribute.php Outdated
return $builder->get()->getResultArray();
}

// ==============

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'd try to code to structure things logically.. having to add comments might be a code smell (according to clean code guidelines). It might just indicate naming is not good or classes are not sticking to single resonsibility principle.

Comment thread app/Views/attributes/form.php
Comment thread app/Database/Migrations/20250522000000_AttributeLinksUniqueConstraint.php Outdated
@objecttothis

Copy link
Copy Markdown
Member Author

@jekkos in the morning I'll remove the comments and extra log lines. I think I was just mimicking what was in other migrations.

I'm OK with just ordering class functions logically, but I think we should agree on an order and move functions to those locations as we work on the code. Doing it all here would make the commit pretty difficult to parse.

I'll squash and merge after pushing those commits.

@objecttothis

Copy link
Copy Markdown
Member Author

- Removed Comments separating sections of code in Attribute model
- Removed extra log line to prevent cluttering of the log

Signed-off-by: objecttothis <objecttothis@gmail.com>
@objecttothis objecttothis merged commit e1fedab into master May 29, 2025
3 checks passed
@objecttothis objecttothis deleted the bugfix-constraint-migration-fix branch May 29, 2025 11:24
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]: Migration fails adding foreign key constraint

3 participants