-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…1520) ## Summary Fixes #1270 ### Time to review: __10 mins__ ## Changes proposed Modify our SQLAlchemy logic to allow for multiple schemas to be setup. This includes: * Setting the schema explicitly in a class that all SQLAlchemy models inherit from * Setting up a schema translate map (only meaningfully needed for local tests) to allow for changing the name of a schema * A new (LOCAL ONLY) script for creating the `api` schema ## Context for reviewers **Non-locally this change does not actually change anything yet - locally it does by making local development more similar to non-local** This does not actually setup any new schemas, and every table we create still lives in a single schema, the `api` schema. This change looks far larger than it actually is. Before, all of our tables had their schema set implicitly by the `DB_SCHEMA` environment variable. Locally this value was set to `public` and non-locally it was set to `api`. These changes make it so locally it also uses `api`, however in order for that to work, the Alembic migrations need to explicitly say `api` (in case we add more schemas later). There is a flag in the Alembic configuration that tells it to generate with the schemas, but we had that disabled. I enabled it so future migrations _just work_. But in order to make everything work locally, I had to manually fix all of the past migrations to have the `api` schema. Non-locally the schema already was `api` so changing already-run migrations won't matter as they already ran as if they had that value set. ## Additional information This change requires you run `make db-recreate` locally in order to use the updated schemas. To test this, I manually ran the database migrations one step at a time, fixing any issues. I then ran the down migrations and made sure they also worked correctly, undoing the up migrations correctly. I then ran a few of our local scripts to make sure everything still worked properly and didn't find any issues. --------- Co-authored-by: nava-platform-bot <[email protected]>
- Loading branch information
1 parent
a59f0a2
commit 85acd72
Showing
34 changed files
with
399 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from enum import StrEnum | ||
|
||
|
||
class Schemas(StrEnum): | ||
API = "api" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import logging | ||
|
||
from sqlalchemy import text | ||
|
||
import src.adapters.db as db | ||
import src.logging | ||
from src.adapters.db import PostgresDBClient | ||
from src.constants.schema import Schemas | ||
from src.util.local import error_if_not_local | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def setup_local_postgres_db() -> None: | ||
with src.logging.init(__package__): | ||
error_if_not_local() | ||
|
||
db_client = PostgresDBClient() | ||
|
||
with db_client.get_connection() as conn, conn.begin(): | ||
for schema in Schemas: | ||
_create_schema(conn, schema) | ||
|
||
|
||
def _create_schema(conn: db.Connection, schema_name: str) -> None: | ||
logger.info("Creating schema %s if it does not already exist", schema_name) | ||
conn.execute(text(f"CREATE SCHEMA IF NOT EXISTS {schema_name}")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.