message: Optional[str],
create_date: "datetime.datetime",
) -> str:
+ 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] + "_"
% {
"rev": rev_id,
"slug": slug,
+ "epoch": epoch,
"year": create_date.year,
"month": create_date.month,
"day": create_date.day,
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
# Uncomment the line below if you want the files to be prepended with date and time
+# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
+# for all available tokens
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
# sys.path path, will be prepended to sys.path if present.
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
# Uncomment the line below if you want the files to be prepended with date and time
+# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
+# for all available tokens
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
# sys.path path, will be prepended to sys.path if present.
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
# Uncomment the line below if you want the files to be prepended with date and time
+# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
+# for all available tokens
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
# sys.path path, will be prepended to sys.path if present.
* ``%%(rev)s`` - revision id
* ``%%(slug)s`` - a truncated string derived from the revision message
+ * ``%%(epoch)s`` - epoch timestamp based on the create date; this makes
+ use of the Python ``datetime.timestamp()`` method to produce an epoch
+ value.
* ``%%(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``
configuration option is also used.
+ .. versionadded:: 1.8 added 'epoch'
+
* ``timezone`` - an optional timezone name (e.g. ``UTC``, ``EST5EDT``, etc.)
that will be applied to the timestamp which renders inside the migration
file's comment as well as within the filename. This option requires installing
--- /dev/null
+.. change::
+ :tags: usecase, commands
+ :tickets: 1027
+
+ Added new token ``epoch`` to the ``file_template`` option, which will
+ populate the integer epoch as determined by ``int(create_date.timestamp())``.
+ Pull request courtesy Caio Carvalho.
"""Test op functions against MSSQL."""
+from __future__ import annotations
from typing import Any
from typing import Dict
from alembic import autogenerate
from alembic import command
+from alembic import testing
from alembic import util
from alembic.environment import EnvironmentContext
from alembic.operations import ops
),
)
+ @testing.combinations(
+ (
+ datetime.datetime(2012, 7, 25, 15, 8, 5, tzinfo=tz.gettz("UTC")),
+ "%s/versions/1343228885_12345_this_is_a_"
+ "message_2012_7_25_15_8_5.py",
+ ),
+ (
+ datetime.datetime(2012, 7, 25, 15, 8, 6, tzinfo=tz.gettz("UTC")),
+ "%s/versions/1343228886_12345_this_is_a_"
+ "message_2012_7_25_15_8_6.py",
+ ),
+ )
+ def test_epoch(self, create_date, expected):
+ 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",
+ )
+ eq_(
+ script._rev_path(
+ script.versions, "12345", "this is a message", create_date
+ ),
+ os.path.abspath(expected % _get_staging_directory()),
+ )
+
def _test_tz(self, timezone_arg, given, expected):
script = ScriptDirectory(
_get_staging_directory(),