Skip to content

Commit

Permalink
fix: Ensure proper database permissions and migration setup
Browse files Browse the repository at this point in the history
- 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_stats_db' in the configuration for running tests.
  • Loading branch information
abbastoof committed Jul 8, 2024
1 parent 102b40b commit d1c8dd2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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_stats_service.settings')

Expand All @@ -12,7 +13,7 @@
DATABASES={
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "postgres",
"NAME": "test_game_stats_db",
"USER": "root",
"PASSWORD": "root",
"PORT": "5432",
Expand Down Expand Up @@ -99,10 +100,11 @@
def django_db_setup():
settings.DATABASES['default'] = {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'NAME': 'test_game_stats_db',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'localhost',
'PORT': '5432',
'ATOMIC_REQUESTS': True,
}
call_command('migrate')
19 changes: 17 additions & 2 deletions Backend/game_stats_service/init_database.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,23 @@ sleep 5

# 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};"

# 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_stats_db.
psql -c "CREATE DATABASE test_game_stats_db OWNER ${DB_USER};"
psql -c "GRANT ALL PRIVILEGES ON DATABASE test_game_stats_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
Expand Down

0 comments on commit d1c8dd2

Please sign in to comment.