From 59d0d2f8f1c12984e31c015a6bd6129bacdcc267 Mon Sep 17 00:00:00 2001 From: Jakub Dubec Date: Sun, 24 Mar 2024 10:58:40 +0100 Subject: [PATCH] =?UTF-8?q?Use=20same=20database=20with=20readonly=20mode?= =?UTF-8?q?=20=F0=9F=97=9C=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/core/jobs.py | 51 ++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/apps/core/jobs.py b/apps/core/jobs.py index 93c6a66..fc54a0a 100644 --- a/apps/core/jobs.py +++ b/apps/core/jobs.py @@ -11,6 +11,7 @@ from uuid import UUID import docker +import psycopg import sentry_sdk from django.conf import settings from django.db import connection @@ -47,28 +48,42 @@ def prepare(self): cursor.execute( f"CREATE USER {self._database_name} WITH CREATEDB ENCRYPTED PASSWORD '{self._database_password}';" ) - cursor.execute(f"GRANT {self._database_name} TO {settings.DATABASES['default']['USER']};") + cursor.execute(f"GRANT CONNECT ON DATABASE {self._task.assigment.database} TO {self._database_name};") cursor.execute(f"CREATE DATABASE {self._database_name} OWNER {self._database_name};") connection.commit() + conn = psycopg.connect( + host=settings.DATABASES['default']['HOST'], + dbname=self._task.assigment.database, + user=settings.DATABASES['default']['USER'], + password=settings.DATABASES['default']['PASSWORD'], + port=settings.DATABASES['default']['PORT'] + ) + + with conn.cursor() as cursor: + cursor.execute(f"GRANT USAGE ON SCHEMA public TO {self._database_name};") + cursor.execute(f"GRANT SELECT ON ALL TABLES IN SCHEMA public TO {self._database_name};") + conn.commit() + conn.close() + self._task.additional_information["database"] = {"name": self._database_name} # Recover database - command = [settings.PSQL_PATH, self._database_name] - - with open(f"{settings.DBS_DATABASES_PATH}/{self._task.assigment.database}.sql") as f: - proc = subprocess.Popen( - " ".join(command), - shell=True, - stdin=f, - stdout=subprocess.DEVNULL, - env={ - "PGPASSWORD": self._database_password, - "PGUSER": self._database_name, - "PGHOST": settings.DATABASES["default"]["HOST"], - }, - ) - proc.wait() + # command = [settings.PSQL_PATH, self._database_name] + + # with open(f"{settings.DBS_DATABASES_PATH}/{self._task.assigment.database}.sql") as f: + # proc = subprocess.Popen( + # " ".join(command), + # shell=True, + # stdin=f, + # stdout=subprocess.DEVNULL, + # env={ + # "PGPASSWORD": self._database_password, + # "PGUSER": self._database_name, + # "PGHOST": settings.DATABASES["default"]["HOST"], + # }, + # ) + # proc.wait() def run(self): client = docker.from_env() @@ -196,8 +211,8 @@ def run(self): def cleanup(self): with connection.cursor() as cursor: - cursor.execute(f"DROP DATABASE {self._database_name};") - cursor.execute(f"DROP USER {self._database_name};") + cursor.execute(f"DROP DATABASE IF EXISTS {self._database_name};") + cursor.execute(f"DROP USER IF EXISTS {self._database_name};") connection.commit() @staticmethod