Database models and migrations for Automating the Ask.
Project description
ata-db-models
Database models and migrations for Automating the Ask.
Usage
As a package
We use SQLModel, a layer on top of SQLAlchemy with Pydantic, to define our tables. This is useful because we can import this package to interact with the tables AND have Pydantic objects in Python that correspond to a row in the table.
To install the package from PyPi, run: pip install ata-db-models
. Check existing versions
here.
Initialize a new cluster
If you want to initialize a fresh database cluster, pass in the env vars to connect to the cluster and run init_db
.
If the target cluster has IP restrictions, make sure your IP address is a valid access point.
An example run with fake credentials (from the root dir of this project with the virtual env
activated):
HOST=fakehost USER=fakeuser PASSWORD=fakepw DB_NAME=postgres python src/init_db.py
No PORT
is passed because the default port is 5432, the standard for Postgres.
Migrations
So you made some changes to what tables there are, what columns there are, indices, etc. and you'd like to update the databases. This is what alembic is for!
To generate a new revision after you've updated the models:
- Run this from the root of the project:
DB_CONNECTION_STRING='postgresql://user:password@host:port/db_name' alembic revision --autogenerate -m "message"
- Check the
/alembic/versions/
directory for the new revision and verify that it does what you want it to - Run this from the root of the project:
DB_CONNECTION_STRING='postgresql://user:password@host:port/db_name' alembic upgrade head
Development
This project uses Poetry to manage dependencies. It also helps with pinning dependency and python versions. We also use pre-commit with hooks for isort, black, and flake8 for consistent code style and readability. Note that this means code that doesn't meet the rules will fail to commit until it is fixed.
We use mypy for static type checking. This can be run manually,
and the CI runs it on PRs to the main
branch. We also use pytest to run our tests.
This can be run manually and the CI runs it on PRs to the main
branch.
Setup
- Install Poetry.
- Run
poetry install --no-root
- Run
source $(poetry env list --full-path)/bin/activate && pre-commit install && deactivate
to set uppre-commit
You're all set up! Your local environment should include all dependencies, including dev dependencies like black
.
This is done with Poetry via the poetry.lock
file.
Run Code Format and Linting
To manually run isort, black, and flake8 all in one go, simply run pre-commit run --all-files
. Explore the pre-commit
docs (linked above)
to see more options.
Run Static Type Checking
To manually run mypy, simply run mypy
from the root directory of the project. It will use the default configuration
specified in pyproject.toml
.
Update Dependencies
To update dependencies in your local environment, make changes to the pyproject.toml
file then run poetry update
from the root directory of the project.
Run Tests
To manually run rests, you need to have a Postgres instance running locally on port 5432. One way to do this is to run a Docker container, then run the tests while it is active.
- (If you don't already have the image locally) Run
docker pull postgres
- Run
docker run --rm --name postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_HOST_AUTH_METHOD=trust -p 127.0.0.1:5432:5432/tcp postgres
- Run
DB_NAME=postgres pytest tests
from the root directory of the project. Explore thepytest
docs (linked above) to see more options.
Note that if you decide to run the Postgres container with different credentials (a different password, port, etc.) or via a different method, you will likely need to update the test file to point to the correct Postgres instance.
Additionally, if you want to re-run the tests, you want to make sure you start over from a fresh Postgres
instance. If you run Postgres via Docker, you can simply ctrl-C
to stop the image and start a new one.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for ata_db_models-0.0.13-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 02ad2da72ae80f26b38f595534a5029d194a254ce0325c3299cf75002c22b509 |
|
MD5 | 8915de9130db16d1b0d5a67ebbadbc95 |
|
BLAKE2b-256 | 00324955f8878860d84ad4c9748a9d082bfcf968d0329b27e8b2c535ad9ba245 |