fix(db): resolve pg role issues
- Seperate posgres user for development and testing
This commit is contained in:
parent
b80883bec0
commit
573d7ead6c
@ -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"
|
||||||
|
@ -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"}
|
||||||
|
@ -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(
|
||||||
|
@ -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(
|
||||||
|
@ -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"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user