Implement postgresql database handling into backend and basic init handling code, also include dev database docker compose file

This commit is contained in:
Curt Spark 2024-04-17 14:58:18 +01:00
parent 2871e5fc65
commit 6f8e0e02bb
4 changed files with 71 additions and 1 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
.venv
__pycache__
__pycache__
postgres-data

28
docker-compose.yml Normal file
View File

@ -0,0 +1,28 @@
services:
blorg-dev-db:
image: postgres:16-alpine
restart: always
volumes:
- ./postgres-data:/var/lib/postgresql/data
environment:
POSTGRES_USER: dev
POSTGRES_PASSWORD: dev
POSTGRES_DB: blorgdb
networks:
blorg-dev-db-network:
ipv4_address: 172.20.0.10
deploy:
resources:
limits:
cpus: '0.10'
memory: 128M
networks:
blorg-dev-db-network:
driver: bridge
ipam:
config:
- subnet: 172.20.0.0/16

40
main.py
View File

@ -5,6 +5,46 @@ from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import psycopg2
dbconn = psycopg2.connect(database="blorgdb",
host="172.20.0.10",
user="dev",
password="dev",
port="5432")
dbcursor = dbconn.cursor()
dbcursor.execute("""
DO $$
BEGIN
IF (EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'users'))
THEN
RAISE NOTICE 'Table does exist!';
ELSE
RAISE NOTICE 'User Table does not exist! Creating table.';
CREATE TABLE users (
UserID int,
FirstName varchar(255),
LastName varchar(255),
PasswordHash varchar(255)
);
END IF;
END;
$$""")
#print(dbcursor.fetchall())
for notice in dbconn.notices:
print("(SQL SERVER) " + notice)
dbconn.commit()
dbcursor.close()
dbconn.close()
app = FastAPI()
origins = [

View File

@ -4,6 +4,7 @@ click==8.1.7
fastapi==0.110.1
h11==0.14.0
idna==3.7
psycopg2-binary==2.9.9
pydantic==2.7.0
pydantic_core==2.18.1
sniffio==1.3.1