`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`
@@ -21,6 +20,9 @@ A docker image and python package are provided for use as a base image in `docke
`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.
## Releases
To initiate a release, increment the value in `COMMON_VERSION` run `./release.sh release`. This will force checkout develop and run the release process to push to the package registry and container registry.
### Configuration via Environment Variables
Below is a list of environmental variables and what they do:
@@ -54,6 +56,81 @@ with PostgresDatabase() as db:
db.query('SELECT NOW()')
```
### Creating a CRUD table with crud_table.py
```
from common.mixins.postgres import PostgresMixin
from common.database import Database
from common.crud_table import CRUDTable
# PLEASE NOTE: the name of class MUST be consistent with the name of the
# table in the database itself.
class users(CRUDTable):
"""Provides CRUD access to the users table"""
def __init__(self, db):
"""__init__:
:param db: the database that contains the 'users' table.
"""
# be sure to define the columns as they appear in the database.
# in this example, the users table has 6 columns.
columns = {
'id': int,
'email': str,
'password': str,
'roles': str,
'token': str,
'is_active': bool
}
# initialize the super class with the column definition, the schema that
# the table is in, and the database object.
super().__init__(columns, schema='public', db)
# TODO add additional functions for your class here, specific to your needs
class PostgresDatabase(Database, PostgresMixin):
"""The class that allows Database to interact with psycopg2."""
def __init__(self):
super().__init__()
# here we are assigning the CRUD table to the database's 'users'
# variable and passing the database object to the constructor
self.users = users(self)
# TODO add additional functions for your class here, specific to your needs
with PostgresDatabase() as db:
db.query('SELECT NOW()')
# this will create a user in the database, with the specified fields and
# to delete entries, a where clause must also be passed. The were clause
# for a deletion should come in the form of keyword arguments
# 'DELETE FROM public.users WHERE id=1337;'
db.users.delete(id=1337)
```
## 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: