~barry/mailman/events-and-web

« back to all changes in this revision

Viewing changes to src/mailman/database/schema/mm_20121015000000.py

  • Committer: Barry Warsaw
  • Date: 2012-10-16 22:40:12 UTC
  • Revision ID: barry@list.org-20121016224012-xxrd5zgkwdrmh9y4
Database
--------
 * The `ban` table now uses list-ids to cross-reference the mailing list,
   since these cannot change even if the mailing list is moved or renamed.

Interfaces
----------
 * The `IBanManager` is no longer a global utility.  Instead, you adapt an
   `IMailingList` to an `IBanManager` to manage the bans for a specific
   mailing list.  To manage the global bans, adapt ``None``.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2012 by the Free Software Foundation, Inc.
 
2
#
 
3
# This file is part of GNU Mailman.
 
4
#
 
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.
 
9
#
 
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.
 
14
#
 
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/>.
 
17
 
 
18
"""3.0b2 -> 3.0b3 schema migrations.
 
19
 
 
20
* bans.mailing_list -> bans.list_id
 
21
"""
 
22
 
 
23
from __future__ import absolute_import, print_function, unicode_literals
 
24
 
 
25
__metaclass__ = type
 
26
__all__ = [
 
27
    'upgrade',
 
28
    ]
 
29
 
 
30
 
 
31
VERSION = '20121015000000'
 
32
 
 
33
 
 
34
 
 
35
def upgrade(database, store, version, module_path):
 
36
    if database.TAG == 'sqlite':
 
37
        upgrade_sqlite(database, store, version, module_path)
 
38
    else:
 
39
        upgrade_postgres(database, store, version, module_path)
 
40
 
 
41
 
 
42
 
 
43
def _make_listid(fqdn_listname):
 
44
    list_name, at, mail_host = fqdn_listname.partition('@')
 
45
    if at == '':
 
46
        # If there is no @ sign in the value, assume it already contains the
 
47
        # list-id.
 
48
        return fqdn_listname
 
49
    return '{0}.{1}'.format(list_name, mail_host)
 
50
 
 
51
 
 
52
 
 
53
def upgrade_sqlite(database, store, version, module_path):
 
54
    database.load_schema(
 
55
        store, version, 'sqlite_{0}_01.sql'.format(version), module_path)
 
56
    results = store.execute("""
 
57
        SELECT id, mailing_list
 
58
        FROM ban;
 
59
        """)
 
60
    for id, mailing_list in results:
 
61
        # Skip global bans since there's nothing to update.
 
62
        if mailing_list is None:
 
63
            continue
 
64
        store.execute("""
 
65
            UPDATE ban_backup SET list_id = '{0}'
 
66
            WHERE id = {1};
 
67
            """.format(_make_listid(mailing_list), id))
 
68
    # Pivot the backup table to the real thing.
 
69
    store.execute('DROP TABLE ban;')
 
70
    store.execute('ALTER TABLE ban_backup RENAME TO ban;')
 
71
 
 
72
 
 
73
 
 
74
def upgrade_postgres(database, store, version, module_path):
 
75
    # Get the old values from the ban table.
 
76
    results = store.execute('SELECT id, mailing_list FROM ban;')
 
77
    store.execute('ALTER TABLE ban ADD COLUMN list_id TEXT;')
 
78
    for id, mailing_list in results:
 
79
        # Skip global bans since there's nothing to update.
 
80
        if mailing_list is None:
 
81
            continue
 
82
        store.execute("""
 
83
            UPDATE ban SET list_id = '{0}'
 
84
            WHERE id = {1};
 
85
            """.format(_make_listid(mailing_list), id))
 
86
    store.execute('ALTER TABLE ban DROP COLUMN mailing_list;')
 
87
    # Record the migration in the version table.
 
88
    database.load_schema(store, version, None, module_path)