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
This commit is contained in:
parent
7324d9c230
commit
08f39e0d22
74
utils/format_dates.py
Executable file
74
utils/format_dates.py
Executable file
@ -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()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user