summaryrefslogtreecommitdiff
path: root/src/mailman/database/base.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/mailman/database/base.py')
-rw-r--r--src/mailman/database/base.py59
1 files changed, 37 insertions, 22 deletions
diff --git a/src/mailman/database/base.py b/src/mailman/database/base.py
index 1595007f1..41d9374f9 100644
--- a/src/mailman/database/base.py
+++ b/src/mailman/database/base.py
@@ -142,17 +142,21 @@ class StormBaseDatabase:
database.DEBUG = (as_boolean(config.database.debug)
if debug is None else debug)
self.store = store
- self.load_migrations()
store.commit()
- def load_migrations(self):
- """Load all not-yet loaded migrations."""
+ def load_migrations(self, until=None):
+ """Load schema migrations.
+
+ :param until: Load only the migrations up to the specified timestamp.
+ With default value of None, load all migrations.
+ :type until: string
+ """
migrations_path = config.database.migrations_path
if '.' in migrations_path:
parent, dot, child = migrations_path.rpartition('.')
else:
parent = migrations_path
- child =''
+ child = ''
# If the database does not yet exist, load the base schema.
filenames = sorted(resource_listdir(parent, child))
# Find out which schema migrations have already been loaded.
@@ -168,17 +172,40 @@ class StormBaseDatabase:
parts = module_fn.split('_')
if len(parts) < 2:
continue
- version = parts[1]
+ version = parts[1].strip()
+ if len(version) == 0:
+ # Not a schema migration file.
+ continue
if version in versions:
- # This one is already loaded.
+ log.debug('already migrated to %s', version)
continue
+ if until is not None and version > until:
+ # We're done.
+ break
module_path = migrations_path + '.' + module_fn
__import__(module_path)
upgrade = getattr(sys.modules[module_path], 'upgrade', None)
if upgrade is None:
continue
+ log.debug('migrating db to %s: %s', version, module_path)
upgrade(self, self.store, version, module_path)
+ def load_sql(self, store, sql):
+ """Load the given SQL into the store.
+
+ :param store: The Storm store to load the schema into.
+ :type store: storm.locals.Store`
+ :param sql: The possibly multi-line SQL to load.
+ :type sql: string
+ """
+ # Discard all blank and comment lines.
+ lines = (line for line in sql.splitlines()
+ if line.strip() != '' and line.strip()[:2] != '--')
+ sql = NL.join(lines)
+ for statement in sql.split(';'):
+ if statement.strip() != '':
+ store.execute(statement + ';')
+
def load_schema(self, store, version, filename, module_path):
"""Load the schema from a file.
@@ -199,22 +226,10 @@ class StormBaseDatabase:
"""
if filename is not None:
contents = resource_string('mailman.database.schema', filename)
- # Discard all blank and comment lines.
- lines = (line for line in contents.splitlines()
- if line.strip() != '' and line.strip()[:2] != '--')
- sql = NL.join(lines)
- for statement in sql.split(';'):
- if statement.strip() != '':
- store.execute(statement + ';')
+ self.load_sql(store, contents)
# Add a marker that indicates the migration version being applied.
store.add(Version(component='schema', version=version))
- # Add a marker so that the module name can be found later. This is
- # used by the test suite to reset the database between tests.
- store.add(Version(component=version, version=module_path))
- def _reset(self):
- """See `IDatabase`."""
- from mailman.database.model import ModelMeta
- self.store.rollback()
- ModelMeta._reset(self.store)
- self.store.commit()
+ @staticmethod
+ def _make_temporary():
+ raise NotImplementedError