I'm using yoyo migrations to modify DB schema. I want to apply/rollback to Postgre DB not all "migrations" in migrations folder, but only those "migrations", that I selected.
For example, if I have 001.test.py and 002.test.py and I want to apply/rollback only 002.test.py, how can I do it? In particular how to do this stuff in python code, not in shell?
Yoyo's manual gives example only for execution of ALL migrations at once:
from yoyo import read_migrations, get_backend
backend = get_backend('postgres://postgres:postgres@localhost/db_test')
migrations = read_migrations('/home/dfialkovskiy/dev/migrations')
backend.apply_migrations(backend.to_apply(migrations))
backend.rollback_migrations(migrations)
I think I need something like this, to choose migration script which to apply:
backend.apply_migrations(backend.to_apply(migrations[1]))
backend.rollback_migrations(migrations[0])
(this example doesn't work obviously)
That is actually a real problem with yoyo migrations I faced the same problem but I overcome this with the code given below, this solves rolling back or applying specific migration. This example does it for applying migration only but it can be applied to roll back in a similar way.
backend = get_backend('postgres://postgres:postgres@localhost/db_test')
migrations = read_migrations('/home/dfialkovskiy/dev/migrations')
migrations_to_apply = backend.to_apply(migrations)
for pending_migration in migrations_to_apply:
if pending_migration.id == os.path.splitext(sys.argv[1])[0].split('/')[-1]:
backend.apply_one(pending_migration)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With