Created
July 10, 2016 17:30
-
-
Save paltman/73cf67290df3e92790c29934fa8a56e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Generated by Django 1.9.7 on 2016-07-10 11:32 | |
from __future__ import unicode_literals | |
from collections import namedtuple | |
from django.conf import settings | |
from django.db import migrations | |
def namedtuplefetchall(cursor): | |
""" | |
Return all rows from a cursor as a namedtuple | |
""" | |
desc = cursor.description | |
nt_result = namedtuple("Result", [col[0] for col in desc]) | |
return [nt_result(*row) for row in cursor.fetchall()] | |
def migrate_biblion_to_blog(apps, schema_editor): | |
FeedHit = apps.get_model("blog", "FeedHit") | |
Image = apps.get_model("blog", "Image") | |
Post = apps.get_model("blog", "Post") | |
ReviewComment = apps.get_model("blog", "ReviewComment") | |
Revision = apps.get_model("blog", "Revision") | |
Section = apps.get_model("blog", "Section") | |
db_alias = schema_editor.connection.alias | |
# Create Sections | |
section = Section.objects.using(db_alias).create(name="General", slug="general") | |
# FeedHits | |
with schema_editor.connection.cursor() as cursor: | |
cursor.execute("SELECT request_data, created FROM biblion_feedhit") | |
results = namedtuplefetchall(cursor) | |
for result in results: | |
FeedHit.objects.using(db_alias).create(request_data=result.request_data, created=result.created) | |
# Posts | |
posts = {} | |
primary_images = {} | |
with schema_editor.connection.cursor() as cursor: | |
cursor.execute("SELECT id, title, slug, author_id, markup, teaser_html, content_html, description, tweet_text, created, updated, published, secret_key, view_count, primary_image_id FROM biblion_post") | |
results = namedtuplefetchall(cursor) | |
for result in results: | |
post = Post.objects.using(db_alias).create( | |
title=result.title, | |
slug=result.slug, | |
author_id=result.author_id, | |
markup=result.markup, | |
teaser_html=result.teaser_html, | |
content_html=result.content_html, | |
description=result.description, | |
tweet_text=result.tweet_text, | |
created=result.created, | |
updated=result.updated, | |
published=result.published, | |
secret_key=result.secret_key, | |
view_count=result.view_count, | |
section=section, | |
state=len(settings.PINAX_BLOG_UNPUBLISHED_STATES) + 1 if result.published is not None else 1 # should be 2 # Default is 1: Draft, 2: Published - See PINAX_BLOG_UNPUBLISHED_STATES | |
) | |
posts[result.id] = post | |
if result.primary_image_id is not None: | |
primary_images[result.id] = result.primary_image_id | |
# Images | |
images = {} | |
with schema_editor.connection.cursor() as cursor: | |
cursor.execute("SELECT id, post_id, image_path, url, timestamp FROM biblion_image") | |
results = namedtuplefetchall(cursor) | |
for result in results: | |
image = Image.objects.using(db_alias).create( | |
post=posts[result.post_id], | |
image_path=result.image_path, | |
url=result.url, | |
timestamp=result.timestamp | |
) | |
images[result.id] = image | |
# Revisions | |
with schema_editor.connection.cursor() as cursor: | |
cursor.execute("SELECT post_id, title, teaser, content, author_id, updated, published, view_count FROM biblion_revision") | |
results = namedtuplefetchall(cursor) | |
for result in results: | |
Revision.objects.using(db_alias).create( | |
post=posts[result.post_id], | |
title=result.title, | |
content=result.content, | |
author_id=result.author_id, | |
updated=result.updated, | |
published=result.published, | |
view_count=result.view_count | |
) | |
# ReviewComments | |
with schema_editor.connection.cursor() as cursor: | |
cursor.execute("SELECT post_id, review_text, timestamp, addressed FROM biblion_reviewcomment") | |
results = namedtuplefetchall(cursor) | |
for result in results: | |
ReviewComment.objects.using(db_alias).create( | |
post=posts[result.post_id], | |
review_text=result.review_text, | |
timestamp=result.timestamp, | |
addressed=result.addressed | |
) | |
# Primary Images | |
for post_id, primary_image_id in primary_images.items(): | |
post = posts[post_id] | |
post.primary_image = images[primary_image_id] | |
post.save() | |
class Migration(migrations.Migration): | |
dependencies = [ | |
("blog", "0006_auto_20160321_1527"), | |
] | |
operations = [ | |
migrations.RunPython(migrate_biblion_to_blog) | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment