Skip to content

Commit

Permalink
Creating schema dynamically (#102)
Browse files Browse the repository at this point in the history
* Creating schema dynamically

* Making sure alembic succeeds before starting app
  • Loading branch information
nathan-moore-97 authored Nov 17, 2023
1 parent af6e831 commit 010f355
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ Gdrive utilizes a postgres database to write various persistent data points. (ex

However, if the user does wish to work on this DB locally, follow steps to [install PostgresDB](https://dev.to/sfpear/install-and-use-postgres-in-wsl-423d#:~:text=To%20install%20Postgres%20and%20run%20it%20in%20WSL%2C,installation%20and%20get%20the%20version%20number%3A%20psql%20--version).

Once installed, a schema needs to be created for IDVA.
> **_NOTE:_** Once installed, a schema needs to be created for IDVA. `env.py` in alembic handles this dynamically, using SqlAlchemy to check if the schema is present and creating if not. This is done before any migrations take place and should be safe to run on a fresh db install.
```sql
create schema if not exists idva;
```
Once the above SQL has been run on postgres, alembic can be used to build the DDL Dependencies.

Alembic uses the same connection string and schema as the gdrive module, loading the
Expand Down
4 changes: 4 additions & 0 deletions alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from sqlalchemy import engine_from_config
from sqlalchemy import pool
from sqlalchemy import schema

from alembic import context
import sqlalchemy
Expand Down Expand Up @@ -77,6 +78,9 @@ def run_migrations_online() -> None:
connect_args={"options": "-csearch_path=%s" % (settings.SCHEMA)},
)

if not connectable.dialect.has_schema(connectable, settings.SCHEMA):
connectable.execute(schema.CreateSchema(settings.SCHEMA))

with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)

Expand Down
2 changes: 1 addition & 1 deletion manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ applications:
buildpacks:
- python_buildpack
command: |
alembic upgrade head
alembic upgrade head &&
uvicorn gdrive.main:app --host 0.0.0.0 --port $PORT
services:
- gdrive
Expand Down

0 comments on commit 010f355

Please sign in to comment.