datatracker/ietf/community/migrations/0006_copy_docs_m2m_table.py
Henrik Levkowetz 815602351f This is a series of 50 migrations that changes the Document and DocAlias
primary keys from character strings to integers, and makes corresponding code
changes.

This was prompted by database limitations discovered when trying to make
DocAlias use a m2m document field; with 255 long strings as primary keys for
Document and DocAlias this violated the MySQL database limitations.

Changing the primary keys to integers should also improve efficiency.  

Due to the data migrations which create the new integer primary keys and adds
corresponding integer foreign keys matching the previous string foreign keys
in all tables having foreign keys to Document and DocAlias, some of these
migrations take a long time.  The total set of migrations are expected to have
a runtime on the order of 2 hours.
 - Legacy-Id: 16237
2019-06-10 11:32:46 +00:00

57 lines
1.9 KiB
Python

# -*- coding: utf-8 -*-
# Generated by Django 1.11.20 on 2019-05-27 05:56
from __future__ import unicode_literals
from django.db import migrations
import sys, time
from tqdm import tqdm
def forward(apps, schema_editor):
CommunityList = apps.get_model('community', 'CommunityList')
CommunityListDocs = apps.get_model('community', 'CommunityListDocs')
SearchRule = apps.get_model('community', 'SearchRule')
SearchRuleDocs = apps.get_model('community', 'SearchRuleDocs')
# Document id fixup ------------------------------------------------------------
sys.stderr.write('\n')
sys.stderr.write(' %s.%s:\n' % (CommunityList.__name__, 'added_docs'))
for l in tqdm(CommunityList.objects.all()):
l.added_docs.set([ d.document for d in CommunityListDocs.objects.filter(communitylist=l) ])
sys.stderr.write(' %s.%s:\n' % (SearchRule.__name__, 'name_contains_index'))
for r in tqdm(SearchRule.objects.all()):
r.name_contains_index.set([ d.document for d in SearchRuleDocs.objects.filter(searchrule=r) ])
def reverse(apps, schema_editor):
pass
def timestamp(apps, schema_editor):
sys.stderr.write('\n %s' % time.strftime('%Y-%m-%d %H:%M:%S'))
class Migration(migrations.Migration):
dependencies = [
('community', '0005_2_add_docs_m2m_table'),
]
operations = [
#migrations.RunPython(forward, reverse),
# Alternative:
migrations.RunPython(timestamp, timestamp),
migrations.RunSQL(
"INSERT INTO community_communitylist_added_docs SELECT * FROM community_communitylistdocs;",
""),
migrations.RunPython(timestamp, timestamp),
migrations.RunSQL(
"INSERT INTO community_searchrule_name_contains_index SELECT * FROM community_searchruledocs;",
""),
migrations.RunPython(timestamp, timestamp),
]