Hard Prerequisites |
|
In this project you will practice the basics of alembic based migrations. A lot of this stuff might seem quite straight-forward now, but focus! When mygrations go wrong in production then life gets way too hard, and real user data can be put at serious risk!
This excercise will also teach you a bit more about why git is important. Make sure you commit often! Seriously! Git will save your bacon!
In this project you’ll need 2 databases, I’ll refer to them as “development” and “prod”. How you set these up is up to you. You could have one docker composition that sets them both up, or (easier) you can have 2 docker compositions that are well named.
This is similar to how you would work with real databases later in life. You’ll have a database on your computer that you can experiment on. If you destroy the data there then it’s not a big deal at all. Your job is to keep your prod database safe.
personal_email_address
. Set it to be a required field, and must be uniqueThis should have been quite smooth
Now a lot can go wrong with databases and migrations. Ideally the db, your models and your migrations will all be in sync. If they fall out of sync life gets kinda hard sometimes.
Generally the models should be considered the truth of things. That truth should then be propogated to the migration files, and then to the database. If the database is updated independantly then weird things start to happen.
In this secrtion we’ll be updating the database independantly. In real life you wouldn’ do this intentionally.
So why are we even doing this weird thing?
Basically if you are working on a team the one of your team mates migration files might disagree with yours. And you might end up in a mess. Or your migration files that work totally fine against your dev db might not work against prod because the last person to update the prod db wasn’t you. This can lead to all sortf of chaos - that chaos is avoidable. But it’s realy worth appreciating.
We’re going to go through 3 situations. They are all nice and simple on their own. In real applications the database structure generally has many interlinking tables. Eg our Umuzi recruitment portal has 26 tables, and they’re all connected to each other.
using your dev db:
Do whatever it takes to get it to work… you might need to checkout an earlier commit. By the end you’ll have the C26 recruits in the prod database.
Rules: Dont delete any data in your production database! But you can completely delete your dev database if you wanted to.
using your dev db:
Do whatever it takes to get it to work… you might need to checkout an earlier commit. By the end you’ll have the C27 recruits in the prod database.
Rules: Dont delete any data in your production database! But you can completely delete your dev database if you wanted to.
using your dev db:
Do whatever it takes to get it to work… you might need to checkout an earlier commit. By the end you’ll have the C28 recruits in the prod database.
Rules: Dont delete any data in your production database! But you can completely delete your dev database if you wanted to.
Earlier I hinted that there are ways to prevent migration chaos. Basically what we do at Umuzi is work with a couple of rules.