From 102b40b2cce036b4453c836fc1c01429e4b8afab Mon Sep 17 00:00:00 2001 From: abbastoof Date: Mon, 8 Jul 2024 18:03:41 +0300 Subject: [PATCH] fix: Ensure proper database permissions and migration setup - Updated init_database.sh to grant necessary privileges to the PostgreSQL user. - Included ALTER USER and GRANT statements to allow the user to create databases and manage tables, sequences, and functions. - Added migration command in conftest.py to ensure Django migrations are applied before running tests. - Verified database connection and user permissions for successful test execution. - Changed database name to 'test_game_history_db' in the configuration for running tests. --- .../game_history/tests/conftest.py | 7 +++-- Backend/game_history/init_database.sh | 26 ++++++++++++++++--- sample env | 12 +++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 sample env diff --git a/Backend/game_history/game_history/game_history/tests/conftest.py b/Backend/game_history/game_history/game_history/tests/conftest.py index 2a19453..2ed71aa 100755 --- a/Backend/game_history/game_history/game_history/tests/conftest.py +++ b/Backend/game_history/game_history/game_history/tests/conftest.py @@ -3,6 +3,8 @@ from django.conf import settings import pytest from datetime import timedelta +from django.core.management import call_command + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'game_history.settings') @@ -12,7 +14,7 @@ DATABASES={ "default": { "ENGINE": "django.db.backends.postgresql", - "NAME": "postgres", + "NAME": "test_game_history_db", "USER": "root", "PASSWORD": "root", "PORT": "5432", @@ -93,10 +95,11 @@ def django_db_setup(): settings.DATABASES['default'] = { 'ENGINE': 'django.db.backends.postgresql', - 'NAME': 'postgres', + 'NAME': 'test_game_history_db', 'USER': 'postgres', 'PASSWORD': 'postgres', 'HOST': 'localhost', 'PORT': '5432', 'ATOMIC_REQUESTS': True, } + call_command('migrate') diff --git a/Backend/game_history/init_database.sh b/Backend/game_history/init_database.sh index 5a87858..6f964f0 100755 --- a/Backend/game_history/init_database.sh +++ b/Backend/game_history/init_database.sh @@ -28,10 +28,30 @@ exec postgres -D /var/lib/postgresql/data & # Wait for PostgreSQL to start (you may need to adjust the sleep time) sleep 5 -# Create a new PostgreSQL user and set the password +# # Create a new PostgreSQL user and set the password psql -c "CREATE USER ${DB_USER} WITH PASSWORD '${DB_PASS}';" -psql -c "GRANT ALL PRIVILEGES ON SCHEMA public TO ${DB_USER};" -psql -c "GRANT ALL PRIVILEGES ON DATABASE postgres TO ${DB_USER};" +# psql -c "GRANT ALL PRIVILEGES ON SCHEMA public TO ${DB_USER};" +# psql -c "GRANT ALL PRIVILEGES ON DATABASE postgres TO ${DB_USER};" + +# # Create the database named test_game_history_db. +# psql -c "CREATE DATABASE test_game_history_db;" +# psql -c "GRANT ALL PRIVILEGES ON DATABASE test_game_history_db TO ${DB_USER};" + +# Grant all necessary privileges to the user +psql -c "ALTER USER ${DB_USER} CREATEDB;" +psql -c "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO ${DB_USER};" +psql -c "GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO ${DB_USER};" +psql -c "GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public TO ${DB_USER};" + +# Create the database named test_game_history_db. +psql -c "CREATE DATABASE test_game_history_db OWNER ${DB_USER};" +psql -c "GRANT ALL PRIVILEGES ON DATABASE test_game_history_db TO ${DB_USER};" + + +# Run Django migrations +cd /app +source venv/bin/activate +python manage.py migrate # Stop the PostgreSQL server after setting the password pg_ctl stop -D /var/lib/postgresql/data diff --git a/sample env b/sample env new file mode 100644 index 0000000..11308ae --- /dev/null +++ b/sample env @@ -0,0 +1,12 @@ +DB_USER = "root" +DB_PASS = "root" + +RABBITMQ_DEFAULT_USER="user" +RABBITMQ_DEFAULT_PASS="pass" + +RABBITMQ_HOST="rabbitmq" +RABBITMQ_USER="user" +RABBITMQ_PASS="pass" +RABBITMQ_PORT="5672" + +PGPASSWORD='root'