~barry/mailman/templatecache

« back to all changes in this revision

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

  • Committer: Barry Warsaw
  • Date: 2012-09-06 00:49:50 UTC
  • Revision ID: barry@list.org-20120906004950-hwj29e6x48bxzub8
Port the schema migration changes for LP: #1024509 to PostgreSQL.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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('@')
 
135
        if at == '':
 
136
            list_id = mailing_list
 
137
        else:
 
138
            list_id = '{0}.{1}'.format(list_name, mail_host)
134
139
        store.execute("""
135
140
            UPDATE mem_backup SET list_id = '{0}'
136
141
            WHERE id = {1};
143
148
 
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 
 
153
        FROM mailinglist;
 
154
        """)
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
176
 
                archive_policy = {0}
177
 
            WHERE id = {1};
178
 
            """.format(archive_policy(archive, archive_private), id))
 
185
                archive_policy = {0},
 
186
                list_id = '{1}'
 
187
            WHERE id = {2};
 
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'):
181
191
        store.execute(
182
192
            'ALTER TABLE mailinglist DROP COLUMN {0};'.format(column))
 
193
    # Now add some indexes that were previously missing.
 
194
    store.execute(
 
195
        'CREATE INDEX ix_mailinglist_list_id ON mailinglist (list_id);')
 
196
    store.execute(
 
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('@')
 
204
        if at == '':
 
205
            list_id = mailing_list
 
206
        else:
 
207
            list_id = '{0}.{1}'.format(list_name, mail_host)
 
208
        store.execute("""
 
209
            UPDATE member SET list_id = '{0}'
 
210
            WHERE id = {1};
 
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)