Commit 10bd9e8a by Matt Drayer Committed by Jonathan Piacenti

mattdrayer/api-org-migration: Moved api_manager data migration work to orgs app

parent 777e2b97
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import datetime import datetime
import logging
from south.db import db from south.db import db
from south.v2 import SchemaMigration from south.v2 import SchemaMigration
from django.db import models from django.db import connection, models, transaction
from organizations.models import Organization
log = logging.getLogger(__name__)
class Migration(SchemaMigration): class Migration(SchemaMigration):
def print_message(self, msg):
print msg
log.info(msg)
def forwards(self, orm): def forwards(self, orm):
# Deleting model 'Organization' if not db.dry_run:
db.delete_table('api_manager_organization') existing_entries = Organization.objects.all().count()
self.print_message('EXISTING ENTRIES: {}'.format(existing_entries))
if existing_entries == 0:
try:
cursor = connection.cursor()
cursor.execute('INSERT INTO organizations_organization SELECT * from api_manager_organization')
log_msg = 'organizations entries moved from api_manager to organizations app'
self.print_message(log_msg)
cursor.execute('INSERT INTO organizations_organization_workgroups '
'SELECT * from api_manager_organization_workgroups')
log_msg = 'organization_workgroups entries moved from api_manager to organizations app'
self.print_message(log_msg)
# Removing M2M table for field users on 'Organization' cursor.execute('INSERT INTO organizations_organization_users '
db.delete_table('api_manager_organization_users') 'SELECT * from api_manager_organization_users')
log_msg = 'organization_users entries moved from api_manager to organizations app'
self.print_message(log_msg)
# Removing M2M table for field groups on 'Organization' cursor.execute('INSERT INTO organizations_organization_groups '
db.delete_table('api_manager_organization_groups') 'SELECT * from api_manager_organization_groups')
log_msg = 'organization_groups entries moved from api_manager to organizations app'
self.print_message(log_msg)
transaction.commit()
# Removing M2M table for field workgroups on 'Organization' # Deleting model 'Organization'
db.delete_table('api_manager_organization_workgroups') db.delete_table('api_manager_organization')
# Removing M2M table for field users on 'Organization'
db.delete_table('api_manager_organization_users')
# Removing M2M table for field groups on 'Organization'
db.delete_table('api_manager_organization_groups')
# Removing M2M table for field workgroups on 'Organization'
db.delete_table('api_manager_organization_workgroups')
except Exception as e:
log_msg = e.message
self.print_message(log_msg)
else:
log_msg = 'oroganizations_organization is not empty. You might have already filled it.'
self.print_message(log_msg)
def backwards(self, orm): def backwards(self, orm):
# Adding model 'Organization' # Adding model 'Organization'
...@@ -60,7 +102,6 @@ class Migration(SchemaMigration): ...@@ -60,7 +102,6 @@ class Migration(SchemaMigration):
)) ))
db.create_unique('api_manager_organization_workgroups', ['organization_id', 'workgroup_id']) db.create_unique('api_manager_organization_workgroups', ['organization_id', 'workgroup_id'])
models = { models = {
'api_manager.coursecontentgrouprelationship': { 'api_manager.coursecontentgrouprelationship': {
'Meta': {'unique_together': "(('course_id', 'content_id', 'group_profile'),)", 'object_name': 'CourseContentGroupRelationship'}, 'Meta': {'unique_together': "(('course_id', 'content_id', 'group_profile'),)", 'object_name': 'CourseContentGroupRelationship'},
...@@ -158,4 +199,4 @@ class Migration(SchemaMigration): ...@@ -158,4 +199,4 @@ class Migration(SchemaMigration):
} }
} }
complete_apps = ['api_manager'] complete_apps = ['api_manager']
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment