Skip to content

Commit

Permalink
- make slug length default to 40, but also make it configurable.
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzeek committed Nov 26, 2013
1 parent 39e4b4a commit f8d9f9e
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 9 deletions.
14 changes: 10 additions & 4 deletions alembic/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand All @@ -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"):
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions alembic/templates/generic/alembic.ini.mako
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions alembic/templates/multidb/alembic.ini.mako
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions alembic/templates/pylons/alembic.ini.mako
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions docs/build/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions docs/build/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions tests/test_revision_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f8d9f9e

Please sign in to comment.