summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2015-07-30 14:09:47 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2015-07-30 14:09:47 -0400
commit0d38962a5e29a591e8e7c5788be05e13eef6f452 (patch)
tree2e3825e7f23ba883a7e2fb35c27ce6549b4c48a0
parentd4da2e156c7eec4d9ac5744ec39b263a801e9523 (diff)
downloadalembic-0d38962a5e29a591e8e7c5788be05e13eef6f452.tar.gz
- changelog for pullrequest bitbucket:46; "alembic edit" command
edits migration files using $EDITOR - alter the edit command so that it accepts an argument in the same way as ``alembic show``.
-rw-r--r--alembic/command.py33
-rw-r--r--alembic/util/__init__.py2
-rw-r--r--alembic/util/pyfiles.py11
-rw-r--r--docs/build/changelog.rst11
-rw-r--r--tests/test_command.py52
5 files changed, 91 insertions, 18 deletions
diff --git a/alembic/command.py b/alembic/command.py
index 13d8a57..6b2bc33 100644
--- a/alembic/command.py
+++ b/alembic/command.py
@@ -5,8 +5,6 @@ from .runtime.environment import EnvironmentContext
from . import util
from . import autogenerate as autogen
-import editor
-
def list_templates(config):
"""List available templates"""
@@ -357,14 +355,29 @@ def stamp(config, revision, sql=False, tag=None):
script.run_env()
-def edit(config):
- """Edit the latest ervision"""
+def edit(config, rev):
+ """Edit revision script(s) using $EDITOR"""
script = ScriptDirectory.from_config(config)
- revisions = script.walk_revisions()
- head = next(revisions)
- try:
- editor.edit(head.path)
- except Exception as exc:
- raise util.CommandError('Error executing editor (%s)' % (exc,))
+ if rev == "current":
+ def edit_current(rev, context):
+ if not rev:
+ raise util.CommandError("No current revisions")
+ for sc in script.get_revisions(rev):
+ util.edit(sc.path)
+ return []
+ with EnvironmentContext(
+ config,
+ script,
+ fn=edit_current
+ ):
+ script.run_env()
+ else:
+ revs = script.get_revisions(rev)
+ if not revs:
+ raise util.CommandError(
+ "No revision files indicated by symbol '%s'" % rev)
+ for sc in revs:
+ util.edit(sc.path)
+
diff --git a/alembic/util/__init__.py b/alembic/util/__init__.py
index bd7196c..ff08ad9 100644
--- a/alembic/util/__init__.py
+++ b/alembic/util/__init__.py
@@ -5,7 +5,7 @@ from .messaging import ( # noqa
write_outstream, status, err, obfuscate_url_pw, warn, msg, format_as_comma)
from .pyfiles import ( # noqa
template_to_file, coerce_resource_to_filename, simple_pyc_file_from_path,
- pyc_file_from_path, load_python_file)
+ pyc_file_from_path, load_python_file, edit)
from .sqla_compat import ( # noqa
sqla_07, sqla_079, sqla_08, sqla_083, sqla_084, sqla_09, sqla_092,
sqla_094, sqla_094, sqla_099, sqla_100, sqla_105)
diff --git a/alembic/util/pyfiles.py b/alembic/util/pyfiles.py
index c51e187..c4de071 100644
--- a/alembic/util/pyfiles.py
+++ b/alembic/util/pyfiles.py
@@ -59,6 +59,17 @@ def pyc_file_from_path(path):
return simple_pyc_file_from_path(path)
+def edit(path):
+ """Given a source path, run the EDITOR for it"""
+
+ import editor
+ from . import CommandError
+ try:
+ editor.edit(path)
+ except Exception as exc:
+ raise CommandError('Error executing editor (%s)' % (exc,))
+
+
def load_python_file(dir_, filename):
"""Load a file from the given path as a Python module."""
diff --git a/docs/build/changelog.rst b/docs/build/changelog.rst
index 4b07c87..e69732e 100644
--- a/docs/build/changelog.rst
+++ b/docs/build/changelog.rst
@@ -8,6 +8,17 @@ Changelog
.. change::
:tags: feature, commands
+ :pullreq: bitbucket:46
+
+ Added new command ``alembic edit``. This command takes the same
+ arguments as ``alembic show``, however runs the target script
+ file within $EDITOR. Makes use of the ``python-editor`` library
+ in order to facilitate the handling of $EDITOR with reasonable
+ default behaviors across platforms. Pull request courtesy
+ Michel Albert.
+
+ .. change::
+ :tags: feature, commands
:tickets: 311
Added new multiple-capable argument ``--depends-on`` to the
diff --git a/tests/test_command.py b/tests/test_command.py
index aaeefbc..aa8efa4 100644
--- a/tests/test_command.py
+++ b/tests/test_command.py
@@ -434,21 +434,59 @@ class EditTest(TestBase):
def teardown_class(cls):
clear_staging_env()
- def test_edit_latest(self):
+ def setUp(self):
+ command.stamp(self.cfg, "base")
+
+ def test_edit_head(self):
expected_call_arg = '%s/scripts/versions/%s_revision_c.py' % (
EditTest.cfg.config_args['here'],
EditTest.c
)
- with mock.patch('alembic.command.editor.edit') as edit:
- command.edit(self.cfg)
+ with mock.patch('alembic.util.edit') as edit:
+ command.edit(self.cfg, "head")
+ edit.assert_called_with(expected_call_arg)
+
+ def test_edit_b(self):
+ expected_call_arg = '%s/scripts/versions/%s_revision_b.py' % (
+ EditTest.cfg.config_args['here'],
+ EditTest.b
+ )
+
+ with mock.patch('alembic.util.edit') as edit:
+ command.edit(self.cfg, self.b[0:3])
edit.assert_called_with(expected_call_arg)
def test_edit_with_missing_editor(self):
- with mock.patch('alembic.command.editor.edit') as edit:
- edit.side_effect = OSError('file not found')
+ with mock.patch('editor.edit') as edit_mock:
+ edit_mock.side_effect = OSError("file not found")
assert_raises_message(
util.CommandError,
'file not found',
- command.edit,
- self.cfg)
+ util.edit,
+ "/not/a/file.txt")
+
+ def test_edit_no_revs(self):
+ assert_raises_message(
+ util.CommandError,
+ "No revision files indicated by symbol 'base'",
+ command.edit,
+ self.cfg, "base")
+
+ def test_edit_no_current(self):
+ assert_raises_message(
+ util.CommandError,
+ "No current revisions",
+ command.edit,
+ self.cfg, "current")
+
+ def test_edit_current(self):
+ expected_call_arg = '%s/scripts/versions/%s_revision_b.py' % (
+ EditTest.cfg.config_args['here'],
+ EditTest.b
+ )
+
+ command.stamp(self.cfg, self.b)
+ with mock.patch('alembic.util.edit') as edit:
+ command.edit(self.cfg, "current")
+ edit.assert_called_with(expected_call_arg)