summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlly Cope <olly@ollycope.com>2018-08-17 14:27:27 +0000
committerOlly Cope <olly@ollycope.com>2018-08-17 14:27:27 +0000
commit4b55081bba244e2528e76184ba88e8a1bde8293d (patch)
treebe1d3324575ff38ee51edba435ad2b33cca84a71
parent47e43e50557673d857e4e5de90ba39d754ceae61 (diff)
downloadyoyo-4b55081bba244e2528e76184ba88e8a1bde8293d.tar.gz
Do not attempt to apply internal migrations if already up to date
This removes unnecessary calls to backend.lock() which were causing concurrency related tests to fail.
-rw-r--r--yoyo/backends.py11
-rw-r--r--yoyo/internalmigrations/__init__.py9
2 files changed, 12 insertions, 8 deletions
diff --git a/yoyo/backends.py b/yoyo/backends.py
index 0548412..c0a92e7 100644
--- a/yoyo/backends.py
+++ b/yoyo/backends.py
@@ -360,11 +360,12 @@ class DatabaseBackend(object):
"""
if self._internal_schema_updated:
return
- assert not self._in_transaction
- with self.lock():
- internalmigrations.upgrade(self)
- self.connection.commit()
- self._internal_schema_updated = True
+ if internalmigrations.needs_upgrading(self):
+ assert not self._in_transaction
+ with self.lock():
+ internalmigrations.upgrade(self)
+ self.connection.commit()
+ self._internal_schema_updated = True
def is_applied(self, migration):
return migration.hash in self.get_applied_migration_hashes()
diff --git a/yoyo/internalmigrations/__init__.py b/yoyo/internalmigrations/__init__.py
index 30c495f..fee2c49 100644
--- a/yoyo/internalmigrations/__init__.py
+++ b/yoyo/internalmigrations/__init__.py
@@ -10,6 +10,7 @@ from . import v2
#: Mapping of {schema version number: module}
schema_versions = {
+ 0: None,
1: v1,
2: v2,
}
@@ -19,6 +20,10 @@ schema_versions = {
USE_VERSION_TABLE_FROM = 2
+def needs_upgrading(backend):
+ return get_current_version(backend) < max(schema_versions)
+
+
def upgrade(backend, version=None):
"""
Check the currently installed yoyo migrations version and update the
@@ -29,8 +34,6 @@ def upgrade(backend, version=None):
else:
desired_version = version
current_version = get_current_version(backend)
- if current_version is None:
- current_version = 0
with backend.transaction():
while current_version < desired_version:
next_version = current_version + 1
@@ -46,7 +49,7 @@ def get_current_version(backend):
tables = set(backend.list_tables())
version_table = backend.version_table
if backend.migration_table not in tables:
- return None
+ return 0
if version_table not in tables:
return 1
with backend.transaction():