Revert "Revert "Added migration to collapse versions of hosted apps""

This reverts commit 4e1cea8726.
This commit is contained in:
Rob Hudson 2012-11-14 21:42:33 -08:00
Родитель 4e1cea8726
Коммит f866ed0439
1 изменённых файлов: 158 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,158 @@
import json
from django.db import connection
import amo
from files.models import File
from versions.models import Version
from mkt.webapps.models import Webapp
def run():
cursor = connection.cursor()
vkey = unicode(Version._meta)
fkey = 'files'
for app in Webapp.uncached.filter(type=amo.ADDON_WEBAPP,
disabled_by_user=False,
is_packaged=False).no_transforms():
print 'Updating app [%s]' % app.id
try:
version = (Version.objects.filter(addon=app)
.no_transforms().latest())
except Version.DoesNotExist:
print 'App [%s] has no version? FIXME!' % app.id
continue
try:
file_ = File.objects.filter(version=version).latest()
except File.DoesNotExist:
file_ = None
old_versions = (app.versions.exclude(pk=version.id)
.no_transforms()
.order_by('-created'))
if not old_versions:
print 'No older versions found.'
continue
for old_version in old_versions:
# Hosted app reviews' version column is always NULL.
# `versioncomments` has no data for apps.
# `applications_versions` has no data for apps.
# VersionLogs and ActivityLogs
cursor.execute('''
SELECT t1.id, t1.version_id, t2.id, t2.arguments, t2.details
FROM log_activity_version_mkt AS t1
JOIN log_activity_mkt AS t2
ON t1.activity_log_id=t2.id
WHERE t1.version_id=%s''', (old_version.id,))
for row in cursor.fetchall():
lav_id, lav_version_id, la_id, la_arguments, la_details = row
arguments = json.loads(la_arguments) if la_arguments else None
details = json.loads(la_details) if la_details else None
do_arguments_save = do_details_save = False
# arguments looks like:
# [{"webapps.webapp": 8}, {"versions.version": 8}]
for _a in arguments:
if vkey in _a:
_a[vkey] = version.id
do_arguments_save = True
# details looks like:
# {"files": [35], "reviewtype": "pending", "comments": "yo"}
if file_ and details and fkey in details:
details[fkey] = [file_.id]
do_details_save = True
if do_arguments_save or do_details_save:
new_a = json.dumps(arguments) if arguments else ''
new_d = json.dumps(details) if details else ''
cursor.execute('''
UPDATE log_activity_mkt
SET arguments=%s, details=%s
WHERE id=%s''', (new_a, new_d, la_id))
if do_arguments_save:
print 'Activity log [%s] arguments updated: %s' % (
la_id, new_a)
if do_details_save:
print 'Activity log [%s] details updated: %s' % (
la_id, new_d)
cursor.execute('''
UPDATE log_activity_version_mkt
SET version_id=%s
WHERE id=%s''', (version.id, lav_id))
print 'Version log [%s] version id updated: %s => %s' % (
lav_id, old_version.id, version.id)
# Copy over important fields if not set on current version.
if not version.releasenotes and old_version.releasenotes:
version.releasenotes = old_version.releasenotes
print 'Copied releasenotes (%s) from old_version [%s].' % (
version.releasenotes, old_version.id)
if not version.approvalnotes and old_version.approvalnotes:
version.approvalnotes = old_version.approvalnotes
print 'Copied approvalnotes (%s) from old_version [%s].' % (
version.approvalnotes, old_version.id)
if not version.reviewed and old_version.reviewed:
version.reviewed = old_version.reviewed
print 'Copied reviewed date (%s) from old version [%s].' % (
version.reviewed, old_version.id)
if (not version.has_editor_comment and
old_version.has_editor_comment):
version.has_editor_comment = old_version.has_editor_comment
print 'Copied editor_comment flag from old version [%s].' % (
old_version.id,)
if (not version.has_info_request and
old_version.has_info_request):
version.has_info_request = old_version.has_info_request
print 'Copied info_request flag from old version [%s].' % (
old_version.id,)
# Delete this version's files and on-disk files.
old_files = File.objects.filter(version_id=old_version.id)
for f in old_files:
print 'Please remove file from file system: %s' % f.file_path
cursor.execute('''
DELETE FROM files
WHERE version_id=%s''', (old_version.id,))
print 'Deleted files attached to version [%s]' % old_version.id
# Delete the version itself.
cursor.execute('''
DELETE FROM versions
WHERE id=%s''', (old_version.id,))
print 'Deleted version [%s]' % old_version.id
# Save version to update any fields that were copied above.
version.save()
# Copy the file reviewed date if not set.
if (version.reviewed and not file_.reviewed and
file_.reviewed != version.reviewed):
file_.update(reviewed=version.reviewed)
# Call `update_version` to set the current_version properly.
app._current_version = None # To avoid missing version errors.
updated = app.update_version()
# Call app.save to invalidate and re-index, etc.
if file_ and updated:
app.save()