from django.core.management.commands import syncdb
from django.db import models, connection, transaction

try:
    from south.management.commands import syncdb
except ImportError:
    pass

class Command(syncdb.Command):
    def handle_noargs(self, **options):
        cursor = connection.cursor()        
        # Ensure we have a __template__ schema.
        cursor.execute("SELECT schema_name FROM information_schema.schemata WHERE schema_name = '__template__';")
        if not cursor.fetchone():
            cursor.execute("CREATE SCHEMA __template__;")
        transaction.commit_unless_managed()
        
        # Set the search path, so we find created models correctly
        cursor.execute("SET search_path TO public,__template__;")
        
        super(Command, self).handle_noargs(**options)
from django.core.management.commands import syncdb
from django.db import models, connection, transaction

try:
    from south.management.commands import syncdb
except ImportError:
    pass

from ...models import Schema, template_schema

class Command(syncdb.Command):
    def handle_noargs(self, **options):
        # Ensure we have a __template__ schema.
        template_schema.create_schema()
        
        # Set the search path, so we find created models correctly
        cursor = connection.cursor()
        cursor.execute("SET search_path TO public,__template__;")
        
        super(Command, self).handle_noargs(**options)
        
        # Ensure all existing schemata exist (in case we imported them using loaddata or something)
        for schema in Schema.objects.all():
            schema.create_schema()
Refactor syncdb replacement. Clone existing schemata if they don't exist at syncdb time.