81 lines
2.5 KiB
Python
Executable File
81 lines
2.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# -*- coding: utf8 -*-
|
|
|
|
"""
|
|
Helper script that normalizes all existing date entries in the DB's "Werk" table to the format "YYYY[-MM[-DD]]".
|
|
"""
|
|
|
|
|
|
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()
|
|
|