Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better auto increment asset-tags [FD-32892] #13301

Merged
merged 2 commits into from
Jul 13, 2023

Conversation

uberbrady
Copy link
Collaborator

Before, it was possible to 'mess up' the auto-incrementing asset tags by bumping the suggested asset tag up by one - then, forever after, each 'suggested' new asset tag would conflict with one already in the system, until you finally go into the asset tag settings and manually bump up the "Next auto-increment" setting by one.

Now, we try to be intelligent and set the next auto-increment value to the currently-just-created asset's asset_tag, plus one, IF AND ONLY IF the currently created asset has a higher value than the next.

Additionally, I've added tests to confirm that various failure scenarios in asset-tag generation no longer happen.

I unfortunately fixed the problem before I added the tests, but I just back-ported the tests only to the existing code-base, and I ran them, and got this lovely output (proving that the tests do break on our current code, but are fixed on my new code. Which is want you want :) )

FAIL Tests\Unit\AssetTest
✓ auto increment
✓ auto increment collision
⨯ auto increment double
⨯ auto increment gap and backfill
⨯ prefixless autoincrement backfill
⨯ unzerofilled prefixless autoincrement backfill
✓ warranty expires attribute

@what-the-diff
Copy link

what-the-diff bot commented Jul 12, 2023

PR Summary

  • Improved Code Clarity in AssetsController
    In the AssetsController.php file, a conditional comment was added. This provides better understanding of why certain code is necessary and beneficial, thereby making it easier to interpret and maintain.

  • Enhanced Autoincrement Function in Asset Method
    The autoincrement_asset method in the Asset.php file now accepts additional increments, allowing for greater flexibility in asset growth management.

  • Updated Asset Creation Process
    Within the AssetObserver.php file, the created method has been adjusted to automatically increment asset tags dependent on specific conditions. This helps streamline the asset creation process and maintain a strict and organised asset hierarchy.

  • Added Option for Autoincrement during Testing
    A new method, enableAutoIncrement, has been introduced to the Settings.php file. It serves to enable testing of the auto-increment mechanism of assets, ensuring its correct functionality under test scenarios.

  • Expanded Test Coverage with Asset Scenarios
    Multiple new test scenarios have been introduced to the AssetTest.php file. These tests ensure the accurate functionality of the auto-increment feature under diverse conditions, confirming robustness and reliability of the feature.

@snipe
Copy link
Owner

snipe commented Jul 12, 2023

This looks fantastic, @uberbrady - I need to test a bit more, but so far it looks excellent.

Copy link
Collaborator

@marcusmoore marcusmoore left a comment

Choose a reason for hiding this comment

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

Just a couple small comments but 🎉 tests! 😄


public function testPrefixlessAutoincrementBackfill()
{
// TODO: COPYPASTA FROM above, is there a way to still run this test but not have it be so duplicative?
Copy link
Collaborator

Choose a reason for hiding this comment

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

One option for reducing duplication is PHPUnit's Data Providers since only the first line of these tests are different.

Here is an example in our codebase and a really good resource on getting started with Data Providers and a gotcha to look out for (see the Caveat section).

In this case though...I would just keep the duplication.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, agreed. I don't like that data provider thing at all, it gives me all of the Oogs. Every single one of them. I think just accepting the repetition is the move.

}

$logAction = new Actionlog();
$logAction->item_type = Asset::class;
$logAction->item_type = Asset::class; // can we instead say $logAction->item = $asset ?
Copy link
Collaborator

Choose a reason for hiding this comment

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

I just tried it and it works 👍🏾 up to you if you want to include it in this chunk of work or not.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it ought to go in a different PR entirely, tbh. It's just a comment that I wanted recorded for posterity, so that we hopefully fix it at some point later.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree 😄


public function testUnzerofilledPrefixlessAutoincrementBackfill()
{
// TODO: COPYPASTA FROM above (AGAIN), is there a way to still run this test but not have it be so duplicative?
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can remove the TODOs in this file so they don't end up confusing anyone in the future 👍🏾

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If we decide that this is the right way (and it's seeming like we are deciding that), then I will leave the comment in just to keep people aware that changes to one spot might need to be copied to the others - but I would pull the TODO tag so we don't think that we need to actually do anything about it.

Comment on lines +548 to +549
$asset->asset_tag = $request->get('asset_tag', Asset::autoincrement_asset()); //yup, problem :/
// NO IT IS NOT!!! This is never firing; we SHOW the asset_tag you're going to get, so it *will* be filled in!
Copy link
Collaborator

Choose a reason for hiding this comment

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

I got confused by these comments. If I'm reading this right we can remove both comments right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was hoping to record, for posterity, how, exactly this system is actually working, rather than "how we think it might work" so that the next time someone has to look at this problem, they don't go through the same mental gymnastics realizing how it really works versus how we think it might want to work.

Very happy to workshop a better way of conveying this to future-us (who are going to look at this again in, like, a few months or years or something...)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah gotcha. I was thinking these were debug comments for yourself as you worked through this PR. Keeping them there works for me 👍🏾

@snipe snipe merged commit 981c3a4 into snipe:develop Jul 13, 2023
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.

3 participants