`python3 -m pip --no-cache-dir install common --index-url https://code.ornl.gov/api/v4/projects/10568/packages/pypi/simple --trusted-host code.ornl.gov`
## Usage
A docker image and python package are provided for use as a base image in `docker-compose.yaml`.
`common_package/sql/` contains the .sql files `create_tables.sql` and `fill_tables.sql` which serve as examples for how to initialize a postgres Database in your project (See below in the Creating an extensible Database Python Class section for details in how to create the Python Class.) The `volumes` statement in the docker-compose files runs these sql files once the container is created.
### Configuration via Environment Variables
Below is a list of environmental variables and what they do:
- DATABASE_TIMEOUT: an integer representing the seconds to wait before deciding a timeout occurred.
- DATABASE_DB: a string representing the database to connect to
- DATABASE_USER: a string representing the user to connect to the database as
- DATABASE_PW: a string representing the password for the DATABASE_USER
- DATABASE_HOST: a string representing the hostname or IP address hosting the database
- DATABASE_PORT: an integer representing the port to connect to the database on
- DATABASE_SCHEMA: a string representing the schema to default to when creating a database connection
These evironmental variables have been assigned default values for the Docker container in the file `envfile`, which is called in `docker-compose.yaml` and `docker-compose.test.yaml`
### Creating an extensible Database Python Class
Some `Database` methods such as `query` and `open` rely on
[python mixins](https://www.python.org/dev/peps/pep-0487/), which allow
abstract classes to interact with different types of databases and provide
additional functionality not provided by the `Database` class. Below is an
example of how to create a Database class that uses the PostgresMixin:
```
from common.mixins.postgres import PostgresMixin
from common.database import Database
class PostgresDatabase(Database, PostgresMixin):
"""The class that allows Database to interact with psycopg2."""
# TODO add additional functions for your class here, specific to your needs
with PostgresDatabase() as db:
db.query('SELECT NOW()')
```
## Testing
Testing for the `common_package` is contained within `/common_package/test/test.py`. This can be accessed by building the test docker compose file with:
`docker-compose -f docker-compose.test.yaml up -d`
and subsequently checking the results in the logs: