summaryrefslogtreecommitdiff
path: root/src/mailman/database
diff options
context:
space:
mode:
Diffstat (limited to 'src/mailman/database')
-rw-r--r--src/mailman/database/base.py99
-rw-r--r--src/mailman/database/factory.py6
-rw-r--r--src/mailman/database/model.py15
3 files changed, 66 insertions, 54 deletions
diff --git a/src/mailman/database/base.py b/src/mailman/database/base.py
index a2392bb3a..0bc530e6b 100644
--- a/src/mailman/database/base.py
+++ b/src/mailman/database/base.py
@@ -68,6 +68,16 @@ class SABaseDatabase:
def _prepare(self, url):
pass
+ def _database_exists(self):
+ """Return True if the database exists and is initialized.
+
+ Return False when Mailman needs to create and initialize the
+ underlying database schema.
+
+ Base classes *must* override this.
+ """
+ raise NotImplementedError
+
def _pre_reset(self, store):
"""Clean up method for testing.
@@ -90,11 +100,10 @@ class SABaseDatabase:
log.debug('Database url: %s', url)
self.url = url
self._prepare(url)
- engine = create_engine(url)
- Session = sessionmaker(bind=engine)
- store = Session()
- self.store = store
- store.commit()
+ self.engine = create_engine(url)
+ Session = sessionmaker(bind=self.engine)
+ self.store = Session()
+ self.store.commit()
def load_migrations(self, until=None):
"""Load schema migrations.
@@ -103,45 +112,47 @@ class SABaseDatabase:
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 = ''
- # 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.
- if self._database_exists(self.store):
- versions = set(version.version for version in
- self.store.find(Version, component='schema'))
- else:
- versions = set()
- for filename in filenames:
- module_fn, extension = os.path.splitext(filename)
- if extension != '.py':
- continue
- parts = module_fn.split('_')
- if len(parts) < 2:
- continue
- version = parts[1].strip()
- if len(version) == 0:
- # Not a schema migration file.
- continue
- if version in versions:
- 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)
- self.commit()
+ from mailman.database.model import Model
+ Model.metadata.create_all(self.engine)
+ # migrations_path = config.database.migrations_path
+ # if '.' in migrations_path:
+ # parent, dot, child = migrations_path.rpartition('.')
+ # else:
+ # parent = migrations_path
+ # 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.
+ # if self._database_exists(self.store):
+ # versions = set(version.version for version in
+ # self.store.query(Version, component='schema'))
+ # else:
+ # versions = set()
+ # for filename in filenames:
+ # module_fn, extension = os.path.splitext(filename)
+ # if extension != '.py':
+ # continue
+ # parts = module_fn.split('_')
+ # if len(parts) < 2:
+ # continue
+ # version = parts[1].strip()
+ # if len(version) == 0:
+ # # Not a schema migration file.
+ # continue
+ # if version in versions:
+ # 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)
+ # self.commit()
def load_sql(self, store, sql):
"""Load the given SQL into the store.
diff --git a/src/mailman/database/factory.py b/src/mailman/database/factory.py
index 426d283e1..64fcc242c 100644
--- a/src/mailman/database/factory.py
+++ b/src/mailman/database/factory.py
@@ -54,7 +54,7 @@ class DatabaseFactory:
database = call_name(database_class)
verifyObject(IDatabase, database)
database.initialize()
- #database.load_migrations()
+ database.load_migrations()
database.commit()
return database
@@ -62,10 +62,10 @@ class DatabaseFactory:
def _reset(self):
"""See `IDatabase`."""
- from mailman.database.model import ModelMeta
+ from mailman.database.model import Model
self.store.rollback()
self._pre_reset(self.store)
- ModelMeta._reset(self.store)
+ Model._reset(self)
self._post_reset(self.store)
self.store.commit()
diff --git a/src/mailman/database/model.py b/src/mailman/database/model.py
index 4b8478fc6..0cb60b7cd 100644
--- a/src/mailman/database/model.py
+++ b/src/mailman/database/model.py
@@ -50,13 +50,14 @@ class ModelMeta(object):
ModelMeta._class_registry.add(self)
@staticmethod
- def _reset(store):
- from mailman.config import config
- config.db._pre_reset(store)
+ def _reset(db):
+ Model.metadata.drop_all(db.engine)
+ Model.metadata.create_all(db.engine)
+
# Make sure this is deterministic, by sorting on the storm table name.
- classes = sorted(ModelMeta._class_registry,
- key=attrgetter('__tablename__'))
- for model_class in classes:
- store.query(model_class).delete()
+ # classes = sorted(ModelMeta._class_registry,
+ # key=attrgetter('__tablename__'))
+ # for model_class in classes:
+ # store.query(model_class).delete()
Model = declarative_base(cls=ModelMeta)