131
131
# Now, do the member table.
132
132
results = store.execute('SELECT id, mailing_list FROM member;')
133
133
for id, mailing_list in results:
134
list_name, at, mail_host = mailing_list.partition('@')
136
list_id = mailing_list
138
list_id = '{0}.{1}'.format(list_name, mail_host)
134
139
store.execute("""
135
140
UPDATE mem_backup SET list_id = '{0}'
144
149
def upgrade_postgres(database, store, version, module_path):
145
150
# Get the old values from the mailinglist table.
146
results = store.execute(
147
'SELECT id, archive, archive_private FROM mailinglist;')
151
results = store.execute("""
152
SELECT id, archive, archive_private, list_name, mail_host
148
155
# Do the simple renames first.
149
156
store.execute("""
150
157
ALTER TABLE mailinglist
166
173
'ALTER TABLE mailinglist DROP COLUMN {0};'.format(column))
167
174
# Now do the trickier collapsing of values. Add the new columns.
168
175
store.execute('ALTER TABLE mailinglist ADD COLUMN archive_policy INTEGER;')
176
store.execute('ALTER TABLE mailinglist ADD COLUMN list_id TEXT;')
169
177
# Query the database for the old values of archive and archive_private in
170
178
# each column. Then loop through all the results and update the new
171
179
# archive_policy from the old values.
172
180
for value in results:
173
id, archive, archive_private = value
181
id, archive, archive_private, list_name, mail_host = value
182
list_id = '{0}.{1}'.format(list_name, mail_host)
174
183
store.execute("""
175
184
UPDATE mailinglist SET
178
""".format(archive_policy(archive, archive_private), id))
185
archive_policy = {0},
188
""".format(archive_policy(archive, archive_private), list_id, id))
179
189
# Now drop the old columns.
180
190
for column in ('archive', 'archive_private'):
182
192
'ALTER TABLE mailinglist DROP COLUMN {0};'.format(column))
193
# Now add some indexes that were previously missing.
195
'CREATE INDEX ix_mailinglist_list_id ON mailinglist (list_id);')
197
'CREATE INDEX ix_mailinglist_fqdn_listname '
198
'ON mailinglist (list_name, mail_host);')
199
# Now, do the member table.
200
results = store.execute('SELECT id, mailing_list FROM member;')
201
store.execute('ALTER TABLE member ADD COLUMN list_id TEXT;')
202
for id, mailing_list in results:
203
list_name, at, mail_host = mailing_list.partition('@')
205
list_id = mailing_list
207
list_id = '{0}.{1}'.format(list_name, mail_host)
209
UPDATE member SET list_id = '{0}'
211
""".format(list_id, id))
212
store.execute('ALTER TABLE member DROP COLUMN mailing_list;')
183
213
# Record the migration in the version table.
184
214
database.load_schema(store, version, None, module_path)