Skip to content

DB Init

Tong Lam edited this page May 2, 2024 · 4 revisions

Environment

We have three enviornments for the project, which are: dev, prod and test. So we will create three databases respectively, each for one envrionment.

dev

The dev enviornment is the most frequently used environment during our devlopment process. Issue #89 implemented an auto generated script when statring the app in the dev environment. It works as the following flow chart shown.

db creation

flowchart TD
    A[Start] --> B{Does dev db exsit?}
    B -->|Yes| C1{Does dev db need migrations?}
    C1 -->|Yes| D1[Flask Migrate to the head version]
    D1 --> End[End]
    C1 -->|No| End
    B -->|No| C2[Create a new dev db]
    C2 --> D2[Execute backup SQL]
    D2 --> E2[Update dev version to head]
    E2 --> End
Loading

Check create_dev_db() and migrate_dev_db() in /app/__init__.py.

data

The mandatory data is maintained under /sql/dev.backup.sql. It will be executed every time when the app recreates the new dev db. Alternatively, you can insert it into your existing dev db manually.

test

The test environment is used only for testing, especially for unit testing.

db creation

The test db will be created every time you run a test under /tests. It is implemented in /tests/config.py; check it for details.

  • Generally speaking, the db will be created using db.create_all() after the app instance is created.
  • Then, the seed data in tests/seeds will be inserted into the test db in the TestBase.setUp() process, which occurs after the app instance is created and before your test code is executed.
  • When your test is finished, the data will be cleaned up in tearDown(), which happens after your test code has been executed.

data

All the test data is organized under /tests/seeds; create your seed data based on the db model.

prod

The prod environment is used for deploying the website online, visit here. For now, the prod db will be updated manually, so there's no need to worry about it.

Clone this wiki locally