]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Replace use of utcnow() with now() for Python 3.13 compatibility, Fixes #1643
authorJens Troeger <jens.troeger@light-speed.de>
Tue, 20 May 2025 21:01:45 +0000 (17:01 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 20 May 2025 21:44:39 +0000 (17:44 -0400)
Fixed issue where use of deprecated ``utcnow()`` function would generate
warnings.  Has been replaced with ``now(UTC)``.  Pull request courtesy
Jens Tröger.

Fixes: #1643
Closes: #1644
Pull-request: https://github.com/sqlalchemy/alembic/pull/1644
Pull-request-sha: 1ddb3caa4a8bc7487d070d39c24d5e5e609e0688

Change-Id: Ide0f830d33529c5d4d9540b5e043954f440f9016

alembic/script/base.py
docs/build/unreleased/1643.rst [new file with mode: 0644]
tests/test_script_production.py

index 34dd461f7dc3f9bb2d6a68826e45097be78205f7..8c5d763f8312a99c9f0921cb17935433dcdcc653 100644 (file)
@@ -602,11 +602,10 @@ class ScriptDirectory:
                     raise util.CommandError(
                         "Can't locate timezone: %s" % self.timezone
                     ) from None
-            create_date = (
-                datetime.datetime.utcnow()
-                .replace(tzinfo=datetime.timezone.utc)
-                .astimezone(tzinfo)
-            )
+
+            create_date = datetime.datetime.now(
+                tz=datetime.timezone.utc
+            ).astimezone(tzinfo)
         else:
             create_date = datetime.datetime.now()
         return create_date
diff --git a/docs/build/unreleased/1643.rst b/docs/build/unreleased/1643.rst
new file mode 100644 (file)
index 0000000..1a9b9b2
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: bug, environment
+    :tickets: 1643
+
+    Fixed issue where use of deprecated ``utcnow()`` function would generate
+    warnings.  Has been replaced with ``now(UTC)``.  Pull request courtesy
+    Jens Tröger.
index 60e1998fb41134eb838f95f4a534e05cb06faa9c..10493579b943b020e184c0ba92531b53daf955f4 100644 (file)
@@ -235,7 +235,13 @@ class ScriptNamingTest(TestBase):
         with mock.patch(
             "alembic.script.base.datetime",
             mock.Mock(
-                datetime=mock.Mock(utcnow=lambda: given, now=lambda: given),
+                datetime=mock.Mock(
+                    now=lambda tz=None: (
+                        given.replace(tzinfo=datetime.timezone.utc)
+                        if timezone_arg
+                        else given
+                    )
+                ),
                 timezone=datetime.timezone,
             ),
         ):
@@ -276,6 +282,19 @@ class ScriptNamingTest(TestBase):
             ),
         )
 
+    def test_generates_a_date(self):
+        """test #1643"""
+        script = ScriptDirectory(
+            _get_staging_directory(),
+            file_template="%(rev)s_%(slug)s_"
+            "%(year)s_%(month)s_"
+            "%(day)s_%(hour)s_"
+            "%(minute)s_%(second)s",
+            timezone="utc",
+        )
+        # generates a date with no warnings
+        eq_(script._generate_create_date(), mock.ANY)
+
     def test_default_tz(self):
         self._test_tz(
             None,