From e1361e556840a1a710385cd1a09bfa1a9f91742a Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Mon, 10 Oct 2022 20:53:04 +0200 Subject: add a common base class to update scripts This makes it easier to add some useful hooks, such as "self.tell" to output information about the update to the user, and it ensures the pre_alembic/post_alembic methods exist. --- fietsboek/updater/__init__.py | 6 ++++-- fietsboek/updater/script.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 fietsboek/updater/script.py diff --git a/fietsboek/updater/__init__.py b/fietsboek/updater/__init__.py index 1c26715..e81a515 100644 --- a/fietsboek/updater/__init__.py +++ b/fietsboek/updater/__init__.py @@ -23,6 +23,8 @@ TEMPLATE = """\ Date created: {{ date }} \"\"\" +from fietsboek.updater.script import UpdateScript + update_id = {{ "{!r}".format(update_id) }} previous = [ {%- for prev in previous %} @@ -32,7 +34,7 @@ previous = [ alembic_revision = {{ "{!r}".format(alembic_revision) }} -class Up: +class Up(UpdateScript): def pre_alembic(self, config): pass @@ -40,7 +42,7 @@ class Up: pass -class Down: +class Down(UpdateScript): def pre_alembic(self, config): pass diff --git a/fietsboek/updater/script.py b/fietsboek/updater/script.py new file mode 100644 index 0000000..15e1d59 --- /dev/null +++ b/fietsboek/updater/script.py @@ -0,0 +1,32 @@ +# Placed in a separate file to avoid cyclic dependencies +class UpdateScript: + def tell(self, text): + """Output a message to the user. + + This function should be used in update scripts instead of :func:`print` + to ensure the right stream is selected. + + :param text: The text to show to the user. + :type text: str + """ + print(text) + + def pre_alembic(self, config): + """Script that is run before the alembic migration is run. + + This method is to be overridden by subclasses. + + :param config: The app configuration. + :type config: dict + """ + pass + + def post_alembic(self, config): + """Script that is run after the alembic migrations have been run. + + This method is to be overridden by subclasses. + + :param config: The app configuration. + :type config: dict + """ + pass -- cgit v1.2.3