Don't use `flake8: noqa`.
[mailman.git] / src / mailman / database / postgresql.py
blob5d42c59f8d36f67363f65616815f4b8e80f33cad
1 # Copyright (C) 2011-2016 by the Free Software Foundation, Inc.
3 # This file is part of GNU Mailman.
5 # GNU Mailman is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option)
8 # any later version.
10 # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # more details.
15 # You should have received a copy of the GNU General Public License along with
16 # GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
18 """PostgreSQL database support."""
20 from mailman import public
21 from mailman.database.base import SABaseDatabase
22 from mailman.database.model import Model
23 from sqlalchemy import Integer
26 @public
27 class PostgreSQLDatabase(SABaseDatabase):
28 """Database class for PostgreSQL."""
30 def _post_reset(self, store):
31 """PostgreSQL-specific test suite cleanup.
33 Reset the <tablename>_id_seq.last_value so that primary key ids
34 restart from zero for new tests.
35 """
36 super()._post_reset(store)
37 tables = reversed(Model.metadata.sorted_tables)
38 # Recipe adapted from
39 # http://stackoverflow.com/questions/544791/
40 # django-postgresql-how-to-reset-primary-key
41 for table in tables:
42 for column in table.primary_key:
43 if (column.autoincrement
44 and isinstance(column.type, Integer) # noqa
45 and not column.foreign_keys): # noqa
46 store.execute("""\
47 SELECT setval('"{0}_{1}_seq"', coalesce(max("{1}"), 1),
48 max("{1}") IS NOT null)
49 FROM "{0}";
50 """.format(table, column.name))