How to Safely Change DB Relations in Live Laravel Project?
Summary
TLDRIn this video, web developer Pavlos Corp demonstrates how to refactor a Laravel project to change a one-to-many relationship to a many-to-many relationship, using a blog example where articles can have multiple categories. The tutorial covers writing automated tests with PHPUnit, migrating data with a pivot table, updating models and views, and ensuring the application functions correctly post-migration. The video serves as a practical guide for developers looking to safely refactor database relationships in existing projects.
Takeaways
- 🔧 The video discusses a real-life scenario where an existing project's database relationship is changed from one-to-many to many-to-many.
- 👨💻 The presenter, Pavlos Corp, is a web developer with extensive experience in Laravel, offering insights on handling such database changes.
- 📝 The example project is a blog where each article can be associated with multiple categories, necessitating a pivot table for the many-to-many relationship.
- 🛠️ Before making changes, it's crucial to write automated tests to ensure that the system's functionality remains intact post-migration.
- 📚 Automated tests cover various features like viewing articles, editing, and filtering articles by categories, ensuring that the system behaves as expected.
- 🔄 The migration process involves creating a pivot table, migrating existing data, and then updating the models to reflect the new many-to-many relationship.
- 🧩 After changing the database schema, the application's views and controllers must be updated to accommodate the new relationship structure.
- 🔍 The video demonstrates how to use Laravel's Eloquent ORM to manage the many-to-many relationship and update the database schema.
- 📉 The presenter uses Sourcetree to visually compare the changes in the codebase, highlighting the modifications made to the controllers and views.
- 🌟 The final step is to run the automated tests again to confirm that all features work correctly after the database and code changes.
Q & A
What is the main challenge discussed in the video script?
-The main challenge discussed is changing a database relationship from one-to-many to many-to-many in an existing, live project, which involves modifying the code, testing thoroughly, and migrating data without introducing bugs.
Why is it necessary to write automated tests before making changes to the database relationship?
-Writing automated tests before making changes ensures that all features work as expected initially. This provides a baseline to compare against after the changes, helping to identify any issues that arise from the refactoring.
What is the first step recommended by Pavlos Corp for refactoring the database relationship?
-The first step recommended by Pavlos Corp is to write automated tests to ensure that all features are working correctly before making any changes to the database relationship.
How does the video script illustrate the change from one-to-many to many-to-many relationship?
-The script uses the example of a blog where each article initially belongs to one category, but the requirement changes to allow articles to belong to multiple categories.
What is the role of Laravel in the context of this video script?
-Laravel is the web development framework used in the example project to demonstrate how to change the database relationship and update the code accordingly.
Why is it important to migrate the data before updating the models in Laravel?
-Migrating the data before updating the models ensures that the existing data is correctly transformed to fit the new many-to-many relationship, which is essential for maintaining data integrity.
What is the significance of creating a pivot table in the context of changing the database relationship?
-Creating a pivot table is significant because it facilitates the many-to-many relationship by providing a junction point between the two related tables, allowing multiple entries for each relationship.
How does the video script address the need to update the user interface after changing the database relationship?
-The script addresses the need to update the user interface by demonstrating changes in views and controllers to reflect the new many-to-many relationship, such as using for-each loops to display multiple categories.
What is the purpose of the 'php artisan migrate' command in the context of the video script?
-The 'php artisan migrate' command is used to apply database changes, such as creating a pivot table and dropping the old category id column, which are necessary steps in transitioning to a many-to-many relationship.
How does the video script ensure that the refactoring process is successful?
-The script ensures the refactoring process is successful by running automated tests before and after the changes, making adjustments to the tests and code as needed, and verifying that all tests pass, indicating that all features are working as expected with the new database relationship.
Outlines
🛠️ Refactoring a Project from One-to-Many to Many-to-Many
The video script introduces a scenario where a project's database relationship needs to be refactored from one-to-many to many-to-many. The example given is a blog where each article is initially linked to only one category, but the client later requests that articles should be linked to multiple categories. The speaker, Pavlos Corp, a web developer with 15 years of experience, particularly in Laravel, guides viewers through the process of making this change step by step. He emphasizes the importance of testing before making changes, suggesting the creation of automated tests to ensure all features work as expected. The video promises to cover coding changes, data migration, and updating the application to handle the new many-to-many relationship.
📝 Writing Automated Tests for Database Relationship Changes
This paragraph delves into the specifics of writing automated tests to prepare for the database relationship change. It outlines the creation of feature tests to ensure that the system functions correctly before refactoring. The tests cover various scenarios, such as viewing articles with categories, editing articles, and filtering articles by category on the homepage. The script mentions the use of database factories and seeders to set up test environments, and it provides a detailed look at how to structure these tests in Laravel. The tests are run, and the results are checked to ensure all are passing, marking the first milestone of the project as successful.
🔄 Migrating Data and Updating Models for Many-to-Many Relationship
The final paragraph describes the process of migrating the data and updating the models to accommodate the many-to-many relationship. It starts with creating a pivot table for the article-category relationship, migrating the existing data to this new table, and then removing the old foreign key column from the articles table. The models are updated to reflect the new relationship, with the 'belongs to' relationship being changed to 'belongs to many'. After these changes, the tests are run again, and as expected, they fail due to the updated database schema. The speaker then discusses the necessary changes in the application's views and controllers to handle the new relationship, ensuring that the system can manage and display multiple categories per article. The video concludes with a demonstration of the updated admin panel and a rerun of the tests to confirm that the migration and code changes have been successful.
Mindmap
Keywords
💡Eloquent
💡Many-to-Many Relationship
💡Migration
💡Pivot Table
💡Automated Tests
💡PHPUnit
💡Factories
💡Seeders
💡Refactoring
💡Admin Panel
💡Feature Test
Highlights
The video demonstrates changing a one-to-many relationship to a many-to-many in an existing project.
The example project is a blog where each article can have multiple categories.
The presenter, Pavlos Corp, is a web developer with 15 years of experience, focusing on Laravel.
The project includes an admin panel and a home page displaying articles and categories.
The process starts with writing automated tests to ensure features work as expected before making changes.
Automated tests cover admin viewing articles, editing, adding articles, and home page functionality.
The importance of having green tests before refactoring is emphasized to minimize risks.
The video shows how to create a pivot table for the many-to-many relationship.
Data migration is demonstrated with a single SQL query within a Laravel migration.
The article model's relationship is updated from 'belongsTo' to 'belongsToMany'.
After updating the model, tests are run to identify failures that need to be addressed.
Changes in the views and controllers are shown through GitHub history for visual comparison.
The admin panel's category selection is updated to support multiple selections.
The home page and article page are updated to display multiple categories per article.
The video concludes with all tests passing, indicating a successful migration.
Additional resources for learning about testing and Laravel's many-to-many relationships are provided.
Transcripts
have you ever worked on a project where
you had to change a relationship from
one to many to many to many
in existing project which is already
alive so how to change the code how to
test everything how to make sure there
are no bugs
how to migrate the data all of those
questions will be answered in a demo
project in this video and we will do
exactly that
step by step on an example project so
imagine there is a blog
and every article on a blog has one
category and at some point client
changes their mind and want the article
to have many categories possibly
so let's do that change step by step
before we dive in if you are new to this
channel or you don't know me personally
my name is pavlos corp i'm a web
developer with 15 years of experience
five of them in laravel and i've created
laravel business youtube channel to
share my knowledge about laravel
so with 400 videos it's a pretty big
library of videos so i advise you to
subscribe now let's get back to the
video
so imagine you have a blog with articles
and in the code in eloquent you have
article model
with category belongs to relationship
and in the database
you have articles table with category id
field and
in your website you have two parts admin
panel which is
generated with our quick admin panel and
also you have list of articles some kind
of home page with
all articles category list list of
articles by category and page of
certain article this is a quick demo
project created by my colleague marios
with some dummy data but it's
a real life scenario and imagine we need
to change the article to be
multi-category and it needs to be
changed everywhere
so in this case you have to query
articles in a different way
when viewing the articles the category
list should be a for each loop not just
one category
then in admin panel when adding new
article you should
change how category is selected it
should be multi-select
also in the list of categories there
should be not one category but also
for each loop probably so quite a lot of
things to change
where do we start and how to make it
safely and let's start from a different
question
how will you test everything after you
make the changes in the code
and to answer that question there's one
rule of thumb my own personally
how to make any big refactorings in any
structure of a big project
is first you need to write automated
tests so you need to write
tests of all the features how they
should work
so you wouldn't need to test that
manually everywhere because manually you
will totally forget something
so let's write a script and let's hire
robots to do that job
so in our case let's write automated
tests for if i'm an admin i see articles
with categories i can edit article i can
add
article then from the home page i see
the articles with right categories
the articles are filtered in the right
way and stuff like that
so like a dozen of tests for every
feature for every scenario which would
cover how
system works currently before doing any
changes for relationships so step one
is to write automated tests ensure that
those tests are green
and successful then we make the change
for the relationship and everything in
the code
and by that time those automated tests
should fail
and then we need to change those tests
to represent a new reality
so if in the test the article is created
with belongs to relationship
it should be created what belongs to
many now but the scenario in the test
should be the same so we expect to see
the list of articles
and then when the updated tests are
green and successful
we are pretty sure that we did okay job
at least it minimizes the risks
so in this project we did exactly that
step by step i won't do it live code
because it would take a lot of time i
will show you the result
and first milestone is automated tests
even if you have no
tests in that project it's worthy to
write them specifically for this case
for this scenario you're refactoring
and even if you haven't written
automated tests before
i have a few things to help you there is
one video about simple demo for php unit
and also i have a separate course for
php unit in laravel for beginners
so let's look into the code we've
created tests feature category test
and let's see how it works so it
refreshes the database every time
because we will launch
test on a separate fresh new database
specifically for testing
and then there is a method setup test so
what we need to prepare for
all the cases launch the default heaters
create three categories as an example
create 10 articles
for example and then assign some default
variables which we will use inside of
those tests
for that we need to have seeders we need
to have factories so
we create a database factories article
factory for example with faker
and then we can launch the test for
example for the case of
let's close that one test if admin can
view the articles with categories
and the code of the test is simple we're
acting as
this user and this user is the first
user of our system which is
administrator
we're getting the url of articles and we
assert that the status is okay 200
and we assert that we see the article id
the article title the category and the
word view which is a button to view
so in the admin panel here we see the
title we see the category we see the
button view
it's called the feature test so we're
testing if feature works okay
we don't really care how it works under
the hood we just see the correct text
and then test by test we check other
things so
new article safe category we are saving
the category and then we assert if the
database has the correct article with
correct category
then test for editing the article
for updating the article and then a few
tests on the home page
so homepage show categories so outside
i'm not logged in as administrator
anymore
we're getting the articles and check if
there is an article
in the list with category and with read
more link
then we check articles by category so if
there is a url with category id
we check that all the articles are
present so correctly filtered by that
category
finally test article page show we assert
see that the category is present
and if we run those tests in phpstorm
inside of that
we passed eight tests with 15 assertions
everything is okay
so this is the first milestone
successful green
test before any changes of the code next
step
is actually change the data so first we
need to create a pivot table with
article category
then we need to migrate the data and in
our case it's just
one statement inside of migration one
sql query so inserting into article
categories
select everything from articles and at
this point our data is migrated and then
third thing we need to do is to drop
that column of category id
this is an example that inside of one
migration in laravel you can do whatever
sequence of actions you need
you can change the data you can change
schema in multiple ways
in one migration and if we run that
migration in the terminal so php artisan
migrate
it is successful and if we look in the
database refresh the tables
there is article category filled in and
articles table doesn't have category id
anymore
so we change the database now we need to
change the models
so in article php we changed category to
categories belongs to many
so it was belongs to now it belongs to
many and
category head has many articles now it
belongs to many articles
and at this point we should run the
tests again and see if they fail they
should actually fail
we run the tests so seven
tests failed which means that we
actually broke the system at this point
and now let's fix it again so let's
change our views and controllers
to reflect the new relationships now
changes in the code in the controllers
and views i will show you that as github
history
because it's much more visual you will
understand before and after
i will use a software called sourcetree
for that
and let's see how much it is changed so
everywhere we have article category you
need to change that into categories both
in admin panel
so storing the article is not just store
we need to sync the categories with that
and quite a lot of small changes so
categories sync here in admin panel
then we generate api as well in quick
admin panels so that needs to change as
well
then in request form requests we need to
validate the categories as well in a
different way
then article factory when creating the
article instead of doing category id we
need to
create the category so assign the
categories as well
then forms for administrator quite a lot
of html and
changes for multiple for each instead of
just
simple select drop down so create edit
then index blade is also changing so
instead of showing category name
we're showing for each of the categories
and then when it's shown
also free so that's admin panel and then
on the home page
article controller changing the
relationship and changing the query
if it's with category id as a parameter
and
in the blade we also in index we change
the categories
how it is shown and in the show we also
show multiple categories
so quite a lot of changes like dozen or
two of files
and if we check out that commit
the project should be working so we can
launch the home page again
and we can see multiple categories for
article
it's actually only one in this case so
let's assign multiple categories to for
example article
with id50 and by the way you see the
changes in admin panel as well
so let's assign multiple categories
and article id 50. let's change that to
url with id 50 and let's see if it works
well
so comma separated two categories but
now how do we make sure
we actually didn't miss anything and
it's all working
let's get back to our tests if we run
the tests again
now tests failed too and we need to make
some changes to the tests
so if we go back to our source tree
inside of the test we set up the
situations
with some data which we need to change
so instead of acting
with category id as a parameter to post
we need to post the categories as
array then we need to assert the
database has in a different way
so a few more changes to the tests
and our next goal is to make them green
again
and i've applied those changes and if we
run the tests again
finally it's all green and at this point
we're pretty sure that our migration is
successful
we check the database if the data is
okay
we checked visually if it's clickable
and everything is
kind of loading and at the back of all
of that we wrote and run automated tests
that make sure that all the features are
still working and to be honest those
tests
could be even deeper so with more cases
but it's at least basic minimum of
common main features to test
if you want to learn more about testing
as i said there is a video and
my own course so i will link them in the
description below
and if you want to know more about pivot
table and many-to-many relationships
there's one popular video on this
channel and i will link that in the top
left corner so you can take a look
everything i know with details about
pivot tables in laravel
and see you guys in other videos
関連動画をさらに表示
Serializer Relationships (Using Nested Serializers) | Learn Django REST Framework #11
Model & Prediksi Data | Model data berdasarkan objek [2.1/3]
Generate C# Model from existing Microsoft SQL Server Database
Database Design One to Many Relationships: 7 Steps to Create Them (With Examples)
Concept of Relationships in ER Diagram
Belajar Laravel 11 | 3. Struktur Folder
5.0 / 5 (0 votes)