当upgrade失败后,如何回退到之前的迁移版本

 migrate后upgrade失败后的报错:

(vpp_agent_venv3.7) [root@localhost source]# python app-migrate.py db migrate
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected removed index 'ix_in_router_manager_room_id' on 'in_router_manager'
INFO  [alembic.autogenerate.compare] Detected removed foreign key (room_id)(id) on table in_router_manager
INFO  [alembic.autogenerate.compare] Detected removed column 'in_router_manager.room_id'
  Generating /srv/web_develop/vpp_agent_dev/agent/source/migrations/versions/8400cd3396af_.py ... done
(vpp_agent_venv3.7) [root@localhost source]# python app-migrate.py db upgrade
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade 48a794328fe9 -> 8400cd3396af, empty message
Traceback (most recent call last):
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
    context)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 509, in do_execute
    cursor.execute(statement, parameters)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/pymysql/cursors.py", line 170, in execute
    result = self._query(query)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/pymysql/cursors.py", line 328, in _query
    conn.query(q)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/pymysql/connections.py", line 516, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/pymysql/connections.py", line 727, in _read_query_result
    result.read()
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/pymysql/connections.py", line 1066, in read
    first_packet = self.connection._read_packet()
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/pymysql/connections.py", line 683, in _read_packet
    packet.check_error()
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/pymysql/protocol.py", line 220, in check_error
    err.raise_mysql_exception(self._data)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.InternalError: (1553, "Cannot drop index 'ix_in_router_manager_room_id': needed in a foreign key constraint")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "app-migrate.py", line 53, in 
    manager.run()
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/flask_script/__init__.py", line 417, in run
    result = self.handle(argv[0], argv[1:])
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/flask_script/__init__.py", line 386, in handle
    res = handle(*args, **config)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/flask_script/commands.py", line 216, in __call__
    return self.run(*args, **kwargs)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/flask_migrate/__init__.py", line 95, in wrapped
    f(*args, **kwargs)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/flask_migrate/__init__.py", line 280, in upgrade
    command.upgrade(config, revision, sql=sql, tag=tag)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/alembic/command.py", line 254, in upgrade
    script.run_env()
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/alembic/script/base.py", line 427, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/alembic/util/pyfiles.py", line 81, in load_python_file
    module = load_module_py(module_id, path)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/alembic/util/compat.py", line 82, in load_module_py
    spec.loader.exec_module(module)
  File "", line 728, in exec_module
  File "", line 219, in _call_with_frames_removed
  File "migrations/env.py", line 87, in 
    run_migrations_online()
  File "migrations/env.py", line 80, in run_migrations_online
    context.run_migrations()
  File "", line 8, in run_migrations
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/alembic/runtime/environment.py", line 836, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/alembic/runtime/migration.py", line 330, in run_migrations
    step.migration_fn(**kw)
  File "/srv/web_develop/vpp_agent_dev/agent/source/migrations/versions/8400cd3396af_.py", line 21, in upgrade
    op.drop_index('ix_in_router_manager_room_id', table_name='in_router_manager')
  File "", line 8, in drop_index
  File "", line 3, in drop_index
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/alembic/operations/ops.py", line 964, in drop_index
    return operations.invoke(op)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/alembic/operations/base.py", line 319, in invoke
    return fn(self, operation)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/alembic/operations/toimpl.py", line 94, in drop_index
    operation.to_index(operations.migration_context)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/alembic/ddl/impl.py", line 204, in drop_index
    self._exec(schema.DropIndex(index))
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/alembic/ddl/impl.py", line 115, in _exec
    return conn.execute(construct, *multiparams, **params)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 948, in execute
    return meth(self, multiparams, params)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/sqlalchemy/sql/ddl.py", line 68, in _execute_on_connection
    return connection._execute_ddl(self, multiparams, params)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1009, in _execute_ddl
    compiled
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1200, in _execute_context
    context)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1413, in _handle_dbapi_exception
    exc_info
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 265, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 248, in reraise
    raise value.with_traceback(tb)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
    context)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 509, in do_execute
    cursor.execute(statement, parameters)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/pymysql/cursors.py", line 170, in execute
    result = self._query(query)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/pymysql/cursors.py", line 328, in _query
    conn.query(q)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/pymysql/connections.py", line 516, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/pymysql/connections.py", line 727, in _read_query_result
    result.read()
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/pymysql/connections.py", line 1066, in read
    first_packet = self.connection._read_packet()
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/pymysql/connections.py", line 683, in _read_packet
    packet.check_error()
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/pymysql/protocol.py", line 220, in check_error
    err.raise_mysql_exception(self._data)
  File "/srv/vpp_agent_venv3.7/lib/python3.7/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1553, "Cannot drop index 'ix_in_router_manager_room_id': needed in a foreign key constraint") [SQL: '\nDROP INDEX ix_in_router_manager_room_id ON in_router_manager'] (Background on this error at: http://sqlalche.me/e/2j85)
(vpp_agent_venv3.7) [root@localhost source]# 

修改后,再次migrate就会报:ERROR [root] Error: Target database is not up to date.

(vpp_agent_venv3.7) [root@localhost source]# python app-migrate.py db migrate
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
ERROR [root] Error: Target database is not up to date.

查看当前的tag和之前的

(vpp_agent_venv3.7) [root@localhost source]# python app-migrate.py db current
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
48a794328fe9
(vpp_agent_venv3.7) [root@localhost source]# python app-migrate.py db heads
8400cd3396af (head)

恢复之前heads的版本
 

(vpp_agent_venv3.7) [root@localhost source]# python app-migrate.py db stamp heads
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running stamp_revision 48a794328fe9 -> 9573295ce506
(vpp_agent_venv3.7) [root@localhost source]# python app-migrate.py db current
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
9573295ce506 (head)

重新migrate和upgrade
 

(vpp_agent_venv3.7) [root@localhost source]# python app-migrate.py db migrate
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected removed foreign key (room_id)(id) on table in_router_manager
INFO  [alembic.autogenerate.compare] Detected added foreign key (room_id)(id) on table in_router_manager
  Generating /srv/web_develop/vpp_agent_dev/agent/source/migrations/versions/ad397c57d1b1_.py ... done
(vpp_agent_venv3.7) [root@localhost source]# python app-migrate.py db upgrade
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade 9573295ce506 -> ad397c57d1b1, empty message

 

你可能感兴趣的:(数据库)