Hi,
I'm using SQLAlchemy 1.3 with PostgreSQL. I'm trying to use alembic in SQL-only mode.
I'm using a custom sql-diff script which takes the pg_dump's --schema output and compares it with a clean db setup's one and tells me exactly what changes do I need to make. This way I catch 100% of the db differences, something what no automated tool was able to do in my testing.
My question is how should I use Alembic for this scenario? I'd like to input SQL queries only, no Python at all.
Here is my concept which works but I'm not sure if it's the right way:
env.py:
def run_migrations_online():
engine = engine_from_config(settings, prefix='sqlalchemy.')
connection = engine.connect()
context.configure(connection=connection)
context.run_migrations()
connection.close()
migration script:
commands = [
'CREATE INDEX...',
]
def upgrade():
for command in commands:
try:
op.execute(sa.text(command))
except Exception as e:
print(e)
def downgrade():
pass
It's important that a failing query should never terminate a script, because I might apply some of them (like CREATE INDEX) to production servers in advance. I was also able to make this work by using "autocommit_block()" but this approach seems simpler to me. I'm surprised on the lack of rollback() in the except part though, but still it seems to work, the failed commands are not blocking any other.
Is this good like this?
Regards,
Zsolt