From 06d11b81cf66460b3bacb482fbbd60be56d5b23d Mon Sep 17 00:00:00 2001 From: Caio Carvalho <21188280+lowercase00@users.noreply.github.com> Date: Mon, 2 May 2022 19:26:02 -0300 Subject: [PATCH 1/3] Add epoch as an option for file_template --- alembic/script/base.py | 3 +++ docs/build/tutorial.rst | 1 + tests/test_script_production.py | 38 +++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/alembic/script/base.py b/alembic/script/base.py index ccbf86c9..03d573a6 100644 --- a/alembic/script/base.py +++ b/alembic/script/base.py @@ -782,6 +782,8 @@ def _rev_path( message: Optional[str], create_date: "datetime.datetime", ) -> str: + epoch_reference_date = datetime.datetime(1970, 1, 1) + epoch = int((create_date - epoch_reference_date).total_seconds()) slug = "_".join(_slug_re.findall(message or "")).lower() if len(slug) > self.truncate_slug_length: slug = slug[: self.truncate_slug_length].rsplit("_", 1)[0] + "_" @@ -790,6 +792,7 @@ def _rev_path( % { "rev": rev_id, "slug": slug, + "epoch": epoch, "year": create_date.year, "month": create_date.month, "day": create_date.day, diff --git a/docs/build/tutorial.rst b/docs/build/tutorial.rst index 5bfab916..d7e8946a 100644 --- a/docs/build/tutorial.rst +++ b/docs/build/tutorial.rst @@ -274,6 +274,7 @@ This file contains the following features: * ``%%(rev)s`` - revision id * ``%%(slug)s`` - a truncated string derived from the revision message + * ``%%(epoch)s`` - epoch timestamp based on the create date * ``%%(year)d``, ``%%(month).2d``, ``%%(day).2d``, ``%%(hour).2d``, ``%%(minute).2d``, ``%%(second).2d`` - components of the create date, by default ``datetime.datetime.now()`` unless the ``timezone`` diff --git a/tests/test_script_production.py b/tests/test_script_production.py index 05167f0b..289165cd 100644 --- a/tests/test_script_production.py +++ b/tests/test_script_production.py @@ -184,6 +184,44 @@ def test_args(self): "message_2012_7_25_15_8_5.py" % _get_staging_directory() ), ) + + def test_epoch_name(self): + script = ScriptDirectory( + _get_staging_directory(), + file_template="%(epoch)s_%(rev)s_%(slug)s_" + "%(year)s_%(month)s_" + "%(day)s_%(hour)s_" + "%(minute)s_%(second)s", + ) + create_date = datetime.datetime(2012, 7, 25, 15, 8, 5) + eq_( + script._rev_path( + script.versions, "12345", "this is a message", create_date + ), + os.path.abspath( + "%s/versions/1343228885_12345_this_is_a_" + "message_2012_7_25_15_8_5.py" % _get_staging_directory() + ), + ) + + def test_epoch_formula(self): + script = ScriptDirectory( + _get_staging_directory(), + file_template="%(epoch)s_%(rev)s_%(slug)s_" + "%(year)s_%(month)s_" + "%(day)s_%(hour)s_" + "%(minute)s_%(second)s", + ) + create_date = datetime.datetime(2012, 7, 25, 15, 8, 6) + eq_( + script._rev_path( + script.versions, "12345", "this is a message", create_date + ), + os.path.abspath( + "%s/versions/1343228886_12345_this_is_a_" + "message_2012_7_25_15_8_6.py" % _get_staging_directory() + ), + ) def _test_tz(self, timezone_arg, given, expected): script = ScriptDirectory( From a9305dd8d1a9597fc7d82739001631d872ef75eb Mon Sep 17 00:00:00 2001 From: Caio Carvalho <21188280+lowercase00@users.noreply.github.com> Date: Tue, 3 May 2022 11:32:23 -0300 Subject: [PATCH 2/3] Fix epoch formula & tests --- alembic/script/base.py | 3 +-- tests/test_script_production.py | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/alembic/script/base.py b/alembic/script/base.py index 03d573a6..03171080 100644 --- a/alembic/script/base.py +++ b/alembic/script/base.py @@ -782,8 +782,7 @@ def _rev_path( message: Optional[str], create_date: "datetime.datetime", ) -> str: - epoch_reference_date = datetime.datetime(1970, 1, 1) - epoch = int((create_date - epoch_reference_date).total_seconds()) + epoch = int(create_date.timestamp()) slug = "_".join(_slug_re.findall(message or "")).lower() if len(slug) > self.truncate_slug_length: slug = slug[: self.truncate_slug_length].rsplit("_", 1)[0] + "_" diff --git a/tests/test_script_production.py b/tests/test_script_production.py index 289165cd..fe0b3a76 100644 --- a/tests/test_script_production.py +++ b/tests/test_script_production.py @@ -199,7 +199,7 @@ def test_epoch_name(self): script.versions, "12345", "this is a message", create_date ), os.path.abspath( - "%s/versions/1343228885_12345_this_is_a_" + "%s/versions/1343239685_12345_this_is_a_" "message_2012_7_25_15_8_5.py" % _get_staging_directory() ), ) @@ -218,7 +218,7 @@ def test_epoch_formula(self): script.versions, "12345", "this is a message", create_date ), os.path.abspath( - "%s/versions/1343228886_12345_this_is_a_" + "%s/versions/1343239686_12345_this_is_a_" "message_2012_7_25_15_8_6.py" % _get_staging_directory() ), ) From 27970cd5d4e047cb7aabd161c68b757b2ee3e4c4 Mon Sep 17 00:00:00 2001 From: Caio Carvalho <21188280+lowercase00@users.noreply.github.com> Date: Tue, 3 May 2022 13:18:34 -0300 Subject: [PATCH 3/3] Add timezone to epoch tests --- tests/test_script_production.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_script_production.py b/tests/test_script_production.py index fe0b3a76..91c8a912 100644 --- a/tests/test_script_production.py +++ b/tests/test_script_production.py @@ -193,13 +193,13 @@ def test_epoch_name(self): "%(day)s_%(hour)s_" "%(minute)s_%(second)s", ) - create_date = datetime.datetime(2012, 7, 25, 15, 8, 5) + create_date = datetime.datetime(2012, 7, 25, 15, 8, 5, tzinfo=tz.gettz("UTC")) eq_( script._rev_path( script.versions, "12345", "this is a message", create_date ), os.path.abspath( - "%s/versions/1343239685_12345_this_is_a_" + "%s/versions/1343228885_12345_this_is_a_" "message_2012_7_25_15_8_5.py" % _get_staging_directory() ), ) @@ -212,13 +212,13 @@ def test_epoch_formula(self): "%(day)s_%(hour)s_" "%(minute)s_%(second)s", ) - create_date = datetime.datetime(2012, 7, 25, 15, 8, 6) + create_date = datetime.datetime(2012, 7, 25, 15, 8, 6, tzinfo=tz.gettz("UTC")) eq_( script._rev_path( script.versions, "12345", "this is a message", create_date ), os.path.abspath( - "%s/versions/1343239686_12345_this_is_a_" + "%s/versions/1343228886_12345_this_is_a_" "message_2012_7_25_15_8_6.py" % _get_staging_directory() ), )