Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ SENTRY_DSN=
# Configure these with your own Docker registry images
DOCKER_IMAGE_BACKEND=backend
DOCKER_IMAGE_FRONTEND=frontend

# DSN for database reverse engineering
DATABASE_SOURCE_DSN=postgresql+psycopg2://<USER>:<PASSWORD>@<IP>:<PORT>/<DB_NAME>
44 changes: 44 additions & 0 deletions backend/app/generated_sqlmodels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import uuid

from sqlalchemy import Boolean, Column, ForeignKeyConstraint, Index, PrimaryKeyConstraint, String, Uuid
from sqlmodel import Field, Relationship, SQLModel

class Category(SQLModel, table=True):
__table_args__ = (
PrimaryKeyConstraint('id', name='category_pkey'),
)

name: str = Field(sa_column=Column('name', String(100), nullable=False))
id: uuid.UUID = Field(sa_column=Column('id', Uuid, primary_key=True))
created_at: str = Field(sa_column=Column('created_at', String, nullable=False))
description: str | None = Field(default=None, sa_column=Column('description', String(500)))


class User(SQLModel, table=True):
__table_args__ = (
PrimaryKeyConstraint('id', name='user_pkey'),
Index('ix_user_email', 'email', unique=True)
)

email: str = Field(sa_column=Column('email', String(255), nullable=False))
is_active: bool = Field(sa_column=Column('is_active', Boolean, nullable=False))
is_superuser: bool = Field(sa_column=Column('is_superuser', Boolean, nullable=False))
hashed_password: str = Field(sa_column=Column('hashed_password', String, nullable=False))
id: uuid.UUID = Field(sa_column=Column('id', Uuid, primary_key=True))
full_name: str | None = Field(default=None, sa_column=Column('full_name', String(255)))

item: list['Item'] = Relationship(back_populates='owner')


class Item(SQLModel, table=True):
__table_args__ = (
ForeignKeyConstraint(['owner_id'], ['user.id'], ondelete='CASCADE', name='item_owner_id_fkey'),
PrimaryKeyConstraint('id', name='item_pkey')
)

title: str = Field(sa_column=Column('title', String(255), nullable=False))
id: uuid.UUID = Field(sa_column=Column('id', Uuid, primary_key=True))
owner_id: uuid.UUID = Field(sa_column=Column('owner_id', Uuid, nullable=False))
description: str | None = Field(default=None, sa_column=Column('description', String(255)))

owner: 'User' | None = Relationship(back_populates='item')
2 changes: 2 additions & 0 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ dependencies = [
"pydantic-settings<3.0.0,>=2.2.1",
"sentry-sdk[fastapi]<2.0.0,>=1.40.6",
"pyjwt<3.0.0,>=2.8.0",
"sqlacodegen>=3.1.1",
"psycopg2>=2.9.10",
]

[tool.uv]
Expand Down
44 changes: 44 additions & 0 deletions backend/scripts/generate_models.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# PowerShell version of SQLModel generation script

# Simple SQLModel generation script
# Read database configuration from .env file

# Stop on error
$ErrorActionPreference = "Stop"

Write-Host "Reading configuration from .env file..." -ForegroundColor Blue

# Read .env file
$envFile = "../../.env"
if (-not (Test-Path $envFile)) {
Write-Error ".env file not found at $envFile"
exit 1
}

# Parse .env file
$envVars = @{}
Get-Content $envFile | ForEach-Object {
if ($_ -match '^([^#][^=]*?)=(.*)$') {
$envVars[$matches[1]] = $matches[2]
}
}

if (-not $envVars.ContainsKey('DATABASE_SOURCE_DSN')) {
Write-Error "DATABASE_SOURCE_DSN not found in .env file"
exit 1
}

$databaseDsn = $envVars['DATABASE_SOURCE_DSN']

Write-Host "Executing sqlacodegen..." -ForegroundColor Blue
Write-Host "Command: uv run sqlacodegen --generator sqlmodels `"$databaseDsn`" --outfile ../app/generated_sqlmodels.py" -ForegroundColor Gray

# Execute sqlacodegen
uv run sqlacodegen --generator sqlmodels "$databaseDsn" --outfile ../app/generated_sqlmodels.py

if ($LASTEXITCODE -eq 0) {
Write-Host "SQLModel code generated successfully: ../app/generated_sqlmodels.py" -ForegroundColor Green
} else {
Write-Error "Failed to generate SQLModel code"
exit 1
}
15 changes: 15 additions & 0 deletions backend/scripts/generate_models.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#! /usr/bin/env bash

# Simple SQLModel generation script
# Read database configuration from .env file

set -e
set -x

# Read .env file
source ../../.env

# Execute sqlacodegen
sqlacodegen --generator sqlmodels "$DATABASE_SOURCE_DSN" --outfile ../app/generated_sqlmodels.py

echo "SQLModel code generated successfully: ../app/generated_sqlmodels.py"
Loading
Loading