Database - chaosmentors/the-godparent GitHub Wiki
Version 0.1.21144.1
Created by famaniel
Last Modified by famaniel
Creating and updating a database
To be independent of the underlying database management system, the application uses Flask-SQLAlchemy to access and maintain the database. This ORM-Framework allows database independent creation of models, and updates and maintains the database automatically instead of having the developer do the heavy lifting.
Flask offers a very simple way of initializing a database, by using the predefined db commands. The commands are called from the command line. To make things easier, and provide all necessary parameters, the project comes with a simple script that will call the necessary commands. The scripts will setup the virtual environment correctly, and then call the corresponding commands.
To setup the database code, the flask_init script needs to be called.
This script needs to be run every time a developer changes the database model.
To initialise a database run the following code on the terminal:
cd the-godparent
./flask_init
The script will call flasks own db migrate and db upgrade command,
which will perform the following tasks:
- Analyse the source code, and check for changes in the model classes.
- Check, if a database exists, and compare its schema with the model classes
- Generate scripts that will alter the database, so that the schema fits the defined model.
- Generate scripts that will update existing data, so that it fits into the updated schema
- Check if the database exists, create it when necessary
- Check which migrations have already been run
- Run all migrations that haven't been run yet, in order of their creation.
This makes it easier to upgrade the database. The script will attempt to run the automatic migration Flask offers. If this is not sufficiant, all of flasks database commands can still be invoked manually.