diff options
| author | Barry Warsaw | 2014-09-22 14:47:02 -0400 |
|---|---|---|
| committer | Barry Warsaw | 2014-09-22 14:47:02 -0400 |
| commit | 6b3114c4f0d458db25aa68dc44deeaca5b642ac4 (patch) | |
| tree | 5ba5344e3186dbc3b0f31da6bf9f23bccb7ace4c /src/mailman/database/model.py | |
| parent | f582dbfd193f15aa840228fa4b1c2544ae379a8e (diff) | |
| download | mailman-6b3114c4f0d458db25aa68dc44deeaca5b642ac4.tar.gz mailman-6b3114c4f0d458db25aa68dc44deeaca5b642ac4.tar.zst mailman-6b3114c4f0d458db25aa68dc44deeaca5b642ac4.zip | |
Diffstat (limited to 'src/mailman/database/model.py')
| -rw-r--r-- | src/mailman/database/model.py | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/src/mailman/database/model.py b/src/mailman/database/model.py index d86ebb80e..f8a15162c 100644 --- a/src/mailman/database/model.py +++ b/src/mailman/database/model.py @@ -26,23 +26,31 @@ __all__ = [ import contextlib -from operator import attrgetter from sqlalchemy.ext.declarative import declarative_base from mailman.config import config -class ModelMeta(object): - """Do more magic on table classes.""" +class ModelMeta: + """The custom metaclass for all model base classes. + + This is used in the test suite to quickly reset the database after each + test. It works by iterating over all the tables, deleting each. The test + suite will then recreate the tables before each test. + """ @staticmethod def _reset(db): - meta = Model.metadata - engine = config.db.engine - with contextlib.closing(engine.connect()) as con: - trans = con.begin() - for table in reversed(meta.sorted_tables): - con.execute(table.delete()) - trans.commit() + with contextlib.closing(config.db.engine.connect()) as connection: + transaction = connection.begin() + try: + for table in reversed(Model.metadata.sorted_tables): + connection.execute(table.delete()) + except: + transaction.abort() + raise + else: + transaction.commit() + Model = declarative_base(cls=ModelMeta) |
