Skip to content

Commit

Permalink
feat: also render if_not_exists for CreateTableOp and if_exists for D…
Browse files Browse the repository at this point in the history
…ropTableOp
  • Loading branch information
lachaib committed Sep 13, 2024
1 parent e6165c8 commit 0e7deeb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
7 changes: 7 additions & 0 deletions alembic/autogenerate/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ def _add_table(autogen_context: AutogenContext, op: ops.CreateTableOp) -> str:
prefixes = ", ".join("'%s'" % p for p in table._prefixes)
text += ",\nprefixes=[%s]" % prefixes

if op.if_not_exists is not None:
text += ",\nif_not_exists=%r" % bool(op.if_not_exists)

text += "\n)"
return text

Expand All @@ -291,6 +294,10 @@ def _drop_table(autogen_context: AutogenContext, op: ops.DropTableOp) -> str:
}
if op.schema:
text += ", schema=%r" % _ident(op.schema)

if op.if_exists is not None:
text += ", if_exists=%r" % bool(op.if_exists)

text += ")"
return text

Expand Down
4 changes: 2 additions & 2 deletions docs/build/unreleased/1446.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
:tags: usecase, autogenerate
:tickets: 151

Render `if_exists` and `if_not_exists` in `CreateIndexOp` and `DropIndexOp` in autogenerate context.
Pull request courtesy of Louis-Amaury Chaib
Render `if_exists` and `if_not_exists` in `CreateTableOp`, `CreateIndexOp`, `DropTableOp` and `DropIndexOp` in autogenerate context.
Pull request courtesy of Louis-Amaury Chaib (@lachaib).
40 changes: 40 additions & 0 deletions tests/test_autogen_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,29 @@ def test_render_addtl_args(self):
"mysql_engine='InnoDB',sqlite_autoincrement=True)",
)

def test_render_if_not_exists(self):
m = MetaData()
t = Table(
"test",
m,
Column("id", Integer, primary_key=True),
Column("q", Integer, ForeignKey("bar.address.id")),
sqlite_autoincrement=True,
mysql_engine="InnoDB",
)
op_obj = ops.CreateTableOp.from_table(t)
op_obj.if_not_exists = True
eq_ignore_whitespace(
autogenerate.render_op_text(self.autogen_context, op_obj),
"op.create_table('test',"
"sa.Column('id', sa.Integer(), nullable=False),"
"sa.Column('q', sa.Integer(), nullable=True),"
"sa.ForeignKeyConstraint(['q'], ['bar.address.id'], ),"
"sa.PrimaryKeyConstraint('id'),"
"mysql_engine='InnoDB',sqlite_autoincrement=True,"
"if_not_exists=True)",
)

def test_render_drop_table(self):
op_obj = ops.DropTableOp.from_table(Table("sometable", MetaData()))
eq_ignore_whitespace(
Expand All @@ -1033,6 +1056,23 @@ def test_render_drop_table_w_schema(self):
"op.drop_table('sometable', schema='foo')",
)

def test_render_drop_table_if_exists(self):
m = MetaData()
t = Table(
"test",
m,
Column("id", Integer, primary_key=True),
Column("q", Integer, ForeignKey("bar.address.id")),
sqlite_autoincrement=True,
mysql_engine="InnoDB",
)
op_obj = ops.DropTableOp.from_table(t)
op_obj.if_exists = True
eq_ignore_whitespace(
autogenerate.render_op_text(self.autogen_context, op_obj),
"op.drop_table('test', if_exists=True)",
)

def test_render_table_no_implicit_check(self):
m = MetaData()
t = Table("test", m, Column("x", Boolean()))
Expand Down

0 comments on commit 0e7deeb

Please sign in to comment.