Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CREATE INDEX (possibly other constraints, need to test) missing when table is dropped in the upgrade #468

Closed
sqlalchemy-bot opened this issue Nov 28, 2017 · 3 comments
Labels
autogenerate - detection bug Something isn't working
Milestone

Comments

@sqlalchemy-bot
Copy link

Migrated issue, originally created by Michael Bayer (@zzzeek)

Step 1, env.py:

from sqlalchemy import *

target_metadata = m = MetaData()

Table(
    'a', m, Column('id', Integer, primary_key=True),
    Column('q', Integer, index=True)
)

step 2:

#!

[classic@photon2 alembic]$ PYTHONPATH=~/dev/sqlalchemy/lib/ python -m alembic.config revision -m "rev1" --autogenerate
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added table 'a'
INFO  [alembic.autogenerate.compare] Detected added index 'ix_a_q' on '['q']'
  Generating /home/classic/dev/alembic/foo/versions/54630d783fa6_rev1.py ... done
[classic@photon2 alembic]$ PYTHONPATH=~/dev/sqlalchemy/lib/ python -m alembic.config upgrade head
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade  -> 54630d783fa6, rev1

migration file has:

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('a',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('q', sa.Integer(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_a_q'), 'a', ['q'], unique=False)
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_a_q'), table_name='a')
    op.drop_table('a')
    # ### end Alembic commands ###

now remove the table:

from sqlalchemy import *

target_metadata = m = MetaData()

#Table(
#    'a', m, Column('id', Integer, primary_key=True),
#   Column('q', Integer, index=True)
#)

run again:

[classic@photon2 alembic]$ PYTHONPATH=~/dev/sqlalchemy/lib/ python -m alembic.config revision -m "rev2" --autogenerate
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected removed table u'a'
  Generating /home/classic/dev/alembic/foo/versions/d8d7ee4f2f3b_rev2.py ... done

migration file is missing the index:

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('a')
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('a',
    sa.Column('id', mysql.INTEGER(display_width=11), nullable=False),
    sa.Column('q', mysql.INTEGER(display_width=11), autoincrement=False, nullable=True),
    sa.PrimaryKeyConstraint('id'),
    mysql_default_charset=u'utf8',
    mysql_engine=u'InnoDB'
    )
    # ### end Alembic commands ###

we need to generate MigrateTableOps for tables that are removed during autogenerate.

@sqlalchemy-bot
Copy link
Author

@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

Detect indexes for table that's dropped

Fixed bug where the indexes would not be included in a
migration that was dropping the owning table. The fix
now will also emit DROP INDEX for the indexes ahead of time,
but more importantly will include CREATE INDEX in the
downgrade migration.

Change-Id: I15852bf1cca6de26dd6e7e5ede69bc9e2145c5c0
Fixes: #468

058b2eb

@sqlalchemy-bot
Copy link
Author

Changes by Michael Bayer (@zzzeek):

  • changed status to closed

@sqlalchemy-bot sqlalchemy-bot added autogenerate - detection bug Something isn't working labels Nov 27, 2018
@sqlalchemy-bot sqlalchemy-bot added this to the fasttrack milestone Nov 27, 2018
vvvrrooomm pushed a commit to vvvrrooomm/alembic that referenced this issue Jan 10, 2019
Fixed bug where the indexes would not be included in a
migration that was dropping the owning table.   The fix
now will also emit DROP INDEX for the indexes ahead of time,
but more importantly will include CREATE INDEX in the
downgrade migration.

Change-Id: I15852bf1cca6de26dd6e7e5ede69bc9e2145c5c0
Fixes: sqlalchemy#468
vvvrrooomm pushed a commit to vvvrrooomm/alembic that referenced this issue Jan 10, 2019
Fixed bug where the indexes would not be included in a
migration that was dropping the owning table.   The fix
now will also emit DROP INDEX for the indexes ahead of time,
but more importantly will include CREATE INDEX in the
downgrade migration.

Change-Id: I15852bf1cca6de26dd6e7e5ede69bc9e2145c5c0
Fixes: sqlalchemy#468
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autogenerate - detection bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant