From 9abf4cc359c47f0482828c4e713828b30b06aa7d Mon Sep 17 00:00:00 2001 From: Robert Jambrecic Date: Thu, 20 Jun 2024 14:48:20 +0200 Subject: [PATCH] Create migrations for new db --- google_sheets/app.py | 4 ++-- google_sheets/db_helpers.py | 6 +++--- migrations/20240620124134_initial/migration.sql | 14 ++++++++++++++ migrations/migration_lock.toml | 3 +++ 4 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 migrations/20240620124134_initial/migration.sql create mode 100644 migrations/migration_lock.toml diff --git a/google_sheets/app.py b/google_sheets/app.py index 3bcd57c..0ddd975 100644 --- a/google_sheets/app.py +++ b/google_sheets/app.py @@ -11,7 +11,7 @@ from prisma.errors import RecordNotFoundError from . import __version__ -from .db_helpers import get_db_connection, get_wasp_db_url +from .db_helpers import get_db_connection, get_db_url __all__ = ["app"] @@ -45,7 +45,7 @@ async def get_user(user_id: Union[int, str]) -> Any: - wasp_db_url = await get_wasp_db_url() + wasp_db_url = await get_db_url(db_name="waspdb") async with get_db_connection(db_url=wasp_db_url) as db: user = await db.query_first( f'SELECT * from "User" where id={user_id}' # nosec: [B608] diff --git a/google_sheets/db_helpers.py b/google_sheets/db_helpers.py index 928e4b7..434ffc8 100644 --- a/google_sheets/db_helpers.py +++ b/google_sheets/db_helpers.py @@ -10,7 +10,7 @@ async def get_db_connection( db_url: Optional[str] = None, ) -> AsyncGenerator[Prisma, None]: if not db_url: - db_url = environ.get("DATABASE_URL", None) + db_url = await get_db_url(db_name="gsheets") if not db_url: raise ValueError( "No database URL provided nor set as environment variable 'DATABASE_URL'" @@ -25,9 +25,9 @@ async def get_db_connection( await db.disconnect() -async def get_wasp_db_url() -> str: +async def get_db_url(db_name: str) -> str: curr_db_url = environ.get("DATABASE_URL") - wasp_db_name = environ.get("WASP_DB_NAME", "waspdb") + wasp_db_name = environ.get("WASP_DB_NAME", db_name) wasp_db_url = curr_db_url.replace(curr_db_url.split("/")[-1], wasp_db_name) # type: ignore[union-attr] if "connect_timeout" not in wasp_db_url: wasp_db_url += "?connect_timeout=60" diff --git a/migrations/20240620124134_initial/migration.sql b/migrations/20240620124134_initial/migration.sql new file mode 100644 index 0000000..46c7cfd --- /dev/null +++ b/migrations/20240620124134_initial/migration.sql @@ -0,0 +1,14 @@ +-- CreateTable +CREATE TABLE "GAuth" ( + "id" TEXT NOT NULL, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + "user_id" INTEGER NOT NULL, + "creds" JSONB NOT NULL, + "info" JSONB NOT NULL, + + CONSTRAINT "GAuth_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "GAuth_user_id_key" ON "GAuth"("user_id"); diff --git a/migrations/migration_lock.toml b/migrations/migration_lock.toml new file mode 100644 index 0000000..99e4f20 --- /dev/null +++ b/migrations/migration_lock.toml @@ -0,0 +1,3 @@ +# Please do not edit this file manually +# It should be added in your version-control system (i.e. Git) +provider = "postgresql"