This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
aparte file voor aanmaken lege tabellen + bestaande tabellen worden e…
…erst gedropt
- Loading branch information
Showing
3 changed files
with
49 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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") | ||
|
@@ -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]") | ||
|
@@ -211,3 +212,5 @@ | |
add_teacher_to_subject(session, student6.id, webtech.id) | ||
|
||
session.commit() | ||
print("Filled database with mock data") | ||
session.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters