weixin_39890652 2020-11-30 11:37
浏览 0

Support for Manual Migrations

Migrated issue, originally created by Glen Fletcher

I'm not sure whether this is a problem with the documentation or the code. However I suspect its the documentation that is at fault.

The Documentation, only talks about automatic migration using change scripts, however this isn't always an suitable for a problem.

I would like to see a clearly defined may of manually altering a specific database, lets say I have a Database connection through SQLAlchemy, open on an interactive console. I then want to be able run commands like those used in the change scripts to alter the database. I can't find how to achieve this in the documentation. It should be possible since the framework is effectively doing that but it is designed to work from the migration environment rather than explaining how to open the connection from an interactive session, and setup the state so I can run the requited commands to alter the database.

Such functivity is important for plugin based projects where plugins may require extra columns or tables in the database, it this case there is no linear series of versions, as every instance of the application will have different plugins and therefore different database migrations.

Please update the documentation to cover such usage (and if necessary update the code to provide necessary support)

该提问来源于开源项目:sqlalchemy/alembic

  • 写回答

6条回答 默认 最新

  • weixin_39890652 2020-11-30 11:37
    关注

    Michael Bayer () wrote:

    hi there -

    This is likely not a common use case, since for manual migrations people just log into their database and type ALTER commands.

    If you want to run op statements in a shell, follow the example at https://alembic.zzzcomputing.com/en/latest/ops.html#alembic.operations.Operations.

    Here it is in context:

    
    Python 3.7.0 (default, Jun 28 2018, 10:59:55) 
    [GCC 8.1.1 20180502 (Red Hat 8.1.1-1)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from alembic.migration import MigrationContext
    >>> from alembic.operations import Operations
    >>> from sqlalchemy import create_engine
    >>> e = create_engine("sqlite://", echo=True)
    >>> conn = e.connect()
    2018-11-17 09:45:59,669 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60)) AS anon_1
    2018-11-17 09:45:59,669 INFO sqlalchemy.engine.base.Engine ()
    2018-11-17 09:45:59,671 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60)) AS anon_1
    2018-11-17 09:45:59,671 INFO sqlalchemy.engine.base.Engine ()
    >>> ctx = MigrationContext.configure(conn)
    >>> op = Operations(ctx)
    >>> from sqlalchemy import Column, Integer
    >>> op.create_table("mytable", Column('x', Integer))
    2018-11-17 09:46:32,202 INFO sqlalchemy.engine.base.Engine 
    CREATE TABLE mytable (
        x INTEGER
    )
    
    
    2018-11-17 09:46:32,202 INFO sqlalchemy.engine.base.Engine ()
    2018-11-17 09:46:32,203 INFO sqlalchemy.engine.base.Engine COMMIT
    Table('mytable', MetaData(bind=None), Column('x', Integer(), table=<mytable>), schema=None)
    
    </mytable>

    does that solve your issue?

    评论

报告相同问题?