diff options
author | Olly Cope <olly@ollycope.com> | 2018-01-18 13:12:15 +0000 |
---|---|---|
committer | Olly Cope <olly@ollycope.com> | 2018-01-18 13:12:15 +0000 |
commit | 516eae8368f667df8ad55b883efbc44a7dca9f78 (patch) | |
tree | 085aefce0074632df38262af266b39879c06eab6 | |
parent | 7cd7efb123ced24ab1226f9ed4bf3207618bf7e6 (diff) | |
download | yoyo-516eae8368f667df8ad55b883efbc44a7dca9f78.tar.gz |
Move lock acquisition up to script entrypoint level
Locking the part of the code that applies the migrations isn't enough:
the code that chooses which migrations to apply/rollback also needs to be locked,
otherwise concurrent processes will still try to apply the same migrations.
-rw-r--r-- | yoyo/backends.py | 11 | ||||
-rwxr-xr-x | yoyo/scripts/migrate.py | 27 |
2 files changed, 21 insertions, 17 deletions
diff --git a/yoyo/backends.py b/yoyo/backends.py index f6a3f36..6af931d 100644 --- a/yoyo/backends.py +++ b/yoyo/backends.py @@ -309,12 +309,11 @@ class DatabaseBackend(object): """ if not migrations: return - with self.lock_migration_table(): - for m in migrations: - try: - self.apply_one(m, force=force) - except exceptions.BadMigration: - continue + for m in migrations: + try: + self.apply_one(m, force=force) + except exceptions.BadMigration: + continue def run_post_apply(self, migrations, force=False): """ diff --git a/yoyo/scripts/migrate.py b/yoyo/scripts/migrate.py index c18d02e..bb3117c 100755 --- a/yoyo/scripts/migrate.py +++ b/yoyo/scripts/migrate.py @@ -160,33 +160,38 @@ def get_migrations(args, backend): def apply(args, config): backend = get_backend(args, config) - migrations = get_migrations(args, backend) - backend.apply_migrations(migrations, args.force) + with backend.lock_migration_table(): + migrations = get_migrations(args, backend) + backend.apply_migrations(migrations, args.force) def reapply(args, config): backend = get_backend(args, config) - migrations = get_migrations(args, backend) - backend.rollback_migrations(migrations, args.force) - backend.apply_migrations(migrations, args.force) + with backend.lock_migration_table(): + migrations = get_migrations(args, backend) + backend.rollback_migrations(migrations, args.force) + backend.apply_migrations(migrations, args.force) def rollback(args, config): backend = get_backend(args, config) - migrations = get_migrations(args, backend) - backend.rollback_migrations(migrations, args.force) + with backend.lock_migration_table(): + migrations = get_migrations(args, backend) + backend.rollback_migrations(migrations, args.force) def mark(args, config): backend = get_backend(args, config) - migrations = get_migrations(args, backend) - backend.mark_migrations(migrations) + with backend.lock_migration_table(): + migrations = get_migrations(args, backend) + backend.mark_migrations(migrations) def unmark(args, config): backend = get_backend(args, config) - migrations = get_migrations(args, backend) - backend.unmark_migrations(migrations) + with backend.lock_migration_table(): + migrations = get_migrations(args, backend) + backend.unmark_migrations(migrations) def prompt_migrations(backend, migrations, direction): |