Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
The following PR was raised in order to resolve the exercises assigned and outlined in this document.
Exercise 1
The new attachment implementation was created by first running the command
php artisan make:model Attachment -m
creating both model and migration. Then i proceeded to create the relationships in the respective models (Post
,Comment
andAttachment
).The feature test was performed to test the new console command, ensuring the data. In the logic of the command, a progress bar was used to have a visual guide of how the migration proceeds, in addition, the data array was segmented so that there were no database errors due to the number of records that are being migrated.
Total data in
attachments
is 9150 records after the migration with a total of 92 inserts of 100 records each; in this way errors are avoided if a large number of records are migrated.In this image you can see that there are a number of 9152 assertions of which 9150 correspond to the number of records that should be migrated, the test is heavier than it should be but I wanted to make sure that all the data was inserted correctly in the
attachments
table.Steps to follow:
1. run
php artisan migrate:fresh --seed
2. run
php artisan migrate_attachments_data
Exercise 2
For this exercise, the feature test was created first, following the TDD approach; In this way, I proceeded to change in the index view the relationships that called the old models (
post_attachments
,comment_attachments
) and replaced them with the new relationship withattachments
that was implemented in exercise 1; and updated theIndexViewTest
again to reflect this change.The next thing that was done to finish the exercise was to move the logic of the route to a controller (
HomeController
), optimize the query usingEager Loading
, adding a new relationship inPost
model (hasManyThrough
) to access theattachments
table more directly , at the same time we save database queries, load fewer models and minimize response time as much as possible on the backend side. Finally we change the way the data is printed in the index view to reflect the changes from the backend.