Skip to content

Commit e6eab42

Browse files
committed
update database configurations for multiple DB support
1 parent e7745a3 commit e6eab42

File tree

5 files changed

+46
-33
lines changed

5 files changed

+46
-33
lines changed

.env.example

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
DB_TYPE=postgresql
2-
DB_NAME=blogdb
3-
DB_USER=blogusr
4-
DB_PASSWORD=blogpwd
1+
DB_TYPE=sqlite # Options: postgresql, mysql
2+
DB_NAME=db.sqlite3
3+
DB_USER=fastapiusr
4+
DB_PASSWORD=fastapipwd
55
DB_HOST=localhost
6-
DB_PORT=5432
7-
DB_URL=postgresql://blogusr:blogpwd@localhost:5432/blogdb
6+
DB_PORT=
7+
# Uncomment the line below if DB_TYPE=postgresql
8+
# DB_URL=postgresql://fastapiusr:fastapipwd@localhost:5432/fastapidb
9+
# Uncomment the line below if DB_TYPE=mysql
10+
# DB_URL=mysql+pymysql://fastapiusr:fastapipwd@localhost:3306/fastapidb
11+
MYSQL_DRIVER=

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,30 @@ venv: ## Create a virtual environment
3939
install: ## Install development dependencies
4040
$(PIP) install --upgrade pip
4141
$(PIP) install -r requirements.txt
42-
$(PIP) install pre-commit==3.7.1
43-
$(PRE_COMMIT) install
4442
cp .env.example .env
4543
@echo "Development dependencies has been setup."
4644

4745
migrate: ## Run the database migration
4846
$(ALEMBIC) upgrade head
4947

5048
check: ## Run all checks using tox and pre-commit
49+
$(PIP) install tox==4.16.0 pre-commit==3.7.1
5150
$(TOX)
51+
$(PRE_COMMIT) install
5252
$(PRE_COMMIT) run --all-files
5353
@echo "All checks passed"
5454

5555
run: ## Run the development server
5656
$(UVICORN) main:app --reload
5757

58-
clean: ## Clean up the project of unneeded files
59-
@echo "Cleaning up the project of unneeded files..."
58+
clean: ## Clean up the project
59+
@echo "Cleaning up the project of temporary files and directories..."
6060
@rm -rf $(VENV_DIR)
6161
@rm -rf .cache
6262
@rm -rf build
6363
@rm -rf htmlcov coverage.xml .coverage
6464
@rm -rf .tox
65+
@rm -rf *.log
6566
@rm -rf .mypy_cache
6667
@rm -rf .ruff_cache
6768
@rm -rf *.egg-info

api/db/database.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,21 @@
1515

1616

1717
def get_db_engine(test_mode: bool = False): # type: ignore
18-
DATABASE_URL = f"postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
19-
2018
if DB_TYPE == "sqlite" or test_mode:
2119
BASE_PATH = f"sqlite:///{BASE_DIR}"
22-
DATABASE_URL = BASE_PATH + "/"
23-
24-
if test_mode:
25-
DATABASE_URL = BASE_PATH + "test.db"
20+
DATABASE_URL = BASE_PATH + ("/test.db" if test_mode else "/db.sqlite3")
2621

27-
return create_engine(
28-
DATABASE_URL, connect_args={"check_same_thread": False}
29-
)
22+
return create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
23+
elif DB_TYPE == "mysql":
24+
DATABASE_URL = (
25+
f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
26+
)
3027
elif DB_TYPE == "postgresql":
3128
DATABASE_URL = (
3229
f"postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
3330
)
31+
else:
32+
raise ValueError(f"Unsupported DB_TYPE: {DB_TYPE}")
3433

3534
return create_engine(DATABASE_URL)
3635

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ pytest==8.3.1
77
pytest-mock==3.14.0
88
python-decouple==3.8
99
SQLAlchemy==2.0.31
10-
tox==4.16.0

tests/database.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,29 @@
77
from api.db.database import Base, get_db
88
from main import app
99

10-
DB_TYPE = config("DB_TYPE")
11-
DB_NAME = config("DB_NAME")
12-
DB_USER = config("DB_USER")
13-
DB_PASSWORD = config("DB_PASSWORD")
14-
DB_HOST = config("DB_HOST")
15-
DB_PORT = config("DB_PORT")
16-
MYSQL_DRIVER = config("MYSQL_DRIVER")
17-
DATABASE_URL = ""
18-
19-
SQLALCHEMY_DATABASE_URL = f"{DB_TYPE}+{MYSQL_DRIVER}://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}_test"
20-
21-
22-
engine = create_engine(SQLALCHEMY_DATABASE_URL)
10+
DB_TYPE = config("DB_TYPE", default="sqlite")
11+
DB_NAME = config("DB_NAME", default="test.db")
12+
DB_USER = config("DB_USER", default="")
13+
DB_PASSWORD = config("DB_PASSWORD", default="")
14+
DB_HOST = config("DB_HOST", default="")
15+
DB_PORT = config("DB_PORT", default="")
16+
MYSQL_DRIVER = config("MYSQL_DRIVER", default="pymysql")
17+
18+
if DB_TYPE == "sqlite":
19+
SQLALCHEMY_DATABASE_URL = f"sqlite:///./{DB_NAME}"
20+
elif DB_TYPE == "mysql":
21+
SQLALCHEMY_DATABASE_URL = f"mysql+{MYSQL_DRIVER}://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}_test"
22+
elif DB_TYPE == "postgresql":
23+
SQLALCHEMY_DATABASE_URL = (
24+
f"postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}_test"
25+
)
26+
else:
27+
raise ValueError(f"Unsupported DB_TYPE: {DB_TYPE}")
28+
29+
engine = create_engine(
30+
SQLALCHEMY_DATABASE_URL,
31+
connect_args={"check_same_thread": False} if DB_TYPE == "sqlite" else {},
32+
)
2333

2434
TestingSessionLocal = sessionmaker(
2535
autocommit=False,

0 commit comments

Comments
 (0)