In this lesson, you will see an example of restructuring the database.
Imagine a scenario:
- You have Posts that belong to one Category
- At some point, you decide to have multiple categories
So, from one-to-many to many-to-many. But the project is already live. What should you do?
Step 1: What Changes Are Needed?
First, let's identify the behavior changes.
Change 1. When viewing a post, instead of showing a single category, it should list all categories with a foreach loop.
Change 2. When showing posts by a category, the query should also be changed.
Where do we start, and how do we do it safely?
Step 2: Automated Tests
Let's start with a different question: "How will you test everything after you make the changes in the code?"
To answer that question, I have a personal rule of thumb. For making any big structure refactorings in a big project: first, write automated tests.
You need to write tests of all the features and how...