1
# Copyright (C) 2012 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)
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
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
"""3.0b2 -> 3.0b3 schema migrations.
20
* bans.mailing_list -> bans.list_id
23
from __future__ import absolute_import, print_function, unicode_literals
31
VERSION = '20121015000000'
35
def upgrade(database, store, version, module_path):
36
if database.TAG == 'sqlite':
37
upgrade_sqlite(database, store, version, module_path)
39
upgrade_postgres(database, store, version, module_path)
43
def _make_listid(fqdn_listname):
44
list_name, at, mail_host = fqdn_listname.partition('@')
46
# If there is no @ sign in the value, assume it already contains the
49
return '{0}.{1}'.format(list_name, mail_host)
53
def upgrade_sqlite(database, store, version, module_path):
55
store, version, 'sqlite_{0}_01.sql'.format(version), module_path)
56
results = store.execute("""
57
SELECT id, mailing_list
60
for id, mailing_list in results:
61
# Skip global bans since there's nothing to update.
62
if mailing_list is None:
65
UPDATE ban_backup SET list_id = '{0}'
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;')
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:
83
UPDATE ban SET list_id = '{0}'
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)