Use Neon branching for testing
We previously ran a DELETE
query on the shoes
table to remove duplicates. However the query was wrong because it didn’t specify all the criteria. Here how the query should have been written:
DELETE FROM shoes a USING shoes b
WHERE a.id > b.id
AND a.brand = b.brand
AND a.model = b.model
AND a.description = b.description
AND a.color = b.color
Since a DELETE
is a sensitive operation, it is recommended to first test the query and make sure it works as expected on a new branch before trying it on the backup
branch.
Navigate to the Branches
page and click on the New Branch
button to create a new branch.
This time, we want create a test branch from Head
with the most up-to-date state of the database and specify backup
as parent.
Note: if you skipped the Data recovery with branching section, you should use main
as the parent branch.
Run remove duplicates query on backup branch
Let’s now navigate to the SQL Editor, select the test
branch and run the query to remove duplicates.
DELETE FROM shoes a USING shoes b
WHERE a.id > b.id
AND a.brand = b.brand
AND a.model = b.model
AND a.description = b.description
AND a.color = b.color
Let’s now run a SELECT model, color FROM shoes
query to verify the result.
| # | model | color |
|---|----------------------|------------------------------------------------|
| 1 | Air Zoom Alphafly | Scream Green/Bright Crimson/Honeydew/Black |
| 2 | Air Zoom Alphafly | Total Orange/Bright Crimson/Ghost Green/Black |
| 3 | ZoomX Vaporfly | Scream Green/Bright Crimson/Honeydew/Black |
Yes! We managed to remove duplicates from the shoes
table. We can now safely apply the same query to the parent branch.
Conclusion
Congratulations! You completed the tutorial. Here is a recap of what we covered:
- You created a Neon project, a schema and inserted data to a table
- You learned about database branching and how to use it for test and data recovery