Skip to content

Commit

Permalink
Added detection of Table prefixes in the create table render
Browse files Browse the repository at this point in the history
Added rendering for the ``Table.prefixes`` element to autogenerate so that
the rendered Python code includes these directives. Pull request courtesy
Rodrigo Ce Moretto.

Fixes: #721
Closes: #734
Pull-request: #734
Pull-request-sha: 4917e19

Change-Id: I1e53500d49784c3ce2672fa9ce82876fa7645ee9
  • Loading branch information
rmoretto authored and zzzeek committed Nov 5, 2020
1 parent 9a11c35 commit 69f3892
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions alembic/autogenerate/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ def _add_table(autogen_context, op):
text += ",\ncomment=%r" % _ident(comment)
for k in sorted(op.kw):
text += ",\n%s=%r" % (k.replace(" ", "_"), op.kw[k])

if table._prefixes:
prefixes = ", ".join("'%s'" % p for p in table._prefixes)
text += ",\nprefixes=[%s]" % prefixes

text += "\n)"
return text

Expand Down
7 changes: 7 additions & 0 deletions docs/build/unreleased/721.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.. change::
:tags: bug, autogenerate
:tickets: 721

Added rendering for the ``Table.prefixes`` element to autogenerate so that
the rendered Python code includes these directives. Pull request courtesy
Rodrigo Ce Moretto.
37 changes: 37 additions & 0 deletions tests/test_autogen_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,43 @@ def test_render_table_w_metadata_schema_override(self):
")",
)

def test_render_table_w_prefixes(self):
m = MetaData()
t = Table(
"test",
m,
Column("id", Integer, primary_key=True),
prefixes=["TEST", "PREFIXES"],
)
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('id', sa.Integer(), nullable=False),"
"sa.PrimaryKeyConstraint('id'),"
"prefixes=['TEST', 'PREFIXES']"
")",
)

def test_render_table_w_prefixes_schema(self):
m = MetaData(schema="foo")
t = Table(
"test",
m,
Column("id", Integer, primary_key=True),
prefixes=["TEST", "PREFIXES"],
)
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('id', sa.Integer(), nullable=False),"
"sa.PrimaryKeyConstraint('id'),"
"schema='foo',"
"prefixes=['TEST', 'PREFIXES']"
")",
)

def test_render_addtl_args(self):
m = MetaData()
t = Table(
Expand Down

0 comments on commit 69f3892

Please sign in to comment.