Skip to content

Commit 1e5b53e

Browse files
committed
Update database script
Signed-off-by: Vikram Vaswani <112123850+vikram-dagger@users.noreply.github.com>
1 parent a3396de commit 1e5b53e

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

conftest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11

22
import pytest
3+
import os
34
from sqlalchemy import create_engine, inspect
45
from sqlalchemy.orm import sessionmaker
56
from fastapi.testclient import TestClient
67
from .main import create_app
78
from .dependencies import get_db, database_url
89
from .models import Base
910

11+
TEST_DATABASE_URL = os.getenv("TEST_DATABASE_URL", "postgresql://app_user:secret@db/app_db_test")
1012

1113
@pytest.fixture(scope="session")
1214
def test_engine():
1315
"""Create test database engine"""
14-
test_engine = create_engine(database_url)
16+
test_engine = create_engine(TEST_DATABASE_URL)
1517
Base.metadata.create_all(bind=test_engine)
1618
yield test_engine
1719
Base.metadata.drop_all(bind=test_engine)

docker-compose.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ services:
77
- "8000:8000"
88
environment:
99
- DATABASE_URL=postgresql://app_user:secret@db/app_db
10+
- TEST_DATABASE_URL=postgresql://app_user:secret@db/app_db_test
1011
depends_on:
1112
db:
1213
condition: service_healthy
@@ -18,13 +19,13 @@ services:
1819
container_name: fastapi_db
1920
restart: unless-stopped
2021
environment:
21-
POSTGRES_DB: app_db
2222
POSTGRES_USER: app_user
2323
POSTGRES_PASSWORD: secret
2424
ports:
2525
- "5432:5432"
2626
volumes:
2727
- postgres_data:/var/lib/postgresql/data
28+
- ./init-dbs.sh:/docker-entrypoint-initdb.d/init-dbs.sh:ro
2829
healthcheck:
2930
test: ["CMD-SHELL", "pg_isready -U app_user -d app_db"]
3031
interval: 10s

init-dbs.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# init-db.sh
2+
#!/bin/bash
3+
set -e
4+
5+
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
6+
CREATE DATABASE app_db;
7+
CREATE DATABASE app_db_test;
8+
EOSQL

0 commit comments

Comments
 (0)