Skip to content

Commit

Permalink
added migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
themrphantom committed Apr 26, 2024
1 parent df3dc1e commit 6ff0f4f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
31 changes: 31 additions & 0 deletions backend/database/Migrations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from flask_sqlalchemy import SQLAlchemy
from database.Models import *
from sqlalchemy.orm import session
from sqlalchemy import text
import util


def migrate(db: session):
current_db_version: KeyValue = db.query(KeyValue).filter_by(
key="version").first()
print("Current database version:", current_db_version.value)

migrations = [
# Add lists for migrations
# E.g ALTER TABLE drink ADD column price6 float DEFAULT 50
#["ALTER TABLE drink ADD column price6 float DEFAULT 50"]
]

if util.CURRENT_VERSION != len(migrations):
print("Error: No migration available")
exit()

for migration in migrations[int(current_db_version.value):]:
print("Migrating from", current_db_version.value,
"to", int(current_db_version.value)+1)
for statement in migration:
db.execute(text(statement))
current_db_version.value = int(current_db_version.value)+1
db.commit()

print("Migrations complete")
8 changes: 4 additions & 4 deletions backend/database/Queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import os
from sqlalchemy import extract
from difflib import SequenceMatcher
from sqlalchemy import inspect, text
import database.Migrations


class Queries:
Expand All @@ -27,6 +27,7 @@ def __init__(self, db):
self.db.create_all()
if self.session.query(Member).first() is None:
self.create_dummy_data()
database.Migrations.migrate(self.session)

def get_users(self):
members = self.session.query(Member).all()
Expand Down Expand Up @@ -587,12 +588,10 @@ def remove_message(self, message_id):
reminder: Reminder = self.session.query(
Reminder).filter_by(id=message_id).first()


self.session.delete(reminder)

self.session.commit()


def get_username_alias(self, member_id):
member: Member = self.session.query(
Member).filter_by(id=member_id).first()
Expand Down Expand Up @@ -829,7 +828,8 @@ def create_dummy_data(self) -> None:
util.moderator_password)
self.session.add(
Member(name=util.moderator_username, password=hashedPassword, salt=salt))

self.session.add(
KeyValue(key="version", value=util.CURRENT_VERSION))
self.session.commit()

if util.token is not None and util.old_domain is not None:
Expand Down
2 changes: 2 additions & 0 deletions backend/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
OIDC_USER_NEEDS_VERIFICATION = os.environ.get(
"OIDC_USER_NEEDS_VERIFICATION") == "true" if os.environ.get("OIDC_USER_NEEDS_VERIFICATION") else True

CURRENT_VERSION = 0

tempfile_path = "tempfiles"
backup_file_name = "backup.json"

Expand Down

0 comments on commit 6ff0f4f

Please sign in to comment.