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_history_db' in the configuration for running tests.
  • Loading branch information
abbastoof committed Jul 8, 2024
1 parent c99e20b commit 102b40b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -12,7 +14,7 @@
DATABASES={
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "postgres",
"NAME": "test_game_history_db",
"USER": "root",
"PASSWORD": "root",
"PORT": "5432",
Expand Down Expand Up @@ -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')
26 changes: 23 additions & 3 deletions Backend/game_history/init_database.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions sample env
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit 102b40b

Please sign in to comment.