From f8d9f9ecc73f03edc16a6c7327b275db7c769222 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 26 Nov 2013 14:08:46 -0500 Subject: [PATCH] - make slug length default to 40, but also make it configurable. --- alembic/script.py | 14 ++++++++++---- alembic/templates/generic/alembic.ini.mako | 4 ++++ alembic/templates/multidb/alembic.ini.mako | 4 ++++ alembic/templates/pylons/alembic.ini.mako | 4 ++++ docs/build/changelog.rst | 7 ++++--- docs/build/tutorial.rst | 9 +++++++++ tests/test_revision_create.py | 18 ++++++++++++++++-- 7 files changed, 51 insertions(+), 9 deletions(-) diff --git a/alembic/script.py b/alembic/script.py index 731d688..cd28d7a 100644 --- a/alembic/script.py +++ b/alembic/script.py @@ -30,10 +30,12 @@ class ScriptDirectory(object): """ - def __init__(self, dir, file_template=_default_file_template): + def __init__(self, dir, file_template=_default_file_template, + truncate_slug_length=40): self.dir = dir self.versions = os.path.join(self.dir, 'versions') self.file_template = file_template + self.truncate_slug_length = truncate_slug_length or 40 if not os.access(dir, os.F_OK): raise util.CommandError("Path doesn't exist: %r. Please use " @@ -53,11 +55,15 @@ def from_config(cls, config): if script_location is None: raise util.CommandError("No 'script_location' key " "found in configuration.") + truncate_slug_length = config.get_main_option("truncate_slug_length") + if truncate_slug_length is not None: + truncate_slug_length = int(truncate_slug_length) return ScriptDirectory( util.coerce_resource_to_filename(script_location), file_template=config.get_main_option( 'file_template', - _default_file_template) + _default_file_template), + truncate_slug_length=truncate_slug_length ) def walk_revisions(self, base="base", head="head"): @@ -221,8 +227,8 @@ def _revision_map(self): def _rev_path(self, rev_id, message, create_date): slug = "_".join(_slug_re.findall(message or "")).lower() - if len(slug) > 60: - slug = slug[:60].rsplit('_', 1)[0] + '_' + if len(slug) > self.truncate_slug_length: + slug = slug[:self.truncate_slug_length].rsplit('_', 1)[0] + '_' filename = "%s.py" % ( self.file_template % { 'rev': rev_id, diff --git a/alembic/templates/generic/alembic.ini.mako b/alembic/templates/generic/alembic.ini.mako index 386e046..f7a5301 100644 --- a/alembic/templates/generic/alembic.ini.mako +++ b/alembic/templates/generic/alembic.ini.mako @@ -7,6 +7,10 @@ script_location = ${script_location} # template used to generate migration files # file_template = %%(rev)s_%%(slug)s +# max length of characters to apply to the +# "slug" field +#truncate_slug_length = 40 + # set to 'true' to run the environment during # the 'revision' command, regardless of autogenerate # revision_environment = false diff --git a/alembic/templates/multidb/alembic.ini.mako b/alembic/templates/multidb/alembic.ini.mako index ab84b8a..bd29898 100644 --- a/alembic/templates/multidb/alembic.ini.mako +++ b/alembic/templates/multidb/alembic.ini.mako @@ -7,6 +7,10 @@ script_location = ${script_location} # template used to generate migration files # file_template = %%(rev)s_%%(slug)s +# max length of characters to apply to the +# "slug" field +#truncate_slug_length = 40 + # set to 'true' to run the environment during # the 'revision' command, regardless of autogenerate # revision_environment = false diff --git a/alembic/templates/pylons/alembic.ini.mako b/alembic/templates/pylons/alembic.ini.mako index 2fa0e68..009b675 100644 --- a/alembic/templates/pylons/alembic.ini.mako +++ b/alembic/templates/pylons/alembic.ini.mako @@ -7,6 +7,10 @@ script_location = ${script_location} # template used to generate migration files # file_template = %%(rev)s_%%(slug)s +# max length of characters to apply to the +# "slug" field +#truncate_slug_length = 40 + # set to 'true' to run the environment during # the 'revision' command, regardless of autogenerate # revision_environment = false diff --git a/docs/build/changelog.rst b/docs/build/changelog.rst index 5c4584c..7caf711 100644 --- a/docs/build/changelog.rst +++ b/docs/build/changelog.rst @@ -11,9 +11,10 @@ Changelog :tags: feature :pullreq: bitbucket:12 - Expanded the size of the filenames generated by "revision" to 60 characters, - and also split on the word rather than the character; courtesy - Frozenball. + Expanded the size of the "slug" generated by "revision" to 40 + characters, which is also configurable by new field + ``truncate_slug_length``; and also split on the word rather than the + character; courtesy Frozenball. .. change:: :tags: bug diff --git a/docs/build/tutorial.rst b/docs/build/tutorial.rst index 97cb285..7da4292 100644 --- a/docs/build/tutorial.rst +++ b/docs/build/tutorial.rst @@ -119,6 +119,10 @@ The file generated with the "generic" configuration looks like:: # template used to generate migration files # file_template = %%(rev)s_%%(slug)s + # max length of characters to apply to the + # "slug" field + #truncate_slug_length = 40 + # set to 'true' to run the environment during # the 'revision' command, regardless of autogenerate # revision_environment = false @@ -196,6 +200,11 @@ This file contains the following features: .. versionadded:: 0.3.6 - added date parameters to ``file_template``. +* ``truncate_slug_length`` - defaults to 40, the max number of characters + to include in the "slug" field. + + .. versionadded:: 0.6.1 - added ``truncate_slug_length`` configuration + * ``sqlalchemy.url`` - A URL to connect to the database via SQLAlchemy. This key is in fact only referenced within the ``env.py`` file that is specific to the "generic" configuration; a file that can be customized by the developer. A multiple diff --git a/tests/test_revision_create.py b/tests/test_revision_create.py index 313be0d..aa85809 100644 --- a/tests/test_revision_create.py +++ b/tests/test_revision_create.py @@ -76,8 +76,22 @@ def test_008_long_name(self): "I'd like it to\nhave\nnewlines") assert os.access( os.path.join(env.dir, 'versions', - '%s_this_is_a_really_long_name_with_lots_of_' - 'characters_and_also_.py' % rid), os.F_OK) + '%s_this_is_a_really_long_name_with_lots_of_.py' % rid), + os.F_OK) + + + def test_009_long_name_configurable(self): + env.truncate_slug_length = 60 + rid = util.rev_id() + env.generate_revision(rid, + "this is a really long name with " + "lots of characters and also " + "I'd like it to\nhave\nnewlines") + assert os.access( + os.path.join(env.dir, 'versions', + '%s_this_is_a_really_long_name_with_lots_' + 'of_characters_and_also_.py' % rid), + os.F_OK) @classmethod