marcosschroh/yoyo-database-migrations

Apply/Rollback N migrations from python code

marcosschroh opened this issue · 0 comments

Now is possible to apply and rollback all the migration from python code:

from yoyo import read_migrations
from yoyo import get_backend

backend = get_backend('postgres://myuser@localhost/mydatabase')
migrations = read_migrations('path/to/migrations')

with backend.lock():
    backend.apply_migrations(backend.to_apply(migrations))
    backend. rollback_migrations(backend. to_rollback(migrations))

Would be nice to specify the number of migrations that should be applied, for example:

So, if there are 10 migrations and 2 were applied, if we execute the command:

backend.apply_migrations(backend.to_apply(migrations), count=3)

The current head should be at 5, means that 5 migrations were applied and 5 are left. The same logic with rollback

backend. rollback_migrations(backend. to_rollback(migrations), count=1)

After that the head should be at 4.