]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
- amending d81619b50b9df7ff4458:
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 20 Oct 2014 18:24:41 +0000 (14:24 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 20 Oct 2014 18:24:41 +0000 (14:24 -0400)
Revision files are now written out using the ``'wb'`` modifier to
``open()``, since Mako reads the templates with ``'rb'``, thus preventing
CRs from being doubled up as has been observed on windows.  The encoding
of the output now defaults to 'utf-8', which can be configured using
a newly added config file parameter ``output_encoding``.
fixes #234

alembic/script.py
alembic/templates/generic/alembic.ini.mako
alembic/templates/multidb/alembic.ini.mako
alembic/templates/pylons/alembic.ini.mako
alembic/util.py
docs/build/changelog.rst
docs/build/tutorial.rst
tests/test_config.py

index 69509794ddbe73fc638c74a2903c18af9d62e20d..9d336716889c9d7c005463d0a175ee3dfd3f954f 100644 (file)
@@ -36,12 +36,13 @@ class ScriptDirectory(object):
 
     def __init__(self, dir, file_template=_default_file_template,
                  truncate_slug_length=40,
-                 sourceless=False):
+                 sourceless=False, output_encoding="utf-8"):
         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
         self.sourceless = sourceless
+        self.output_encoding = output_encoding
 
         if not os.access(dir, os.F_OK):
             raise util.CommandError("Path doesn't exist: %r.  Please use "
@@ -70,7 +71,8 @@ class ScriptDirectory(object):
                 'file_template',
                 _default_file_template),
             truncate_slug_length=truncate_slug_length,
-            sourceless=config.get_main_option("sourceless") == "true"
+            sourceless=config.get_main_option("sourceless") == "true",
+            output_encoding=config.get_main_option("output_encoding", "utf-8")
         )
 
     def walk_revisions(self, base="base", head="head"):
@@ -319,6 +321,7 @@ class ScriptDirectory(object):
                     util.template_to_file,
                     src,
                     dest,
+                    self.output_encoding,
                     **kw
                     )
 
index a738a24accb71437123bee9ada16ab70308844bb..90037d77792597bf98a64229f0fcd2309ff09ec3 100644 (file)
@@ -20,6 +20,10 @@ script_location = ${script_location}
 # versions/ directory
 # sourceless = false
 
+# the output encoding used when revision files
+# are written from script.py.mako
+# output_encoding = utf-8
+
 sqlalchemy.url = driver://user:pass@localhost/dbname
 
 
index 132b2462e5f704c1a7a281dc075a2e00e7676235..ced3558d955d33362ba52bacba8b58c51e1221db 100644 (file)
@@ -20,6 +20,10 @@ script_location = ${script_location}
 # versions/ directory
 # sourceless = false
 
+# the output encoding used when revision files
+# are written from script.py.mako
+# output_encoding = utf-8
+
 databases = engine1, engine2
 
 [engine1]
index 771c0273aa851b08736c762cee11f5bab3f339a5..6a5f206e06ccc286770769f2e19d37b855f95dd4 100644 (file)
@@ -20,6 +20,10 @@ script_location = ${script_location}
 # versions/ directory
 # sourceless = false
 
+# the output encoding used when revision files
+# are written from script.py.mako
+# output_encoding = utf-8
+
 pylons_config_file = ./development.ini
 
 # that's it !
\ No newline at end of file
index 9d48ffb96bdfc8c408c5b36d39cfdc11f516e1cc..d261d09d5e27a9f986fba7180334cd85bc714431 100644 (file)
@@ -57,10 +57,11 @@ except (ImportError, IOError):
     TERMWIDTH = None
 
 
-def template_to_file(template_file, dest, **kw):
+def template_to_file(template_file, dest, output_encoding, **kw):
     with open(dest, 'wb') as f:
+        template = Template(filename=template_file)
         f.write(
-            Template(filename=template_file).render(**kw)
+            template.render_unicode(**kw).encode(output_encoding)
         )
 
 
index 5b135687b1e153a06f41b9b9983719f52d480d4c..dc87462e754f3a4f4a2c9fddabb05d478732015d 100644 (file)
@@ -11,7 +11,9 @@ Changelog
 
       Revision files are now written out using the ``'wb'`` modifier to
       ``open()``, since Mako reads the templates with ``'rb'``, thus preventing
-      CRs from being doubled up as has been observed on windows.
+      CRs from being doubled up as has been observed on windows.  The encoding
+      of the output now defaults to 'utf-8', which can be configured using
+      a newly added config file parameter ``output_encoding``.
 
     .. change::
       :tags: bug, operations
index a0fd0ec202d29c03d75428479adcea225615e91a..82e7284b0c6ec56f59273df0ef3ece871837219c 100644 (file)
@@ -132,6 +132,10 @@ The file generated with the "generic" configuration looks like::
     # versions/ directory
     # sourceless = false
 
+    # the output encoding used when revision files
+    # are written from script.py.mako
+    # output_encoding = utf-8
+
     sqlalchemy.url = driver://user:pass@localhost/dbname
 
     # Logging configuration
@@ -228,6 +232,11 @@ This file contains the following features:
 
   .. versionadded:: 0.6.4
 
+* ``output_encoding`` - the encoding to use when Alembic writes the
+  ``script.py.mako`` file into a new migration file.  Defaults to ``'utf-8'``.
+
+  .. versionadded:: 0.7.0
+
 * ``[loggers]``, ``[handlers]``, ``[formatters]``, ``[logger_*]``, ``[handler_*]``,
   ``[formatter_*]`` - these sections are all part of Python's standard logging configuration,
   the mechanics of which are documented at `Configuration File Format <http://docs.python.org/library/logging.config.html#configuration-file-format>`_.
index 1d03b18fcba26ebc2bc08afbde6e2b7e87111a23..941504c0c34a5cd6341ee09dd644ca2dc4c07841 100644 (file)
@@ -9,6 +9,8 @@ from alembic.testing.mock import Mock, call
 
 from alembic.testing import eq_, assert_raises_message
 from alembic.testing.fixtures import capture_db
+from alembic.testing.env import _no_sql_testing_config, clear_staging_env,\
+    staging_env
 
 
 class ConfigTest(TestBase):
@@ -46,7 +48,7 @@ class ConfigTest(TestBase):
         )
 
 
-class OutputEncodingTest(TestBase):
+class StdoutOutputEncodingTest(TestBase):
 
     def test_plain(self):
         stdout = Mock(encoding='latin-1')
@@ -74,3 +76,21 @@ class OutputEncodingTest(TestBase):
             stdout.mock_calls,
             [call.write('m?il x y'), call.write('\n')]
         )
+
+
+class TemplateOutputEncodingTest(TestBase):
+    def setUp(self):
+        staging_env()
+        self.cfg = _no_sql_testing_config()
+
+    def tearDown(self):
+        clear_staging_env()
+
+    def test_default(self):
+        script = ScriptDirectory.from_config(self.cfg)
+        eq_(script.output_encoding, 'utf-8')
+
+    def test_setting(self):
+        self.cfg.set_main_option('output_encoding', 'latin-1')
+        script = ScriptDirectory.from_config(self.cfg)
+        eq_(script.output_encoding, 'latin-1')