# dna-api

## Overview
dna-api is an asynchronous web API that serves data from a Postgres database. It uses asyncio and marshmallow for API functionality, and sqitch for database migrations. dna-api depends on Atlas auth and the apollo user service, as well as search index and artifacts generated by `dna-etl-data-ingest-scheduled-service`.

## Local installation
- Install Python 3.9 (or create a virtual environment)
- Configure pip using `config/pip.conf`
- Install dependencies by running `pip install -r requirements/dev.txt`

## Environment variables
Environment variables are located in `.env`. A sample file is included in `.env.sample`.

## Data Storage
The API uses PostgreSQL as its operational database. The database connection information is stored in the `.env` file.

## Dependencies
The dna-api project depends on the following:

- Atlas authentication
- Apollo user service
- Search index and artifacts generated by `dna-etl-data-ingest-scheduled-service`

## Usage
To run the project locally, use the following command:
```bash
adev runserver .
```
The API will be available at http://localhost:8000/. A swagger interface can be accessed at http://localhost:8000/api/doc/.

## Running in Docker
dna-api can also be run in a Docker container. To start the container, use the following command:

```bash
make docker/up-local
```

## Running Tests
Tests can be run using the following command:
```bash
make test
```

## Code Style Check
Code style can be checked using the following command:
```bash
make checkstyle
```

## Security Check
Security checks can be run using the following command:
```bash
make security
```

## Code Prettifier
Code can be prettified using the following command:
```bash
make prettify
```

## Pre-Commit
`make pre-commit` is a combination of the above commands and should be run before committing changes.

## Using sqitch for migrations
Service `dna-api` uses `PostgreSQL` for storing data and `sqitch` for migrations with `PostgreSQL`. To make migrations or to make some tests locally you should:

**1.** Install [PostgreSQL](https://www.postgresql.org/download/)

**2.** Install [Sqitch](https://sqitch.org/download/)

**3.** After installing `PostgreSQL` and `Sqitch`:

Create postgres user:
```bash
$ psql -U postgres
```

In `psql` shell:
```bash
create user <user>;
```

Give him permissions to create databases:
```bash
alter user <user> createdb;
```

Exit from `psql`:
```bash
\q
```

Create a `dna-api` database with user you've just created:
```bash
createdb dna_api -U <user>
```

Make migrations:
```bash
sqitch deploy -u <user>
```

Basicly, that's it, you made initial migrations.

Now, you can check the status running:
```bash
sqitch status -u <user>
```

Now you can make your own migrations, to do so: you need to:

**1.** add a new migration files
```bash
sqitch add <migration_name> --requires <the last migration name> -n 'description`
```
for example:
```bash
sqitch add new_migration --requires create_table_dna_account -n 'dummy migration`
```

To get the last migration name, you can check the `server/db/pgdb_migrations/sqitch.plan` file, it wll be the last line.

After you have added a new migration, you can check that in `server/db/pgdb_migrations/sqitch.plan` a name of your migation now located at the end of file, also in `[]` brackets is a name of migration that current miration depends on (`--requires` flag). Also there were created three new files in `deploy`, `verify` and `revert` folders.

Now you can open new file in `deploy` folder and add some `SQL` dummy `SQL` code (`SELECT 1`). Then you should add some verification to the new file in `verify` folder. That's should verify the result of your migration. To make migration (deploy) and verify you changes to db you should run:
```bash
sqitch deploy --verify -u <user>
```
Or
```bash
sqitch deploy -u <user>
```

and then
```bash
sqitch verify -u <user>
```
Also, you can revert your changes:
```bash
siqtch revert -u <user>
```

or revert for some migrations above, for example 2 migrations above:
```bash
sqitch revert --to HEAD^^ -u <user>
```

and check your actions:
```bash
sqitch status -u <user>
```

you will see, that `name` will changed to the first migration name.

Also, you can check logs:
```bash
sqitch log -u <user>
```

For more information check the official [Sqitch](https://sqitch.org/docs/manual/sqitchtutorial/) documentation.