Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alembic conditional based on database engine

Can alembic run slightly different migration code based on database type? For example, run ALTER TABLE object AUTO_INCREMENT = 6000; only on MySQL, but skip this for SQLite?

Some background: We're using alembic to run migrations. On many dev setups, we use sqlite, and on production we use mysql. On production, we want to start some Primary keys at a particular value, but on dev setups using sqlite, this is not possible and not needed, so we can skip this step.

like image 843
Charles L. Avatar asked Sep 17 '25 20:09

Charles L.


1 Answers

It's possible to get the engine name from the bind. I added the following to my migration:

def upgrade():
    # create table call
    bind = op.get_bind()
    if bind.engine.name == 'mysql':
        op.execute("ALTER TABLE object AUTO_INCREMENT = 5000")
    else:
        print("Skipping setting initial ID value")
like image 60
Charles L. Avatar answered Sep 19 '25 10:09

Charles L.