Data Seeding and Migration
Well, this is not the seed that we are going to talking about. I am going to talk about data seeding and also its migration implemented on my group project, Digipus, build on Django. Let’s talk about some basic definitions first, then talk about how my group implements both data seeding and migration to the project itself.
Data seeding and migration
The goal of data seeding is pretty straight forward actually. Your team is going to set up an application, but you need to pre-populate your database with hard-coded initial data, so the user interface will look good without any real data made yet.
While the goal of data migration is to propagate the changes implemented on the models (like adding, deleting, or modifying a field). You do need to implement the models first before you migrate and seed your initial data. To make it even clearer, I will show an example of how data seeding and migration is implemented to one of the models in Django.
Implementation: Digipus
As we are going to focus more on data seeding and migration, I will assume that the tests are made, you can also read how I made the test for the same model I will discuss (User) in my previous article. There are 3 steps to be done: defining the models, migrate our models to the database, and seed our initial data to the database.
Creating the user model, and add a custom user manager
There are two types of registered users on Digipus: Contributor (User) and Admin (Superuser). Define those users on models.py.
Migrate our models to the database
To manually migrate our models to the database, there are two commands to be run. First, the makemigrations command, which is done to create migration files from our previously defined models.py.
After the migration files are made, we need to apply our changes to the tables in our database by using the migrate command.
This is the final look of our user table in the database.
Seed our initial data to our database
Last but not least, we need to seed our initial data into the database using fixtures. “Fixtures” is a directory containing an initial JSON file, literally called initial.json, filled with hard-coded predefined data of our users.
Automatic migration on deployment
Later on deployment, every migration process is done automatically by adding the commands on the .gitlab-ci.yaml file.
Conclusion
Data seeding and migration are really useful, especially at the beginning of the development, where no real data exists, but you need to show something to ensure your project is doing good.
References
Data Seeding. (n.d.). Retrieved April 29, 2020, from https://docs.djangoproject.com/en/2.2/howto/initial-data/
Data Migration. (n.d.). Retrieved April 29, 2020, from https://docs.djangoproject.com/en/2.2/topics/migrations/#data-migrations