fix: implemented basic db test functions that interact with db

This commit is contained in:
iritmaximus
2023-02-05 22:11:59 +02:00
parent 77a4f0a924
commit 6ae1765f40
10 changed files with 87 additions and 148 deletions
-50
View File
@@ -1,50 +0,0 @@
services:
flask:
# adding this ****ing little **** took at least 3 hours of debugging
# command: sh -c "gunicorn -w 4 flaskr:app -b 0.0.0.0:5000 --access-logfile '-' --error-logfile '-'"
build:
context: .
dockerfile: Dockerfile
ports:
- "5000:5000"
volumes:
- .:/app
env_file: .env
environment:
- POSTGRES_DB
- POSTGRES_USER
- POSTGRES_PASSWORD
- POSTGRES_URL
- ENVIRONMENT=production
depends_on:
db:
condition: service_healthy
db:
image: postgres:bullseye
volumes:
- postgres_db:/var/lib/postgresql/data
env_file: .env
environment:
- POSTGRES_DB
- POSTGRES_USER
- POSTGRES_PASSWORD
restart: always
expose:
- 5432
healthcheck:
test: sh -c "pg_isready -d ${POSTGRES_DB} -U ${POSTGRES_USER}"
interval: 3s
timeout: 2s
retries: 30
# adminer:
# image: adminer
# restart: always
# ports:
# - 8080:8080
volumes:
postgres_db:
+1 -15
View File
@@ -1,22 +1,8 @@
from flask import Flask from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from os import getenv
import psycopg2
from flaskr.views import general
from flaskr.views import forum
from flaskr.views import users
from dotenv import load_dotenv from dotenv import load_dotenv
load_dotenv() load_dotenv()
app = Flask(__name__) app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = str(getenv("POSTGRES_URL")) import flaskr.views
db = SQLAlchemy(app)
app.register_blueprint(general.mod)
app.register_blueprint(forum.mod)
app.register_blueprint(users.mod)
+51
View File
@@ -0,0 +1,51 @@
from os import getenv
from flaskr import app
from flask_sqlalchemy import SQLAlchemy
from random import randint
app.config["SQLALCHEMY_DATABASE_URI"] = getenv("POSTGRES_URL")
db = SQLAlchemy(app)
def create_db():
print("Initializing the db...")
sqllist = [
"CREATE TABLE IF NOT EXISTS users (id int primary key, name varchar(80), password varchar(80));",
"CREATE TABLE IF NOT EXISTS games (id int primary key, name varchar(80));",
"CREATE TABLE IF NOT EXISTS courses (id int primary key, name varchar(80), game_id int references games(id));",
"CREATE TABLE IF NOT EXISTS standards (id int primary key, name varchar(50), tier varchar(3), points numeric, game_id int references games(id), course_id int references courses(id));",
"CREATE TABLE IF NOT EXISTS times (id int primary key, game_id int references games(id), course_id int references courses(id), timems varchar(80), date date);",
]
with app.app_context():
for sql in sqllist:
db.session.execute(sql)
db.session.commit()
print("Db initialization completed.")
return "Success"
def test_db():
print("TESTING THE DB ***********************")
with app.app_context():
sql = "DROP TABLE IF EXISTS test;"
db.session.execute(sql)
db.session.commit()
sql = "CREATE TABLE IF NOT EXISTS test (id int primary key, greeting text);"
db.session.execute(sql)
db.session.commit()
sql = f"INSERT INTO test (id, greeting) VALUES ({randint(1,100)}, 'hello');"
db.session.execute(sql)
db.session.commit()
result = db.session.execute("SELECT * FROM test;").fetchall()
print(result)
return "Working lol"
-25
View File
@@ -1,25 +0,0 @@
import flaskr
def create_db():
print("Initializing the db...")
def test_db():
print("TESTING THE DB ***********************")
with flaskr.app.app_context():
sql = "DROP TABLE IF EXISTS test"
flaskr.db.session.execute(sql)
sql = "CREATE TABLE IF NOT EXISTS test (id int primary key, greeting text)"
flaskr.db.session.execute(sql)
sql = "INSERT INTO test (id, greeting) VALUES (1, 'hello')"
flaskr.db.session.execute(sql)
result = flaskr.db.session.execute("SELECT * FROM test").fetchall()
print(result)
return "Working lol"
+22
View File
@@ -0,0 +1,22 @@
from flaskr import app
from flaskr.database import db, test_db, create_db
@app.route("/")
def test_backend():
return "This is the working backend"
@app.route("/db")
def test_db_route():
return test_db()
@app.route("/login")
def login():
return "Login to the site"
@app.route("/init")
def initialize_db():
return create_db()
View File
-13
View File
@@ -1,13 +0,0 @@
from flask import Blueprint
mod = Blueprint("forum", __name__)
@mod.route("/forum")
def show_forum():
return "All of the forum things"
@mod.route("/forum/add")
def add_new_post():
return "New member to the familytree"
-19
View File
@@ -1,19 +0,0 @@
from flask import Blueprint
from flaskr import db
mod = Blueprint("general", __name__)
@mod.route("/")
def test_backend():
return "This is the working backend"
@mod.route("/db")
def test_db_route():
return db.test_db()
@mod.route("/login")
def login():
return "Login to the site"
-13
View File
@@ -1,13 +0,0 @@
from flask import Blueprint
mod = Blueprint("users", __name__)
@mod.route("/users/<id>")
def user(_id):
return "Account information"
@mod.route("/users/create")
def create_user():
return "New user"
+13 -13
View File
@@ -1,33 +1,33 @@
CREATE TABLE IF NOT EXIXST users ( CREATE TABLE IF NOT EXISTS users (
id int primary key, id int primary key,
name varchar(80), name varchar(80),
password varchar(80), password varchar(80)
); );
CREATE TABLE IF NOT EXIST games ( CREATE TABLE IF NOT EXISTS games (
id int primary key,
name varchar(80)
);
CREATE TABLE IF NOT EXISTS courses (
id int primary key, id int primary key,
name varchar(80), name varchar(80),
game_id int references games(id)
); );
CREATE TABLE IF NOT EXIST courses ( CREATE TABLE IF NOT EXISTS standards (
id int primary key,
name varchar(80),
game_id int references games(id),
);
CREATE TABLE IF NOT EXIST standards (
id int primary key, id int primary key,
name varchar(50), name varchar(50),
tier varchar(3), tier varchar(3),
points numeric, points numeric,
game_id int references games(id), game_id int references games(id),
course_id int references courses(id), course_id int references courses(id)
); );
CREATE TABLE IF NOT EXIST times ( CREATE TABLE IF NOT EXISTS times (
id int primary key, id int primary key,
game_id int references games(id), game_id int references games(id),
course_id int references courses(id), course_id int references courses(id),
timems varchar(80), timems varchar(80),
date date, date date
); );