fix(db): resolve pg role issues

- Seperate posgres user for development and testing
This commit is contained in:
minhtrannhat 2024-03-01 01:33:21 -05:00
parent b80883bec0
commit 573d7ead6c
Signed by: minhtrannhat
GPG Key ID: E13CFA85C53F8062
5 changed files with 20 additions and 7 deletions

View File

@ -1,4 +1,4 @@
UVICORN_PORT="5050" UVICORN_PORT="5050"
TODO_DEBUG=true TODO_DEBUG=true
TODO_SECRET_KEY="secret key" TODO_SECRET_KEY="secret key"
TODO_DB_DATABASE_URL="postgresql://todo:todo@0.0.0.0:5432/todo" TODO_DB_DATABASE_URL="postgresql://todo_dev:todo@0.0.0.0:5432/todo_dev"

View File

@ -66,4 +66,4 @@ dev = {cmd = "uvicorn neo_neo_todo.main:app --reload", env_file = "development.e
recreate-db-base = "python3 src/neo_neo_todo/utils/database.py" recreate-db-base = "python3 src/neo_neo_todo/utils/database.py"
migration-base = "python3 src/neo_neo_todo/migrations/0.py" migration-base = "python3 src/neo_neo_todo/migrations/0.py"
recreate-db = {composite = ["recreate-db-base", "migration-base"], env_file = "development.env"} recreate-db = {composite = ["recreate-db-base", "migration-base"], env_file = "development.env"}
test = {composite = ["recreate-db-base", "pytest tests/"], env_file = "testing.env"} test = {composite = ["recreate-db-base", "migration-base", "pytest tests/"], env_file = "testing.env"}

View File

@ -21,7 +21,8 @@ def migrate() -> None:
port = url_parts.port port = url_parts.port
print( print(
f"The database name is {dbname}, the username is {user_name}, the password is {password}, the host is {host}, the port is {port}" f"The database name is {dbname}, the username is {user_name}, "
f"the password is {password}, the host is {host}, the port is {port}"
) )
with psycopg.connect( with psycopg.connect(

View File

@ -21,7 +21,9 @@ def recreate_db() -> None:
port = url_parts.port port = url_parts.port
print( print(
f"The database name is {dbname}, the username is {user_name}, the password is {password}, the host is {host}, the port is {port}" "Running database recreation script, "
f"The database name is {dbname}, the username is {user_name}, "
f"the password is {password}, the host is {host}, the port is {port}"
) )
with psycopg.connect( with psycopg.connect(
@ -31,8 +33,17 @@ def recreate_db() -> None:
conn._set_autocommit(True) conn._set_autocommit(True)
cur.execute( cur.execute(
sql.SQL("DROP OWNED BY {}").format( sql.SQL(
sql.Identifier(user_name) # type: ignore """
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = '{}') THEN
DROP OWNED BY {};
END IF;
END $$;
"""
).format(
sql.Identifier(user_name), sql.Identifier(user_name) # type: ignore
) )
) )
@ -42,6 +53,7 @@ def recreate_db() -> None:
dbname=sql.Identifier(dbname), dbname=sql.Identifier(dbname),
) )
) )
# Drop the user if it exists # Drop the user if it exists
cur.execute( cur.execute(
sql.SQL("DROP USER IF EXISTS {}").format( sql.SQL("DROP USER IF EXISTS {}").format(

View File

@ -2,4 +2,4 @@ UVICORN_PORT="5050"
TODO_DEBUG=true TODO_DEBUG=true
TODO_SECRET_KEY="secret key" TODO_SECRET_KEY="secret key"
TODO_TESTING=true TODO_TESTING=true
TODO_DB_DATABASE_URL="postgresql://todo:todo@0.0.0.0:5432/todo_test" TODO_DB_DATABASE_URL="postgresql://todo_test:todo@0.0.0.0:5432/todo_test"