def create_schema(name, **options): """ This function creates a schema and perform a syncdb on it. As we call some syncdb and migrate management commands, we can't rely on transaction support. We are going to catch any exception (including SystemExit). """ try: cursor = connection.cursor() # We can't use params with system names cursor.execute('CREATE SCHEMA "%s"' % escape_schema_name(name)) transaction.commit_unless_managed() defaults = { 'verbosity': 0, 'traceback': None, 'noinput': True } defaults.update(options) sync_options = options # We never want migration to launch with syncdb call sync_options['migrate'] = False _, isolated_apps = get_apps() syncdb_apps(isolated_apps, name, **sync_options) migrate_apps(get_migration_candidates(isolated_apps), name, **options) schema_store.reset_path() except BaseException, e: drop_schema(name) raise Exception(str(e))
def drop_schema(name): Schema.objects.filter(name=name).delete() cursor = connection.cursor() try: # We can't use params with system names cursor.execute('DROP SCHEMA "%s" CASCADE' % escape_schema_name(name)) transaction.commit() except DatabaseError: transaction.rollback() except: transaction.rollback() raise
def create_schema_from_template(name, template_file): """ This function creates a new schema based on a template file. This template should be generated by ``schematemplate`` command (with --aspython option) """ sql = imp.load_source('schema_module', template_file).schema_sql sql = sql % {'schema_name': '"%s"' % escape_schema_name(name)} cursor = connection.cursor() try: # Systems behave differently with cursor.execute() try: cursor.execute(sql) except IndexError: cursor.execute(sql.replace('%', '%%')) transaction.commit() except: transaction.rollback() raise