Skip to content

Commit 70a38cb

Browse files
committed
Update fastapi template for postgresql 18
1 parent 10ecc17 commit 70a38cb

File tree

4 files changed

+15
-26
lines changed

4 files changed

+15
-26
lines changed

src/fastapi/core_files.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ fn create_core_utils_file() -> String {
168168
169169
from collections.abc import Callable
170170
from typing import Any
171-
from uuid import uuid4
172171
173172
from fastapi import APIRouter as FastAPIRouter
174173
from fastapi.types import DecoratedCallable
@@ -200,10 +199,6 @@ class APIRouter(FastAPIRouter):
200199
return add_path(func)
201200
202201
return decorator
203-
204-
205-
def create_db_primary_key() -> str:
206-
return str(uuid4())
207202
"#
208203
.to_string()
209204
}
@@ -229,7 +224,6 @@ from loguru import logger
229224
230225
from {module}.core.config import settings
231226
from {module}.core.security import get_password_hash
232-
from {module}.core.utils import create_db_primary_key
233227
from {module}.exceptions import NoDbPoolError
234228
from {module}.services.db.user_services import get_user_by_email
235229
@@ -290,17 +284,16 @@ class Database:
290284
logger.debug(f"User with email {{settings.FIRST_SUPERUSER_EMAIL}} not found, adding")
291285
query = """
292286
INSERT INTO users (
293-
id, email, full_name, hashed_password, is_active, is_superuser
287+
email, full_name, hashed_password, is_active, is_superuser
294288
)
295-
VALUES ($1, $2, $3, $4, $5, $6)
289+
VALUES ($1, $2, $3, $4, $5)
296290
"""
297291
298292
hashed_password = get_password_hash(settings.FIRST_SUPERUSER_PASSWORD.get_secret_value())
299293
async with self.db_pool.acquire() as conn:
300294
try:
301295
await conn.execute(
302296
query,
303-
create_db_primary_key(),
304297
settings.FIRST_SUPERUSER_EMAIL,
305298
settings.FIRST_SUPERUSER_NAME,
306299
hashed_password,

src/fastapi/docker_files.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn create_dockercompose_file(project_info: &ProjectInfo) -> String {
7070
- traefik.http.routers.${{STACK_NAME?Variable not set}}-backend-https.middlewares=${{STACK_NAME?Variable not set}}-api-rate-limit,${{STACK_NAME?Variable not set}}-security-headers
7171
7272
db:
73-
image: postgres:17-alpine
73+
image: postgres:18-alpine
7474
restart: unless-stopped
7575
container_name: {base_name}-db
7676
healthcheck:
@@ -84,7 +84,6 @@ fn create_dockercompose_file(project_info: &ProjectInfo) -> String {
8484
env_file:
8585
- .env
8686
environment:
87-
- PGDATA=/var/lib/postgresql/data/pgdata
8887
- POSTGRES_PASSWORD=${{POSTGRES_PASSWORD?Variable not set}}
8988
- POSTGRES_USER=${{POSTGRES_USER?Variable not set}}
9089
- POSTGRES_DB=${{POSTGRES_DB?Variable not set}}

src/fastapi/migration_files.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{file_manager::save_file_with_content, project_info::ProjectInfo};
55

66
fn create_initial_up_migration() -> String {
77
r#"CREATE TABLE IF NOT EXISTS users (
8-
id TEXT PRIMARY KEY,
8+
id UUID DEFAULT uuidv7() PRIMARY KEY,
99
email TEXT NOT NULL UNIQUE,
1010
full_name TEXT NOT NULL,
1111
hashed_password TEXT NOT NULL,

src/fastapi/service_files.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ from typing import TYPE_CHECKING
9595
from loguru import logger
9696
9797
from {module}.core.security import get_password_hash, verify_password
98-
from {module}.core.utils import create_db_primary_key
9998
from {module}.exceptions import DbInsertError, DbUpdateError, UserNotFoundError
10099
from {module}.models.users import (
101100
UpdatePassword,
@@ -127,16 +126,15 @@ async def authenticate(*, pool: Pool, email: str, password: str) -> UserInDb | N
127126
async def create_user(*, pool: Pool, cache_client: Valkey, user: UserCreate) -> UserInDb:
128127
query = """
129128
INSERT INTO users (
130-
id,
131129
email,
132130
full_name,
133131
hashed_password,
134132
is_active,
135133
is_superuser
136134
)
137-
VALUES ($1, $2, $3, $4, $5, $6)
135+
VALUES ($1, $2, $3, $4, $5)
138136
RETURNING
139-
id,
137+
id::text,
140138
email,
141139
full_name,
142140
hashed_password,
@@ -148,7 +146,6 @@ async def create_user(*, pool: Pool, cache_client: Valkey, user: UserCreate) ->
148146
async with pool.acquire() as conn:
149147
result = await conn.fetchrow(
150148
query,
151-
create_db_primary_key(),
152149
user.email,
153150
user.full_name,
154151
get_password_hash(user.password),
@@ -167,7 +164,7 @@ async def create_user(*, pool: Pool, cache_client: Valkey, user: UserCreate) ->
167164
168165
169166
async def delete_user(*, pool: Pool, cache_client: Valkey, user_id: str) -> None:
170-
query = "DELETE FROM users WHERE id = $1"
167+
query = "DELETE FROM users WHERE id::text = $1"
171168
async with pool.acquire() as conn:
172169
async with asyncio.TaskGroup() as tg:
173170
db_task = tg.create_task(conn.execute(query, user_id))
@@ -183,7 +180,7 @@ async def delete_user(*, pool: Pool, cache_client: Valkey, user_id: str) -> None
183180
184181
async def get_users(*, pool: Pool, offset: int = 0, limit: int = 100) -> list[UserInDb] | None:
185182
query = """
186-
SELECT id,
183+
SELECT id::text,
187184
email,
188185
full_name,
189186
hashed_password,
@@ -241,7 +238,7 @@ async def get_users_public(
241238
242239
async def get_user_by_email(*, pool: Pool, email: str) -> UserInDb | None:
243240
query = """
244-
SELECT id,
241+
SELECT id::text,
245242
email,
246243
full_name,
247244
hashed_password,
@@ -278,15 +275,15 @@ async def get_user_by_id(*, pool: Pool, cache_client: Valkey, user_id: str) -> U
278275
return cached_user
279276
280277
query = """
281-
SELECT id,
278+
SELECT id::text,
282279
email,
283280
full_name,
284281
hashed_password,
285282
is_active,
286283
is_superuser,
287284
last_login
288285
FROM users
289-
WHERE id = $1
286+
WHERE id::text = $1
290287
"""
291288
292289
async with pool.acquire() as conn:
@@ -341,9 +338,9 @@ async def update_user(
341338
query = """
342339
UPDATE users
343340
SET hashed_password=$1
344-
WHERE id = $2
341+
WHERE id::text = $2
345342
RETURNING
346-
id,
343+
id::text,
347344
email,
348345
full_name,
349346
hashed_password,
@@ -373,9 +370,9 @@ async def update_user(
373370
query = f"""
374371
UPDATE users
375372
SET {{set_clause}}
376-
WHERE id = $1
373+
WHERE id::text = $1
377374
RETURNING
378-
id,
375+
id::text,
379376
email,
380377
full_name,
381378
hashed_password,

0 commit comments

Comments
 (0)