]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Render autogenerate=True if present
authorelad <elad.almos@fundbox.com>
Fri, 29 Jun 2018 15:13:43 +0000 (11:13 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 29 Jun 2018 17:12:55 +0000 (13:12 -0400)
Fixed issue where "autoincrement=True" would not render for a column that
specified it, since as of SQLAlchemy 1.1 this is no longer the default
value for "autoincrement".  Note the behavior only takes effect against the
SQLAlchemy 1.1.0 and higher; for pre-1.1 SQLAlchemy, "autoincrement=True"
does not render as was the case before. Pull request courtesy  Elad Almos.

Change-Id: Ia043c60b4bb6f0520056a08c8d5e2f0c838ddff7
Pull-request: https://bitbucket.org/zzzeek/alembic/pull-requests/80

alembic/autogenerate/render.py
alembic/util/sqla_compat.py
docs/build/unreleased/render_autoinc.rst [new file with mode: 0644]
tests/test_autogen_render.py

index 4fcb4b2e4ee6449679827a77c36894891a7cabf3..abdd277d082b96a08e595837b57f4e14121ed16b 100644 (file)
@@ -1,6 +1,7 @@
 from sqlalchemy import schema as sa_schema, types as sqltypes, sql
 from ..operations import ops
 from ..util import compat
+from ..util import sqla_compat
 import re
 from ..util.compat import string_types
 from .. import util
@@ -533,7 +534,8 @@ def _render_column(column, autogen_context):
         if rendered:
             opts.append(("server_default", rendered))
 
-    if not column.autoincrement:
+    if column.autoincrement is not None and \
+            column.autoincrement != sqla_compat.AUTOINCREMENT_DEFAULT:
         opts.append(("autoincrement", column.autoincrement))
 
     if column.nullable is not None:
index a0991495f69c87d1241e73c85d3f44fa0ee714b2..4620cda3c048acf1372b21bbd3e6b6936a304f88 100644 (file)
@@ -38,6 +38,10 @@ if sqla_08:
 else:
     from sqlalchemy.sql.expression import _TextClause as TextClause
 
+if sqla_110:
+    AUTOINCREMENT_DEFAULT = 'auto'
+else:
+    AUTOINCREMENT_DEFAULT = True
 
 def _table_for_constraint(constraint):
     if isinstance(constraint, ForeignKeyConstraint):
diff --git a/docs/build/unreleased/render_autoinc.rst b/docs/build/unreleased/render_autoinc.rst
new file mode 100644 (file)
index 0000000..50962ca
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, autogenerate
+
+    Fixed issue where "autoincrement=True" would not render for a column that
+    specified it, since as of SQLAlchemy 1.1 this is no longer the default
+    value for "autoincrement".  Note the behavior only takes effect against the
+    SQLAlchemy 1.1.0 and higher; for pre-1.1 SQLAlchemy, "autoincrement=True"
+    does not render as was the case before. Pull request courtesy  Elad Almos.
index b122e3785dab63a27db7079e881c3fe8808c100b..a9bbf4d3a20d9c8c9634ec40ab2fca8a50f7c210 100644 (file)
@@ -924,6 +924,24 @@ class AutogenRenderTest(TestBase):
             "sa.PrimaryKeyConstraint('x'))"
         )
 
+    @config.requirements.fail_before_sqla_110
+    def test_render_table_w_autoincrement(self):
+        m = MetaData()
+        t = Table(
+            'test', m,
+            Column('id1', Integer, primary_key=True),
+            Column('id2', Integer, primary_key=True, autoincrement=True))
+        op_obj = ops.CreateTableOp.from_table(t)
+        eq_ignore_whitespace(
+            autogenerate.render_op_text(self.autogen_context, op_obj),
+            "op.create_table('test',"
+            "sa.Column('id1', sa.Integer(), nullable=False),"
+            "sa.Column('id2', sa.Integer(), autoincrement=True, "
+            "nullable=False),"
+            "sa.PrimaryKeyConstraint('id1', 'id2')"
+            ")"
+        )
+
     def test_render_add_column(self):
         op_obj = ops.AddColumnOp(
             "foo", Column("x", Integer, server_default="5"))