Skip to content

Instantly share code, notes, and snippets.

@kkirsche
Created June 21, 2021 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkirsche/975f6d5d05eb9a80ea743238162fedaa to your computer and use it in GitHub Desktop.
Save kkirsche/975f6d5d05eb9a80ea743238162fedaa to your computer and use it in GitHub Desktop.
Generate Alembic Slug
import re
# https://github.com/sqlalchemy/alembic/blob/c97d5b3dd6fde31acb8e9e0c67d0ebc54fa0d809/alembic/script/base.py#L682
_slug_re = re.compile(r"\w+")
truncate_slug_length = 40
def generate_slug(message: str) -> str:
slug = "_".join(_slug_re.findall(message or "")).lower()
if len(slug) > truncate_slug_length:
slug = slug[: truncate_slug_length].rsplit("_", 1)[0] + "_"
return slug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment