Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating schema dynamically #102

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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