Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
aparte file voor aanmaken lege tabellen + bestaande tabellen worden e…
Browse files Browse the repository at this point in the history
…erst gedropt
  • Loading branch information
rvdkeere committed Mar 31, 2024
1 parent efd8c5f commit f83421d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
40 changes: 40 additions & 0 deletions backend/create_database_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from db.extensions import Base, engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy_utils import create_database, database_exists
from sqlalchemy import Table, MetaData, inspect

# noinspection PyUnresolvedReferences
from domain.logic.admin import create_admin # DO NOT REMOVE THIS LINE, create_all doesn't work without it


def initialize_tables(session_instance, engine_instance):
if not database_exists(engine_instance.url):
create_database(engine_instance.url)

if inspect(engine_instance).has_table('users'): # Check if the tables already exist and remove them
print("Dropping existing tables and making new ones...")
metadata = MetaData()
tables = ['submissions',
'students_groups',
'students_subjects',
'teachers_subjects',
'groups',
'projects',
'subjects',
'teachers',
'students',
'admins',
'users']
for name in tables:
table = Table(name, metadata, autoload_with=engine_instance)
table.drop(engine_instance)

Base.metadata.create_all(engine_instance)
session_instance.commit()


if __name__ == "__main__":
session = sessionmaker(autocommit=False, bind=engine)()
initialize_tables(session, engine)
print("Tables initialized")
session.close()
11 changes: 7 additions & 4 deletions backend/fill_database_mock.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from datetime import datetime

from db.extensions import engine
from psycopg2 import tz
from sqlalchemy.orm import sessionmaker

from db.extensions import Base, engine
from domain.logic.admin import create_admin
from domain.logic.group import add_student_to_group, create_group
from domain.logic.project import create_project
Expand All @@ -14,11 +13,13 @@
from domain.logic.teacher import create_teacher
from domain.logic.user import modify_user_roles
from domain.models.SubmissionDataclass import SubmissionState
from create_database_tables import initialize_tables


if __name__ == "__main__":
Base.metadata.create_all(engine)
SessionLocal = sessionmaker(autocommit=False, bind=engine)
session = SessionLocal()
initialize_tables(session, engine) # drops existing tables and makes new ones

# Create subjects
objeprog = create_subject(session, name="Objectgericht Programmeren")
Expand Down Expand Up @@ -93,7 +94,7 @@
groep5_algo = create_group(session, algo_project.id)
groep6_algo = create_group(session, algo_project.id)
groep1_web = create_group(session, web_project.id)
groep2_web = create_group(session, web_project.id) # empty group
groep2_web = create_group(session, web_project.id) # empty group

# Create students
student1 = create_student(session, "Lukas", "[email protected]")
Expand Down Expand Up @@ -211,3 +212,5 @@
add_teacher_to_subject(session, student6.id, webtech.id)

session.commit()
print("Filled database with mock data")
session.close()
3 changes: 2 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ uvicorn==0.27.1
httpx==0.27.0
defusedxml~=0.7.1
cryptography~=42.0.5
PyJWT~=2.8.0
PyJWT~=2.8.0
SQLAlchemy-Utils==0.41.2

0 comments on commit f83421d

Please sign in to comment.