Skip to content

Commit

Permalink
- Fixed bug where the include_object() filter would not receive
Browse files Browse the repository at this point in the history
the original :class:`.Column` object when evaluating a database-only
column to be dropped; the object would not include the parent
:class:`.Table` nor other aspects of the column that are important
for generating the "downgrade" case where the column is recreated.
fixes #200
  • Loading branch information
zzzeek committed Apr 30, 2014
1 parent 4e0b345 commit bc6971a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
10 changes: 2 additions & 8 deletions alembic/autogenerate/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,10 @@ def _compare_columns(schema, tname, object_filters, conn_table, metadata_table,
log.info("Detected added column '%s.%s'", name, cname)

for cname in set(conn_col_names).difference(metadata_col_names):
rem_col = sa_schema.Column(
cname,
conn_table.c[cname].type,
nullable=conn_table.c[cname].nullable,
server_default=conn_table.c[cname].server_default
)
if _run_filters(rem_col, cname,
if _run_filters(conn_table.c[cname], cname,
"column", True, None, object_filters):
diffs.append(
("remove_column", schema, tname, rem_col)
("remove_column", schema, tname, conn_table.c[cname])
)
log.info("Detected removed column '%s.%s'", name, cname)

Expand Down
10 changes: 10 additions & 0 deletions docs/build/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ Changelog
.. changelog::
:version: 0.6.5

.. change::
:tags: bug, autogenerate
:tickets: 200

Fixed bug where the ``include_object()`` filter would not receive
the original :class:`.Column` object when evaluating a database-only
column to be dropped; the object would not include the parent
:class:`.Table` nor other aspects of the column that are important
for generating the "downgrade" case where the column is recreated.

.. change::
:tags: bug, environment
:tickets: 195
Expand Down
12 changes: 7 additions & 5 deletions tests/test_autogenerate.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ def include_object(obj, name, type_, reflected, compare_to):
assert obj.metadata is not self.m2
else:
assert obj.metadata is self.m2
return name in ("address", "order")
return name in ("address", "order", "user")
elif type_ == "column":
if reflected:
assert obj.table.metadata is not self.m2
Expand All @@ -529,7 +529,6 @@ def include_object(obj, name, type_, reflected, compare_to):
else:
return True


context = MigrationContext.configure(
connection=self.bind.connect(),
opts={
Expand All @@ -545,12 +544,15 @@ def include_object(obj, name, type_, reflected, compare_to):
)
template_args = {}
autogenerate._produce_migration_diffs(context, template_args, set())

template_args['upgrades'] = template_args['upgrades'].replace("u'", "'")
template_args['downgrades'] = template_args['downgrades'].\
replace("u'", "'")
assert "op.create_table('item'" not in template_args['upgrades']
assert "op.create_table('item'" not in template_args['downgrades']

assert "alter_column('user'" not in template_args['upgrades']
assert "alter_column('user'" not in template_args['downgrades']
assert "alter_column('user'" in template_args['upgrades']
assert "alter_column('user'" in template_args['downgrades']
assert "'street'" not in template_args['upgrades']
assert "'street'" not in template_args['downgrades']
assert "alter_column('order'" in template_args['upgrades']
Expand Down Expand Up @@ -743,7 +745,7 @@ def test_render_diffs_extras(self):
server_default=None,
existing_nullable=True,
schema='%(schema)s')
op.add_column('user', sa.Column('pw', sa.VARCHAR(length=50), nullable=True), schema='%(schema)s')
op.add_column('user', sa.Column('pw', sa.VARCHAR(length=50), autoincrement=False, nullable=True), schema='%(schema)s')
op.alter_column('order', 'amount',
existing_type=sa.Numeric(precision=10, scale=2),
type_=sa.NUMERIC(precision=8, scale=2),
Expand Down

0 comments on commit bc6971a

Please sign in to comment.