# encoding: utf-8
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models, utils
import pickle

class Migration(SchemaMigration):

    no_dry_run = True
    def forwards(self, orm):
        try:
            db.rename_table("config_configvalue", "server_serverconfig")
            for conf in orm.ServerConfig.objects.all():
                conf.db_value = pickle.dumps(conf.db_value)
                conf.save()
        except Exception: #utils.DatabaseError:
            # this will happen if we start db from scratch (the config
            # app will then already be gone and no data is to be transferred)
            # So instead of renaming the old we instead have to manually create the new model.
            # Adding model 'ServerConfig'
            db.create_table('server_serverconfig', (
                    ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
                    ('db_key', self.gf('django.db.models.fields.CharField')(unique=True, max_length=64)),
                    ('db_value', self.gf('django.db.models.fields.TextField')(blank=True)),
                    ))
            db.send_create_signal('server', ['ServerConfig'])

    def backwards(self, orm):
        raise RuntimeError("This migration cannot be reversed.")

    models = {
        'config.configvalue': {
            'Meta': {'object_name': 'ConfigValue'},
            'db_key': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '100'}),
            'db_value': ('django.db.models.fields.TextField', [], {}),
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
        },
        'server.serverconfig': {
            'Meta': {'object_name': 'ServerConfig'},
            'db_key': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '64'}),
            'db_value': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
        }
    }

    complete_apps = ['config', 'server']

@@ -143,106 +143,423 @@
 le%0A%0A
-class Migration(SchemaMigration):%0A%0A    no_dry_run = True%0A    def forwards(self, orm):%0A        try:
+HAS_CONFIGVAL = True%0Atry: from src.server.models import ConfigValue%0Aexcept ImportError: HAS_CONFIGVAL = False%0AHAS_SERVERCONF = True%0Atry: from src.server.models import ServerConfig%0Aexcept ImportError: HAS_SERVERCONF = False%0A%0Aclass Migration(SchemaMigration):%0A%0A    no_dry_run = True%0A    def forwards(self, orm):%0A        if HAS_CONFIGVAL:# and not HAS_SERVERCONF:%0A            # this means we have to rename the old one
 %0A   
@@ -785,45 +785,32 @@
    e
-xcept Exception: #utils.DatabaseError
+lse:# not HAS_SERVERCONF
 :%0A  

Fix of migration to remove a bogus FATAL error during migration. This also fixes a one error in the chain of errors reported by postgresql databases (those still fail on a latter point, alas).