From 08f39e0d22a444edcefafb18f512217887ea268d Mon Sep 17 00:00:00 2001 From: eclipse Date: Tue, 6 May 2025 12:19:12 +0200 Subject: [PATCH] this script corrects wrongly formatted dates in the DB table "Werk"; needs only to be run once, and only if there are bad legacy dates in the DB --- utils/format_dates.py | 74 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100755 utils/format_dates.py diff --git a/utils/format_dates.py b/utils/format_dates.py new file mode 100755 index 0000000..1510108 --- /dev/null +++ b/utils/format_dates.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 +# -*- coding: utf8 -*- + +from flask import Flask +from dotenv import load_dotenv +import os +from flask_sqlalchemy import SQLAlchemy +from sqlalchemy import select +import re + +# initialize db exactly like in the main app +app = Flask(__name__) +load_dotenv() +app.config.from_prefixed_env() +if os.getenv("SQLALCHEMY_DATABASE_DIALECT") == "sqlite": + app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///" + os.path.abspath(app.root_path + "/..") + "/" + os.getenv("SQLALCHEMY_DATABASE_SQLITE_FILENAME") +else: + exit("no SQLite database URI given; exiting") +db = SQLAlchemy() +db.init_app(app) + +with app.app_context(): + db.reflect() + + class Werk(db.Model): + __table__ = db.Model.metadata.tables['Werk'] + + # iterate over ids + for id in range(1, 54): + werk = db.session.get(Werk, id) + if werk is None: + print(f"ID {id}: Werk does not exist") + continue + + # case: None/NULL + if werk.Erscheinungsdatum is None: + print(f"ID {id}: date is None; was normalized to '{werk.Erscheinungsdatum}'") + continue + + # case: empty string + if werk.Erscheinungsdatum == "": + werk.Erscheinungsdatum = None + print(f"ID {id}: date is empty string; was normalized to '{werk.Erscheinungsdatum}'") + continue + + # case: YYYY + if len(werk.Erscheinungsdatum) == 4: + print(f"ID {id}: date matches format YYYY; was normalized to '{werk.Erscheinungsdatum}'") + continue + + # case: YYYY-MM-DD + result = re.search(r"^(\d\d\d\d)-(\d\d)-(\d\d)$", werk.Erscheinungsdatum) + if result is not None: + print(f"ID {id}: date matches format YYYY-MM-DD; was normalized to '{werk.Erscheinungsdatum}'") + continue + + # case: DD.MM.YYYY + result = re.search(r"^(\d\d)\.(\d\d)\.(\d\d\d\d)$", werk.Erscheinungsdatum) + if result is not None: + werk.Erscheinungsdatum = result.group(3) + "-" + result.group(2) + "-" + result.group(1) + print(f"ID {id}: date matches format DD.MM.YYYY; was normalized to '{werk.Erscheinungsdatum}'") + continue + + # case: DD.M.YYYY + result = re.search(r"^(\d\d)\.(\d)\.(\d\d\d\d)$", werk.Erscheinungsdatum) + if result is not None: + werk.Erscheinungsdatum = result.group(3) + "-0" + result.group(2) + "-" + result.group(1) + print(f"ID {id}: date matches format DD.M.YYYY; was normalized to '{werk.Erscheinungsdatum}'") + continue + + print(f"ID {id}: date did not match any format") + + db.session.commit() +