cerc_persistence/README.md

64 lines
2.4 KiB
Markdown
Raw Normal View History

2023-10-31 13:20:38 -04:00
# CERC Persistence
2023-10-24 11:47:35 -04:00
The persistence package includes classes to store different class objects in a Postgres database.
## Models
2023-10-24 11:47:35 -04:00
This defines models for all class objects that we want to persist. It is used for Object Relation Mapping (ORM)
of the class objects to database table columns
2023-10-31 13:20:38 -04:00
## Repositories
This defines repository classes that contain CRUD methods for database operations. The constructor of all repositories
2023-10-31 13:20:38 -04:00
requires the database name to connect to and the application environment (PROD or TEST). Tests use a different database
from the production environment, which is why this is necessary. An example is shown below:
2023-10-24 11:47:35 -04:00
```python
2023-11-14 12:05:40 -05:00
from cerc_persistence.db_setup import DBSetup
2023-10-24 11:47:35 -04:00
# instantiate city repo for hub production database
2023-11-14 12:05:40 -05:00
db = DBSetup(db_name='hub', app_env='PROD', dotenv_path='/path/to/.env', admin_password='application_password', application_uuid='UUID')
2023-10-24 11:47:35 -04:00
```
2023-10-31 13:20:38 -04:00
All database operations are conducted with the production database (*PROD*) named *hub* in the example above.
2023-10-24 11:47:35 -04:00
2023-10-31 13:20:38 -04:00
## config_db
This Python file is a configuration class that contains variables that map to configuration parameters in a .env file.
2023-10-24 11:47:35 -04:00
It also contains a method ``def conn_string()`` which returns the connection string to a Postgres database.
## Base
This class has a constructor that establishes a database connection and returns a reference for database-related CRUD
operations.
2023-10-24 11:47:35 -04:00
2023-10-31 13:20:38 -04:00
## Database Configuration Parameter
A .env file (or environment variables) with configuration parameters described below are needed to establish a database
connection:
2023-10-24 11:47:35 -04:00
```
# production database credentials
PROD_DB_USER=postgres-database-user
PROD_DB_PASSWORD=postgres-database-password
PROD_DB_HOST=database-host
PROD_DB_PORT=database-port
# test database credentials
TEST_DB_USER=postgres-database-user
TEST_DB_PASSWORD=postgres-database-password
TEST_DB_HOST=database-host
TEST_DB_PORT=database-port
```
2023-10-31 13:20:38 -04:00
## Database Related Unit Test
2023-10-24 11:47:35 -04:00
Unit tests that involve database operations require a Postgres database to be set up.
The tests connect to the database server using the default postgres user (*postgres*).
2023-10-24 11:47:35 -04:00
NB: You can provide any credentials for the test to connect to postgres, just make sure
2023-10-31 13:20:38 -04:00
the credentials are set in your .env file as explained above in *Database Configuration Parameters* section.
2023-10-24 11:47:35 -04:00
When the tests are run, a **test_db** database is created and then the required tables for
the test. Before the tests run, the *test_db* is deleted to ensure that each test starts
2023-10-31 13:20:38 -04:00
on a clean slate.