def handle_noargs(self, **options): from django.conf import settings from django.db import models from django.core.management.sql import emit_post_sync_signal from django.db.utils import DEFAULT_DB_ALIAS from django.core.management import call_command ignore_reset = options.get('ignore_reset', False) rebuild_haystack = options.get('rebuild_haystack', False) db = options.get('database', DEFAULT_DB_ALIAS) database_config = settings.DATABASES[db] if not ignore_reset: reset_schema(database_config) # init db schema call_command('syncdb', interactive=False) # Emit the post sync signal. This allows individual # applications to respond as if the database had been # sync'd from scratch. emit_post_sync_signal(models.get_models(), 0, 0, db) # get all fixtures fixtures_blocks = self._find_fixtures(settings.PROJECT_ROOT) for fixtures in fixtures_blocks: sys.stdout.write("Load fixtures: %s\n" % " ".join(fixtures)) call_command('loaddata', *fixtures) if rebuild_haystack: call_command('rebuild_index', interactive=False)
def handle_noargs(self, **options): from django.conf import settings from django.db import connection, models from django.core.management.sql import emit_post_sync_signal from django.db.utils import DEFAULT_DB_ALIAS from gwark.framework.db import reset_schema db = options.get('database', DEFAULT_DB_ALIAS) database_config = settings.DATABASES[db] reset_schema(database_config) # Reinstall the initial_data fixture. from django.core.management.commands import syncdb syncdb.Command().execute(noinput=True) # Emit the post sync signal. This allows individual # applications to respond as if the database had been # sync'd from scratch. emit_post_sync_signal(models.get_models(), 0, 0, db) from django.core.management import call_command # get all fixtures fixtures = self._find_fixtures(settings.PROJECT_ROOT) sys.stdout.write("Load fixtures: %s\n" % " ".join(fixtures))
def handle_noargs(self, **options): from django.conf import settings from django.db import connection, transaction, models from django.dispatch import dispatcher from django.core.management.sql import emit_post_sync_signal verbosity = int(options.get('verbosity', 1)) interactive = options.get('interactive') self.style = no_style() # Import the 'management' module within each installed app, to register # dispatcher events. for app_name in settings.INSTALLED_APPS: try: __import__(app_name + '.management', {}, {}, ['']) except ImportError: pass if interactive: confirm = raw_input("""You have requested a flush of the database. This will IRREVERSIBLY DESTROY all data currently in the %r database, and return each table to the state it was in after syncdb. Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: """ % settings.DJANGO_SQLALCHEMY_DBURI) else: confirm = 'yes' if confirm == 'yes': try: from sqlalchemy import create_engine # TODO: Original django code flushes by deleting rows from # each table and reseting sequences back to zero. This # doesn't reset sequences. from django_sqlalchemy.backend import metadata, session for app in models.get_apps(): for table in sql._get_tables_for_app(app): session.execute(table.delete()) session.commit() except Exception, e: # transaction.rollback_unless_managed() raise CommandError("""Database %s couldn't be flushed. Possible reasons: * The database isn't running or isn't configured correctly. * At least one of the expected database tables doesn't exist. * The SQL was invalid. Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run. The full error: %s""" % (settings.DJANGO_SQLALCHEMY_DBURI, e)) # transaction.commit_unless_managed() # Emit the post sync signal. This allows individual # applications to respond as if the database had been # sync'd from scratch. emit_post_sync_signal(models.get_models(), verbosity, interactive) # Reinstall the initial_data fixture. from django.core.management import call_command call_command('loaddata', 'initial_data', **options)
def handle_noargs(self, **options): db = options.get('database', DEFAULT_DB_ALIAS) connection = connections[db] verbosity = int(options.get('verbosity', 1)) interactive = options.get('interactive') self.style = no_style() # Import the 'management' module within each installed app, to register # dispatcher events. for app_name in settings.INSTALLED_APPS: try: import_module('.management', app_name) except ImportError: pass sql_list = sql_flush(self.style, connection, only_django=True) if interactive: confirm = raw_input("""You have requested a flush of the database. This will IRREVERSIBLY DESTROY all data currently in the %r database, and return each table to the state it was in after syncdb. Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: """ % connection.settings_dict['NAME']) else: confirm = 'yes' if confirm == 'yes': try: cursor = connection.cursor() for sql in sql_list: cursor.execute(sql) except Exception, e: transaction.rollback_unless_managed(using=db) raise CommandError("""Database %s couldn't be flushed. Possible reasons: * The database isn't running or isn't configured correctly. * At least one of the expected database tables doesn't exist. * The SQL was invalid. Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run. The full error: %s""" % (connection.settings_dict['NAME'], e)) transaction.commit_unless_managed(using=db) # Emit the post sync signal. This allows individual # applications to respond as if the database had been # sync'd from scratch. all_models = [] for app in models.get_apps(): all_models.extend([ m for m in models.get_models(app, include_auto_created=True) if router.allow_syncdb(db, m) ]) emit_post_sync_signal(set(all_models), verbosity, interactive, db) # Reinstall the initial_data fixture. kwargs = options.copy() kwargs['database'] = db call_command('loaddata', 'initial_data', **kwargs)
def handle_noargs(self, **options): from django.conf import settings from django.db import connection, transaction, models from django.dispatch import dispatcher from django.core.management.sql import sql_flush, emit_post_sync_signal verbosity = int(options.get('verbosity', 1)) interactive = options.get('interactive') self.style = no_style() # Import the 'management' module within each installed app, to register # dispatcher events. for app_name in settings.INSTALLED_APPS: try: __import__(app_name + '.management', {}, {}, ['']) except ImportError: pass sql_list = sql_flush(self.style, only_django=True) if interactive: confirm = input("""You have requested a flush of the database. This will IRREVERSIBLY DESTROY all data currently in the %r database, and return each table to the state it was in after syncdb. Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: """ % settings.DATABASE_NAME) else: confirm = 'yes' if confirm == 'yes': try: cursor = connection.cursor() for sql in sql_list: cursor.execute(sql) except Exception as e: transaction.rollback_unless_managed() raise CommandError( """Database %s couldn't be flushed. Possible reasons: * The database isn't running or isn't configured correctly. * At least one of the expected database tables doesn't exist. * The SQL was invalid. Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run. The full error: %s""" % (settings.DATABASE_NAME, e)) transaction.commit_unless_managed() # Emit the post sync signal. This allows individual # applications to respond as if the database had been # sync'd from scratch. emit_post_sync_signal(models.get_models(), verbosity, interactive) # Reinstall the initial_data fixture. from django.core.management import call_command call_command('loaddata', 'initial_data', **options) else: print("Flush cancelled.")
def handle_noargs(self, **options): from django.conf import settings from django.db import connection, transaction, models from django.core.management.sql import sql_flush, emit_post_sync_signal verbosity = int(options.get('verbosity', 1)) interactive = options.get('interactive') self.style = no_style() # Import the 'management' module within each installed app, to register # dispatcher events. for app_name in settings.INSTALLED_APPS: try: __import__(app_name + '.management', {}, {}, ['']) except ImportError: pass sql_list = sql_flush(self.style, only_django=True) if interactive: confirm = raw_input("""You have requested a flush of the database. This will IRREVERSIBLY DESTROY all data currently in the %r database, and return each table to the state it was in after syncdb. Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: """ % settings.DATABASE_NAME) else: confirm = 'yes' if confirm == 'yes': try: cursor = connection.cursor() for sql in sql_list: try: cursor.execute(sql) except Exception, e: #print "error! %s %s" % (sql,e) pass except Exception, e: transaction.rollback_unless_managed() raise CommandError("""Database %s couldn't be flushed. Possible reasons: * The database isn't running or isn't configured correctly. * At least one of the expected database tables doesn't exist. * The SQL was invalid. Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run. The full error: %s""" % (settings.DATABASE_NAME, e)) transaction.commit_unless_managed() # Emit the post sync signal. This allows individual # applications to respond as if the database had been # sync'd from scratch. emit_post_sync_signal(models.get_models(), verbosity, interactive) # Reinstall the initial_data fixture. from django.core.management import call_command call_command('loaddata', 'initial_data', **options)
def emit_post_syncdb(verbosity, interactive, database): # Emit the post sync signal. This allows individual applications to # respond as if the database had been sync'd from scratch. all_models = [] for app in models.get_apps(): all_models.extend([ m for m in models.get_models(app, include_auto_created=True) if router.allow_syncdb(database, m) ]) emit_post_sync_signal(set(all_models), verbosity, interactive, database)
def emit_post_migrate_or_sync(verbosity, interactive, database_name, created_models, post_migrate_state, plan): """Emit the post_migrate and/or post_sync signals. This will emit the :py:data:`~django.db.models.signals.post_migrate` and/or :py:data:`~django.db.models.signals.post_sync` signals, providing the appropriate arguments for the current version of Django. Args: verbosity (int): The verbosity level for output. interactive (bool): Whether handlers of the signal can prompt on the terminal for input. database_name (unicode): The name of the database that was migrated. created_models (list of django.db.models.Model): The list of models created outside of any migrations. post_migrate_state (django.db.migrations.state.ProjectState): The project state after applying migrations. plan (list): The full migration plan that was applied. """ emit_kwargs = { 'db': database_name, 'interactive': interactive, 'verbosity': verbosity, } if django_version <= (1, 8): emit_kwargs['created_models'] = created_models elif django_version >= (1, 10): if post_migrate_state: apps = post_migrate_state.apps else: apps = None emit_kwargs.update({ 'apps': apps, 'plan': plan, }) if emit_post_sync_signal: emit_post_sync_signal(**emit_kwargs) else: emit_post_migrate_signal(**emit_kwargs)
def execute_migrations(self, show_traceback=True): all_migrations = self._filter_down() if not len(all_migrations): sys.stdout.write("There are no migrations to apply.\n") created_models = set() for db, migrations in all_migrations.iteritems(): connection = connections[db] # init connection cursor = connection.cursor() cursor.close() # enter transaction management transaction.enter_transaction_management(using=db) transaction.managed(True, using=db) try: for migration in migrations: if db == DEFAULT_DB_ALIAS: migration_path = os.path.join(self.path, migration) else: migration_path = os.path.join(self.path, db, migration) created_models |= self._execute_migration(db, migration_path, show_traceback=show_traceback) sys.stdout.write("Emitting post sync signal.\n") emit_post_sync_signal( created_models=created_models, verbosity=self.verbosity, interactive=self.interactive, db=db, ) sys.stdout.write("Running loaddata for initial_data fixtures.\n") call_command( "loaddata", "initial_data", verbosity=self.verbosity, database=db, ) except Exception: transaction.rollback(using=db) sys.stdout.write("Rolled back all migrations on %r.\n" % db) raise else: transaction.commit(using=db) finally: transaction.leave_transaction_management(using=db)
def execute_migrations(self, show_traceback=True): """ Executes all pending migrations across all capable databases """ all_migrations = get_pending_migrations(self.path, self.databases) if not len(all_migrations): sys.stdout.write("There are no migrations to apply.\n") for db, migrations in all_migrations.iteritems(): connection = connections[db] # init connection cursor = connection.cursor() cursor.close() for migration in migrations: migration_path = self._get_migration_path(db, migration) with Transactional(): sys.stdout.write( "Executing migration %r on %r...." % (migration, db) ) created_models = self._execute_migration( db, migration_path, show_traceback=show_traceback ) emit_post_sync_signal( created_models=created_models, verbosity=self.verbosity, interactive=self.interactive, db=db, ) if self.load_initial_data: sys.stdout.write( "Running loaddata for initial_data fixtures on %r.\n" % db ) call_command( "loaddata", "initial_data", verbosity=self.verbosity, database=db, )
def handle_noargs(self, **options): from django.conf import settings from django.db import models from django.core.management.sql import emit_post_sync_signal from django.db.utils import DEFAULT_DB_ALIAS from django.core.management import call_command ignore_reset = options.get('ignore_reset') if ignore_reset is None: ignore_reset = self.ignore_reset_default db = options.get('database', DEFAULT_DB_ALIAS) database_config = settings.DATABASES[db] if not ignore_reset: reset_schema(database_config) # hack to avoid using south klass = load_command_class('django.core', 'syncdb') args = {} defaults = {} options['interactive'] = False for opt in klass.option_list: if opt.default is NO_DEFAULT: defaults[opt.dest] = None else: defaults[opt.dest] = opt.default defaults.update(options) klass.execute(*args, **defaults) # Emit the post sync signal. This allows individual # applications to respond as if the database had been # sync'd from scratch. emit_post_sync_signal(models.get_models(), 0, 0, db) call_command('migrate', fake=True) self.log("load Random Dummy data ...") try: user = get_user_model().objects.create_superuser('admin', '*****@*****.**', DUMMY_PASSWORD) user.first_name, user.last_name = name_factory.get_full_name() user.save() self.log("superuser: admin") except Exception, e: self.log("WARNING: could not create superuser: %s" % str(e))
def handle_noargs(self, **options): db = options.get("database", DEFAULT_DB_ALIAS) connection = connections[db] verbosity = int(options.get("verbosity", 1)) interactive = options.get("interactive") self.style = no_style() if interactive: confirm = raw_input( """You have requested a erase all the data in the current nucleos EDMS installation. This will IRREVERSIBLY ERASE all user data currently in the database, and return each table to the state it was in after syncdb. Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: """ ) else: confirm = "yes" if confirm == "yes": try: Cleanup.execute_all() except Exception as exception: raise CommandError( """Unable to erase data. Possible reasons: * The database isn't running or isn't configured correctly. * At least one of the expected database tables doesn't exist.""" ) # Emit the post sync signal. This allows individual # applications to respond as if the database had been # sync'd from scratch. all_models = [] for app in models.get_apps(): all_models.extend( [m for m in models.get_models(app, include_auto_created=True) if router.allow_syncdb(db, m)] ) emit_post_sync_signal(set(all_models), verbosity, interactive, db) # Reinstall the initial_data fixture. kwargs = options.copy() kwargs["database"] = db else: print "Erase data cancelled."
def handle_noargs(self, **options): db = options.get('database', DEFAULT_DB_ALIAS) connection = connections[db] verbosity = int(options.get('verbosity', 1)) interactive = options.get('interactive') self.style = no_style() if interactive: confirm = raw_input( """You have requested a erase all the data in the current Mayan EDMS installation. This will IRREVERSIBLY ERASE all user data currently in the database, and return each table to the state it was in after syncdb. Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: """) else: confirm = 'yes' if confirm == 'yes': try: Cleanup.execute_all() except Exception as exception: raise CommandError("""Unable to erase data. Possible reasons: * The database isn't running or isn't configured correctly. * At least one of the expected database tables doesn't exist.""") # Emit the post sync signal. This allows individual # applications to respond as if the database had been # sync'd from scratch. all_models = [] for app in models.get_apps(): all_models.extend([ m for m in models.get_models(app, include_auto_created=True) if router.allow_syncdb(db, m) ]) emit_post_sync_signal(set(all_models), verbosity, interactive, db) # Reinstall the initial_data fixture. kwargs = options.copy() kwargs['database'] = db else: print 'Erase data cancelled.'
class Command(NoArgsCommand): option_list = NoArgsCommand.option_list + ( make_option('--verbosity', action='store', dest='verbosity', default='1', type='choice', choices=['0', '1', '2'], help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'), make_option('--noinput', action='store_false', dest='interactive', default=True, help='Tells Django to NOT prompt the user for input of any kind.'), ) help = "Create the database tables for all apps in INSTALLED_APPS whose tables haven't already been created." def handle_noargs(self, **options): from django.db import connection, transaction, models from django.conf import settings from django.core.management.sql import table_list, installed_models, sql_model_create, sql_for_pending_references, many_to_many_sql_for_model, custom_sql_for_model, sql_indexes_for_model, emit_post_sync_signal verbosity = int(options.get('verbosity', 1)) interactive = options.get('interactive') self.style = no_style() # Import the 'management' module within each installed app, to register # dispatcher events. for app_name in settings.INSTALLED_APPS: try: __import__(app_name + '.management', {}, {}, ['']) except ImportError, exc: if not exc.args[0].startswith('No module named management'): raise cursor = connection.cursor() if connection.features.uses_case_insensitive_names: table_name_converter = lambda x: x.upper() else: table_name_converter = lambda x: x # Get a list of all existing database tables, so we know what needs to # be added. tables = [table_name_converter(name) for name in table_list()] # Get a list of already installed *models* so that references work right. seen_models = installed_models(tables) created_models = set() pending_references = {} # Create the tables for each model for app in models.get_apps(): app_name = app.__name__.split('.')[-2] model_list = models.get_models(app) for model in model_list: # Create the model's database table, if it doesn't already exist. if verbosity >= 2: print "Processing %s.%s model" % (app_name, model._meta.object_name) if table_name_converter(model._meta.db_table) in tables: continue sql, references = sql_model_create(model, self.style, seen_models) seen_models.add(model) created_models.add(model) for refto, refs in references.items(): pending_references.setdefault(refto, []).extend(refs) if refto in seen_models: sql.extend(sql_for_pending_references(refto, self.style, pending_references)) sql.extend(sql_for_pending_references(model, self.style, pending_references)) if verbosity >= 1: print "Creating table %s" % model._meta.db_table for statement in sql: cursor.execute(statement) tables.append(table_name_converter(model._meta.db_table)) # Create the m2m tables. This must be done after all tables have been created # to ensure that all referred tables will exist. for app in models.get_apps(): app_name = app.__name__.split('.')[-2] model_list = models.get_models(app) for model in model_list: if model in created_models: sql = many_to_many_sql_for_model(model, self.style) if sql: if verbosity >= 2: print "Creating many-to-many tables for %s.%s model" % (app_name, model._meta.object_name) for statement in sql: cursor.execute(statement) transaction.commit_unless_managed() # Send the post_syncdb signal, so individual apps can do whatever they need # to do at this point. emit_post_sync_signal(created_models, verbosity, interactive) # Install custom SQL for the app (but only if this # is a model we've just created) for app in models.get_apps(): app_name = app.__name__.split('.')[-2] for model in models.get_models(app): if model in created_models: custom_sql = custom_sql_for_model(model) if custom_sql: if verbosity >= 1: print "Installing custom SQL for %s.%s model" % (app_name, model._meta.object_name) try: for sql in custom_sql: cursor.execute(sql) except Exception, e: sys.stderr.write("Failed to install custom SQL for %s.%s model: %s" % \ (app_name, model._meta.object_name, e)) transaction.rollback_unless_managed() else: transaction.commit_unless_managed()
class Command(NoArgsCommand): option_list = NoArgsCommand.option_list + ( make_option( '--noinput', action='store_false', dest='interactive', default=True, help='Tells Django to NOT prompt the user for input of any kind.'), make_option('--database', action='store', dest='database', default=DEFAULT_DB_ALIAS, help='Nominates a database to synchronize. ' 'Defaults to the "default" database.'), ) help = "Create the database tables for all apps in INSTALLED_APPS whose tables haven't already been created." def handle_noargs(self, **options): verbosity = int(options.get('verbosity', 1)) interactive = options.get('interactive') show_traceback = options.get('traceback', False) # Stealth option -- 'load_initial_data' is used by the testing setup # process to disable initial fixture loading. load_initial_data = options.get('load_initial_data', True) self.style = no_style() # Import the 'management' module within each installed app, to register # dispatcher events. for app_name in settings.INSTALLED_APPS: try: import_module('.management', app_name) except ImportError, exc: # This is slightly hackish. We want to ignore ImportErrors # if the "management" module itself is missing -- but we don't # want to ignore the exception if the management module exists # but raises an ImportError for some reason. The only way we # can do this is to check the text of the exception. Note that # we're a bit broad in how we check the text, because different # Python implementations may not use the same text. # CPython uses the text "No module named management" # PyPy uses "No module named myproject.myapp.management" msg = exc.args[0] if not msg.startswith( 'No module named') or 'management' not in msg: raise db = options.get('database', DEFAULT_DB_ALIAS) connection = connections[db] cursor = connection.cursor() # Get a list of already installed *models* so that references work right. tables = connection.introspection.table_names() seen_models = connection.introspection.installed_models(tables) created_models = set() pending_references = {} # Build the manifest of apps and models that are to be synchronized all_models = [(app.__name__.split('.')[-2], [ m for m in models.get_models(app, include_auto_created=True) if router.allow_syncdb(db, m) ]) for app in models.get_apps()] def model_installed(model): opts = model._meta converter = connection.introspection.table_name_converter return not ((converter(opts.db_table) in tables) or (opts.auto_created and converter( opts.auto_created._meta.db_table) in tables)) manifest = SortedDict((app_name, filter(model_installed, model_list)) for app_name, model_list in all_models) # Create the tables for each model if verbosity >= 1: print "Creating tables ..." for app_name, model_list in manifest.items(): for model in model_list: # Create the model's database table, if it doesn't already exist. if verbosity >= 3: print "Processing %s.%s model" % (app_name, model._meta.object_name) sql, references = connection.creation.sql_create_model( model, self.style, seen_models) seen_models.add(model) created_models.add(model) for refto, refs in references.items(): pending_references.setdefault(refto, []).extend(refs) if refto in seen_models: sql.extend( connection.creation.sql_for_pending_references( refto, self.style, pending_references)) sql.extend( connection.creation.sql_for_pending_references( model, self.style, pending_references)) if verbosity >= 1 and sql: print "Creating table %s" % model._meta.db_table for statement in sql: cursor.execute(statement) tables.append( connection.introspection.table_name_converter( model._meta.db_table)) transaction.commit_unless_managed(using=db) # Send the post_syncdb signal, so individual apps can do whatever they need # to do at this point. emit_post_sync_signal(created_models, verbosity, interactive, db) # The connection may have been closed by a syncdb handler. cursor = connection.cursor() # Install custom SQL for the app (but only if this # is a model we've just created) if verbosity >= 1: print "Installing custom SQL ..." for app_name, model_list in manifest.items(): for model in model_list: if model in created_models: custom_sql = custom_sql_for_model(model, self.style, connection) if custom_sql: if verbosity >= 2: print "Installing custom SQL for %s.%s model" % ( app_name, model._meta.object_name) try: for sql in custom_sql: cursor.execute(sql) except Exception, e: sys.stderr.write("Failed to install custom SQL for %s.%s model: %s\n" % \ (app_name, model._meta.object_name, e)) if show_traceback: import traceback traceback.print_exc() transaction.rollback_unless_managed(using=db) else: transaction.commit_unless_managed(using=db) else: if verbosity >= 3: print "No custom SQL for %s.%s model" % ( app_name, model._meta.object_name)
def execute_migrations(self, show_traceback=False): migrations = self._filter_down() if not len(migrations): sys.stdout.write("There are no migrations to apply.\n") created_models = set() for migration in migrations: iteration_models = set() try: migration_path = os.path.join(self.path, migration) fp = open(migration_path, "rb") lines = fp.readlines() fp.close() content = "".join(lines) if migration_path.endswith(".sql"): to_execute = "".join([ l for l in lines if not l.startswith("### New Model: ") ]) connection = connections[self.db] cursor = connection.cursor() sys.stdout.write("Executing %s... " % migration) try: cursor.execute(to_execute) except Exception: sys.stdout.write("failed\n") if show_traceback: traceback.print_exc() raise MigrationError() else: sys.stdout.write("success\n") for l in lines: if l.startswith("### New Model: "): iteration_models.add( get_model(*l.replace("### New Model: ", "").strip().split("."))) elif migration_path.endswith(".py"): sys.stdout.write("Executing %s... " % migration) module = {} execfile(migration_path, {}, module) if "migrate" in module and callable(module["migrate"]): try: module["migrate"]() except Exception: sys.stdout.write("failed\n") if show_traceback: traceback.print_exc() raise MigrationError() else: sys.stdout.write("success\n") Migration.objects.create( migration_label=migration, content=content, scm_version=self._get_rev(migration_path)) except Exception: transaction.rollback(using=self.db) sys.stdout.write("Rolled back all migrations.\n") raise else: created_models = created_models.union(iteration_models) transaction.commit(using=self.db) sys.stdout.write("Emitting post sync signal.\n") if created_models: emit_post_sync_signal(created_models, self.verbosity, self.interactive, self.db) sys.stdout.write("Running loaddata for initial_data fixtures.\n") call_command("loaddata", "initial_data", verbosity=self.verbosity, database=self.db) transaction.commit()
if count == 0: print "Create tables for models in sitemanager.modules:" print "Creating table %s" % model._meta.db_table count += 1 for statement in sql: cursor.execute(statement) tables.append(connection.introspection.table_name_converter(model._meta.db_table)) transaction.commit_unless_managed(using=db) if len(created_models) > 0: # Send the post_syncdb signal, so individual apps can do whatever they need # to do at this point. emit_post_sync_signal(created_models, verbosity, interactive, db) # The connection may have been closed by a syncdb handler. cursor = connection.cursor() # Install SQL indicies for all newly created models for app_name, model_list in manifest.items(): for model in model_list: if model in created_models: index_sql = connection.creation.sql_indexes_for_model(model, style) if index_sql: if verbosity >= 1: print "Installing index for %s.%s model" % (app_name, model._meta.object_name) try: for sql in index_sql: cursor.execute(sql)
def create(model, using): ''' Mostly lifted from django.core.management.commands.syncdb''' opts = model._meta # Get database connection and cursor db = using or DEFAULT_DB_ALIAS connection = connections[db] cursor = connection.cursor() # Get a list of already installed *models* so that references work right. tables = connection.introspection.table_names() seen_models = connection.introspection.installed_models(tables) created_models = set() pending_references = {} # Abort if the table we're going to create already exists if opts.db_table in tables: return # Build the manifest of apps and models that are to be synchronized all_models = [ (app.__name__.split('.')[-2], [m for m in models.get_models(app, include_auto_created=True) if router.allow_syncdb(db, m)]) for app in models.get_apps() ] def model_installed(model): opts = model._meta converter = connection.introspection.table_name_converter return not ((converter(opts.db_table) in tables) or (opts.auto_created and converter(opts.auto_created._meta.db_table) in tables)) manifest = SortedDict( (app_name, filter(model_installed, model_list)) for app_name, model_list in all_models ) # Get create SQL sql, references = connection.creation.sql_create_model(model, no_style(), seen_models) seen_models.add(model) created_models.add(model) for refto, refs in references.items(): pending_references.setdefault(refto, []).extend(refs) if refto in seen_models: sql.extend(connection.creation.sql_for_pending_references(refto, no_style(), pending_references)) sql.extend(connection.creation.sql_for_pending_references(model, no_style(), pending_references)) for statement in sql: cursor.execute(statement) tables.append(connection.introspection.table_name_converter(model._meta.db_table)) # Create content_type try: ct = ContentType.objects.get(app_label=opts.app_label, model=opts.object_name.lower()) except ContentType.DoesNotExist: ct = ContentType(name=smart_unicode(opts.verbose_name_raw), app_label=opts.app_label, model=opts.object_name.lower()) ct.save() # Commit changes to the database transaction.commit_unless_managed(using=db) # Send the post_syncdb signal, so individual apps can do whatever they need # to do at this point. emit_post_sync_signal(created_models, 0, False, db) # The connection may have been closed by a syncdb handler. cursor = connection.cursor() # Install SQL indicies for all newly created models for app_name, model_list in manifest.items(): for model in model_list: if model in created_models: index_sql = connection.creation.sql_indexes_for_model(model, no_style()) if index_sql: try: for sql in index_sql: cursor.execute(sql) except Exception, e: sys.stderr.write("Failed to install index for %s.%s model: %s\n" % \ (app_name, model._meta.object_name, e)) transaction.rollback_unless_managed(using=db) else: transaction.commit_unless_managed(using=db)
def execute_migrations(self): migrations = self._filter_down() if len(migrations) == 0: print "There are no migrations to apply." return created_models = [] for migration in migrations: migration_path = os.path.join(self.path, migration) fp = open(migration_path, "rb") lines = fp.readlines() fp.close() content = "".join(lines) if migration_path.endswith(".sql"): to_execute = "".join( [l for l in lines if not l.startswith("### New Model: ")] ) cmd = get_db_exec_args(self.db) try: p = Popen( cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT ) except: sys.exit( "There was an error executing: %s\nPlease make sure that" " this command is in your path." % " ".join(cmd) ) print migration (out, err) = p.communicate(input=to_execute) print "stdout:", out print "stderr:", err if p.returncode != 0: sys.exit( "\nExecution stopped!\n\nThere was an error in %s\n" % \ migration_path ) created_models.extend([ get_model( *l.replace("### New Model: ", "").strip().split(".") ) for l in lines if l.startswith("### New Model: ") ]) elif migration_path.endswith(".py"): print migration module = {} execfile(migration_path, {}, module) if "migrate" in module and callable(module["migrate"]): module["migrate"]() Migration.objects.create( migration_label=migration, content=content, scm_version=self._get_rev(migration_path) ) fp.close() emit_post_sync_signal( created_models, self.verbosity, self.interactive, self.db ) call_command( 'loaddata', 'initial_data', verbosity=self.verbosity, database=self.db )
def handle_noargs(self, **options): verbosity = int(options.get('verbosity')) interactive = options.get('interactive') show_traceback = options.get('traceback') load_initial_data = options.get('load_initial_data') self.style = no_style() # Import the 'management' module within each installed app, to register # dispatcher events. for app_name in settings.INSTALLED_APPS: try: import_module('.management', app_name) except ImportError as exc: # This is slightly hackish. We want to ignore ImportErrors # if the "management" module itself is missing -- but we don't # want to ignore the exception if the management module exists # but raises an ImportError for some reason. The only way we # can do this is to check the text of the exception. Note that # we're a bit broad in how we check the text, because different # Python implementations may not use the same text. # CPython uses the text "No module named management" # PyPy uses "No module named myproject.myapp.management" msg = exc.args[0] if not msg.startswith('No module named') or 'management' not in msg: raise db = options.get('database') connection = connections[db] cursor = connection.cursor() # Get a list of already installed *models* so that references work right. tables = connection.introspection.table_names() seen_models = connection.introspection.installed_models(tables) created_models = set() pending_references = {} # Build the manifest of apps and models that are to be synchronized all_models = [ (app.__name__.split('.')[-2], [m for m in get_models(app, include_auto_created=True) if router.allow_syncdb(db, m)]) for app in models.get_apps() ] def model_installed(model): opts = model._meta converter = connection.introspection.table_name_converter return not ((converter(opts.db_table) in tables) or (opts.auto_created and converter(opts.auto_created._meta.db_table) in tables)) manifest = SortedDict( (app_name, list(filter(model_installed, model_list))) for app_name, model_list in all_models ) # Create the tables for each model if verbosity >= 1: self.stdout.write("Creating tables ...\n") for app_name, model_list in manifest.items(): for model in model_list: # Create the model's database table, if it doesn't already exist. if verbosity >= 3: self.stdout.write("Processing %s.%s model\n" % (app_name, model._meta.object_name)) sql, references = connection.creation.sql_create_model(model, self.style, seen_models) seen_models.add(model) created_models.add(model) for refto, refs in references.items(): pending_references.setdefault(refto, []).extend(refs) if refto in seen_models: sql.extend(connection.creation.sql_for_pending_references(refto, self.style, pending_references)) sql.extend(connection.creation.sql_for_pending_references(model, self.style, pending_references)) if verbosity >= 1 and sql: self.stdout.write("Creating table %s\n" % model._meta.db_table) for statement in sql: cursor.execute(statement) tables.append(connection.introspection.table_name_converter(model._meta.db_table)) transaction.commit_unless_managed(using=db) # Send the post_syncdb signal, so individual apps can do whatever they need # to do at this point. emit_post_sync_signal(created_models, verbosity, interactive, db) # The connection may have been closed by a syncdb handler. cursor = connection.cursor() # Install custom SQL for the app (but only if this # is a model we've just created) if verbosity >= 1: self.stdout.write("Installing custom SQL ...\n") for app_name, model_list in manifest.items(): for model in model_list: if model in created_models: custom_sql = custom_sql_for_model(model, self.style, connection) if custom_sql: if verbosity >= 2: self.stdout.write("Installing custom SQL for %s.%s model\n" % (app_name, model._meta.object_name)) try: for sql in custom_sql: cursor.execute(sql) except Exception as e: self.stderr.write("Failed to install custom SQL for %s.%s model: %s\n" % \ (app_name, model._meta.object_name, e)) if show_traceback: traceback.print_exc() transaction.rollback_unless_managed(using=db) else: transaction.commit_unless_managed(using=db) else: if verbosity >= 3: self.stdout.write("No custom SQL for %s.%s model\n" % (app_name, model._meta.object_name)) if verbosity >= 1: self.stdout.write("Installing indexes ...\n") # Install SQL indices for all newly created models for app_name, model_list in manifest.items(): for model in model_list: if model in created_models: index_sql = connection.creation.sql_indexes_for_model(model, self.style) if index_sql: if verbosity >= 2: self.stdout.write("Installing index for %s.%s model\n" % (app_name, model._meta.object_name)) try: for sql in index_sql: cursor.execute(sql) except Exception as e: self.stderr.write("Failed to install index for %s.%s model: %s\n" % \ (app_name, model._meta.object_name, e)) transaction.rollback_unless_managed(using=db) else: transaction.commit_unless_managed(using=db) # Load initial_data fixtures (unless that has been disabled) if load_initial_data: call_command('loaddata', 'initial_data', verbosity=verbosity, database=db, skip_validation=True)
def handle_noargs(self, **options): from django.db import connection, transaction, models from django.conf import settings from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal verbosity = int(options.get('verbosity', 1)) interactive = options.get('interactive') show_traceback = options.get('traceback', False) self.style = no_style() # Import the 'management' module within each installed app, to register # dispatcher events. for app_name in settings.INSTALLED_APPS: try: __import__(app_name + '.management', {}, {}, ['']) except ImportError as exc: # This is slightly hackish. We want to ignore ImportErrors # if the "management" module itself is missing -- but we don't # want to ignore the exception if the management module exists # but raises an ImportError for some reason. The only way we # can do this is to check the text of the exception. Note that # we're a bit broad in how we check the text, because different # Python implementations may not use the same text. # CPython uses the text "No module named management" # PyPy uses "No module named myproject.myapp.management" msg = exc.args[0] if not msg.startswith('No module named') or 'management' not in msg: raise cursor = connection.cursor() # Get a list of already installed *models* so that references work right. tables = connection.introspection.table_names() seen_models = connection.introspection.installed_models(tables) created_models = set() pending_references = {} # Create the tables for each model for app in models.get_apps(): app_name = app.__name__.split('.')[-2] model_list = models.get_models(app) for model in model_list: # Create the model's database table, if it doesn't already exist. if verbosity >= 2: print("Processing %s.%s model" % (app_name, model._meta.object_name)) if connection.introspection.table_name_converter(model._meta.db_table) in tables: continue sql, references = connection.creation.sql_create_model(model, self.style, seen_models) seen_models.add(model) created_models.add(model) for refto, refs in list(references.items()): pending_references.setdefault(refto, []).extend(refs) if refto in seen_models: sql.extend(connection.creation.sql_for_pending_references(refto, self.style, pending_references)) sql.extend(connection.creation.sql_for_pending_references(model, self.style, pending_references)) if verbosity >= 1: print("Creating table %s" % model._meta.db_table) for statement in sql: cursor.execute(statement) tables.append(connection.introspection.table_name_converter(model._meta.db_table)) # Create the m2m tables. This must be done after all tables have been created # to ensure that all referred tables will exist. for app in models.get_apps(): app_name = app.__name__.split('.')[-2] model_list = models.get_models(app) for model in model_list: if model in created_models: sql = connection.creation.sql_for_many_to_many(model, self.style) if sql: if verbosity >= 2: print("Creating many-to-many tables for %s.%s model" % (app_name, model._meta.object_name)) for statement in sql: cursor.execute(statement) transaction.commit_unless_managed() # Send the post_syncdb signal, so individual apps can do whatever they need # to do at this point. emit_post_sync_signal(created_models, verbosity, interactive) # The connection may have been closed by a syncdb handler. cursor = connection.cursor() # Install custom SQL for the app (but only if this # is a model we've just created) for app in models.get_apps(): app_name = app.__name__.split('.')[-2] for model in models.get_models(app): if model in created_models: custom_sql = custom_sql_for_model(model, self.style) if custom_sql: if verbosity >= 1: print("Installing custom SQL for %s.%s model" % (app_name, model._meta.object_name)) try: for sql in custom_sql: cursor.execute(sql) except Exception as e: sys.stderr.write("Failed to install custom SQL for %s.%s model: %s\n" % \ (app_name, model._meta.object_name, e)) if show_traceback: import traceback traceback.print_exc() transaction.rollback_unless_managed() else: transaction.commit_unless_managed() else: if verbosity >= 2: print("No custom SQL for %s.%s model" % (app_name, model._meta.object_name)) # Install SQL indicies for all newly created models for app in models.get_apps(): app_name = app.__name__.split('.')[-2] for model in models.get_models(app): if model in created_models: index_sql = connection.creation.sql_indexes_for_model(model, self.style) if index_sql: if verbosity >= 1: print("Installing index for %s.%s model" % (app_name, model._meta.object_name)) try: for sql in index_sql: cursor.execute(sql) except Exception as e: sys.stderr.write("Failed to install index for %s.%s model: %s\n" % \ (app_name, model._meta.object_name, e)) transaction.rollback_unless_managed() else: transaction.commit_unless_managed() # Install the 'initial_data' fixture, using format discovery from django.core.management import call_command call_command('loaddata', 'initial_data', verbosity=verbosity)
def handle(self, *args, **options): """ Handle liquibase command parameters """ database = getattr(settings, 'LIQUIMIGRATE_DATABASE', options['database']) try: dbsettings = databases[database] except KeyError: raise CommandError("don't know such a connection: %s" % database) verbosity = int(options.get('verbosity')) # get driver driver_class = options.get('driver') or \ dbsettings.get('ENGINE').split('.')[-1] dbtag, driver, classpath = LIQUIBASE_DRIVERS.get(driver_class, (None, None, None)) classpath = options.get('classpath') or classpath if driver is None: raise CommandError("unsupported db driver '%s'\n\ available drivers: %s" % (driver_class, ' '.join(LIQUIBASE_DRIVERS.keys()))) # command options changelog_file = options.get('changelog_file') or \ _get_changelog_file(options['database']) username = options.get('username') or dbsettings.get('USER') or '' password = options.get('password') or dbsettings.get('PASSWORD') or '' url = options.get('url') or _get_url_for_db(dbtag, dbsettings) if len(args) < 1: raise CommandError("give me any command, for example 'update'") command = args[0] cmdargs = { 'jar': LIQUIBASE_JAR, 'changelog_file': changelog_file, 'username': username, 'password': password, 'command': command, 'driver': driver, 'classpath': classpath, 'url': url, 'args': ' '.join(args[1:]), } cmdline = "java -jar %(jar)s --changeLogFile %(changelog_file)s \ --username=%(username)s --password=%(password)s \ --driver=%(driver)s --classpath=%(classpath)s --url=%(url)s \ %(command)s %(args)s" % (cmdargs) if verbosity > 0: print "changelog file: %s" % (changelog_file,) print "executing: %s" % (cmdline,) created_models = None # we dont know it if emit_pre_migrate_signal and not options.get('no_signals'): emit_pre_migrate_signal(created_models, 0, options.get('interactive'), database) rc = os.system(cmdline) if rc == 0: try: if not options.get('no_signals'): if emit_post_migrate_signal: emit_post_migrate_signal(created_models, 0, options.get('interactive'), database) elif emit_post_sync_signal: emit_post_sync_signal(created_models, 0, options.get('interactive'), database) call_command('loaddata', 'initial_data', verbosity=0, database=database) except TypeError: # singledb (1.1 and older) emit_post_sync_signal(created_models, 0, options.get('interactive')) call_command('loaddata', 'initial_data', verbosity=0) else: raise CommandError('Liquibase returned an error code %s' % rc)
def handle(self, *args, **options): """ Handle liquibase command parameters """ database = getattr(settings, 'LIQUIMIGRATE_DATABASE', options['database']) try: dbsettings = databases[database] except KeyError: raise CommandError("don't know such a connection: %s" % database) verbosity = int(options.get('verbosity')) # get driver driver_class = (options.get('driver') or dbsettings.get('ENGINE').split('.')[-1]) dbtag, driver, classpath = LIQUIBASE_DRIVERS.get( driver_class, (None, None, None)) classpath = options.get('classpath') or classpath if driver is None: raise CommandError( "unsupported db driver '%s'\n" "available drivers: %s" % (driver_class, ' '.join(LIQUIBASE_DRIVERS.keys()))) # command options changelog_file = (options.get('changelog_file') or _get_changelog_file(options['database'])) username = options.get('username') or dbsettings.get('USER') or '' password = options.get('password') or dbsettings.get('PASSWORD') or '' url = options.get('url') or _get_url_for_db(dbtag, dbsettings) command = options['command'] cmdargs = { 'jar': LIQUIBASE_JAR, 'changelog_file': changelog_file, 'username': username, 'password': password, 'command': command, 'driver': driver, 'classpath': classpath, 'url': url, 'args': ' '.join(args), } cmdline = "java -jar %(jar)s --changeLogFile %(changelog_file)s \ --username=%(username)s --password=%(password)s \ --driver=%(driver)s --classpath=%(classpath)s --url=%(url)s \ %(command)s %(args)s" % (cmdargs) if verbosity > 0: print("changelog file: %s" % (changelog_file, )) print("executing: %s" % (cmdline, )) created_models = None # we dont know it if emit_pre_migrate_signal and not options.get('no_signals'): if django_19_or_newer: emit_pre_migrate_signal(1, options.get('interactive'), database) else: emit_pre_migrate_signal(created_models, 1, options.get('interactive'), database) rc = os.system(cmdline) if rc == 0: try: if not options.get('no_signals'): if emit_post_migrate_signal: if django_19_or_newer: emit_post_migrate_signal( 0, options.get('interactive'), database) else: emit_post_migrate_signal( created_models, 0, options.get('interactive'), database) elif emit_post_sync_signal: emit_post_sync_signal(created_models, 0, options.get('interactive'), database) if not django_19_or_newer: call_command('loaddata', 'initial_data', verbosity=1, database=database) except TypeError: # singledb (1.1 and older) emit_post_sync_signal(created_models, 0, options.get('interactive')) call_command('loaddata', 'initial_data', verbosity=0) else: raise CommandError('Liquibase returned an error code %s' % rc)
def handle_noargs(self, **options): db = options.get('database') connection = connections[db] verbosity = int(options.get('verbosity')) interactive = options.get('interactive') # 'reset_sequences' and 'allow_cascade' are stealth options reset_sequences = options.get('reset_sequences', True) allow_cascade = options.get('allow_cascade', False) self.style = no_style() # Import the 'management' module within each installed app, to register # dispatcher events. for app_name in settings.INSTALLED_APPS: try: import_module('.management', app_name) except ImportError: pass sql_list = sql_flush(self.style, connection, only_django=True, reset_sequences=reset_sequences, allow_cascade=allow_cascade) if interactive: confirm = input("""You have requested a flush of the database. This will IRREVERSIBLY DESTROY all data currently in the %r database, and return each table to the state it was in after syncdb. Are you sure you want to do this? Type 'yes' to continue, or 'no' to cancel: """ % connection.settings_dict['NAME']) else: confirm = 'yes' if confirm == 'yes': try: with transaction.commit_on_success_unless_managed(): cursor = connection.cursor() for sql in sql_list: cursor.execute(sql) except Exception as e: new_msg = ( "Database %s couldn't be flushed. Possible reasons:\n" " * The database isn't running or isn't configured correctly.\n" " * At least one of the expected database tables doesn't exist.\n" " * The SQL was invalid.\n" "Hint: Look at the output of 'django-admin.py sqlflush'. That's the SQL this command wasn't able to run.\n" "The full error: %s") % (connection.settings_dict['NAME'], e) six.reraise(CommandError, CommandError(new_msg), sys.exc_info()[2]) # Emit the post sync signal. This allows individual # applications to respond as if the database had been # sync'd from scratch. all_models = [] for app in models.get_apps(): all_models.extend([ m for m in models.get_models(app, include_auto_created=True) if router.allow_syncdb(db, m) ]) emit_post_sync_signal(set(all_models), verbosity, interactive, db) # Reinstall the initial_data fixture. if options.get('load_initial_data'): # Reinstall the initial_data fixture. call_command('loaddata', 'initial_data', **options) else: self.stdout.write("Flush cancelled.\n")
def handle_noargs(self, **options): verbosity = int(options.get('verbosity')) interactive = options.get('interactive') show_traceback = options.get('traceback') load_initial_data = options.get('load_initial_data') self.style = no_style() # Import the 'management' module within each installed app, to register # dispatcher events. for app_name in settings.INSTALLED_APPS: try: import_module('.management', app_name) except ImportError as exc: # This is slightly hackish. We want to ignore ImportErrors # if the "management" module itself is missing -- but we don't # want to ignore the exception if the management module exists # but raises an ImportError for some reason. The only way we # can do this is to check the text of the exception. Note that # we're a bit broad in how we check the text, because different # Python implementations may not use the same text. # CPython uses the text "No module named management" # PyPy uses "No module named myproject.myapp.management" msg = exc.args[0] if not msg.startswith( 'No module named') or 'management' not in msg: raise db = options.get('database') connection = connections[db] cursor = connection.cursor() # Get a list of already installed *models* so that references work right. tables = connection.introspection.table_names() seen_models = connection.introspection.installed_models(tables) created_models = set() pending_references = {} # Build the manifest of apps and models that are to be synchronized all_models = [(app.__name__.split('.')[-2], [ m for m in models.get_models(app, include_auto_created=True) if router.allow_syncdb(db, m) ]) for app in models.get_apps()] def model_installed(model): opts = model._meta converter = connection.introspection.table_name_converter return not ((converter(opts.db_table) in tables) or (opts.auto_created and converter( opts.auto_created._meta.db_table) in tables)) manifest = SortedDict( (app_name, list(filter(model_installed, model_list))) for app_name, model_list in all_models) # Create the tables for each model if verbosity >= 1: self.stdout.write("Creating tables ...\n") for app_name, model_list in manifest.items(): for model in model_list: # Create the model's database table, if it doesn't already exist. if verbosity >= 3: self.stdout.write("Processing %s.%s model\n" % (app_name, model._meta.object_name)) sql, references = connection.creation.sql_create_model( model, self.style, seen_models) seen_models.add(model) created_models.add(model) for refto, refs in references.items(): pending_references.setdefault(refto, []).extend(refs) if refto in seen_models: sql.extend( connection.creation.sql_for_pending_references( refto, self.style, pending_references)) sql.extend( connection.creation.sql_for_pending_references( model, self.style, pending_references)) if verbosity >= 1 and sql: self.stdout.write("Creating table %s\n" % model._meta.db_table) for statement in sql: cursor.execute(statement) tables.append( connection.introspection.table_name_converter( model._meta.db_table)) transaction.commit_unless_managed(using=db) # Send the post_syncdb signal, so individual apps can do whatever they need # to do at this point. emit_post_sync_signal(created_models, verbosity, interactive, db) # The connection may have been closed by a syncdb handler. cursor = connection.cursor() if verbosity >= 1: self.stdout.write("Installing indexes ...\n") # Install SQL indices for all newly created models for app_name, model_list in manifest.items(): for model in model_list: if model in created_models: index_sql = connection.creation.sql_indexes_for_model( model, self.style) if index_sql: if verbosity >= 2: self.stdout.write( "Installing index for %s.%s model\n" % (app_name, model._meta.object_name)) try: for sql in index_sql: cursor.execute(sql) except Exception as e: self.stderr.write("Failed to install index for %s.%s model: %s\n" % \ (app_name, model._meta.object_name, e)) transaction.rollback_unless_managed(using=db) else: transaction.commit_unless_managed(using=db) # Install custom SQL for the app (but only if this # is a model we've just created) if verbosity >= 1: self.stdout.write("Installing custom SQL ...\n") for app_name, model_list in manifest.items(): for model in model_list: if model in created_models: custom_sql = custom_sql_for_model(model, self.style, connection) if custom_sql: if verbosity >= 2: self.stdout.write( "Installing custom SQL for %s.%s model\n" % (app_name, model._meta.object_name)) try: for sql in custom_sql: cursor.execute(sql) except Exception as e: self.stderr.write("Failed to install custom SQL for %s.%s model: %s\n" % \ (app_name, model._meta.object_name, e)) if show_traceback: traceback.print_exc() transaction.rollback_unless_managed(using=db) else: transaction.commit_unless_managed(using=db) else: if verbosity >= 3: self.stdout.write( "No custom SQL for %s.%s model\n" % (app_name, model._meta.object_name)) # Load initial_data fixtures (unless that has been disabled) if load_initial_data: call_command('loaddata', 'initial_data', verbosity=verbosity, database=db, skip_validation=True)
def handle_noargs(self, **options): from django.db import connection, transaction, models from django.conf import settings from django.core.management.sql import table_names, installed_models, sql_model_create, sql_for_pending_references, many_to_many_sql_for_model, custom_sql_for_model, sql_indexes_for_model, emit_post_sync_signal verbosity = int(options.get('verbosity', 1)) interactive = options.get('interactive') show_traceback = options.get('traceback', False) self.style = no_style() # Import the 'management' module within each installed app, to register # dispatcher events. for app_name in settings.INSTALLED_APPS: try: __import__(app_name + '.management', {}, {}, ['']) except ImportError as exc: # This is slightly hackish. We want to ignore ImportErrors # if the "management" module itself is missing -- but we don't # want to ignore the exception if the management module exists # but raises an ImportError for some reason. The only way we # can do this is to check the text of the exception. Note that # we're a bit broad in how we check the text, because different # Python implementations may not use the same text. # CPython uses the text "No module named management" # PyPy uses "No module named myproject.myapp.management" msg = exc.args[0] if not msg.startswith( 'No module named') or 'management' not in msg: raise cursor = connection.cursor() if connection.features.uses_case_insensitive_names: table_name_converter = lambda x: x.upper() else: table_name_converter = lambda x: x # Get a list of all existing database tables, so we know what needs to # be added. tables = [table_name_converter(name) for name in table_names()] # Get a list of already installed *models* so that references work right. seen_models = installed_models(tables) created_models = set() pending_references = {} # Create the tables for each model for app in models.get_apps(): app_name = app.__name__.split('.')[-2] model_list = models.get_models(app) for model in model_list: # Create the model's database table, if it doesn't already exist. if verbosity >= 2: print("Processing %s.%s model" % (app_name, model._meta.object_name)) if table_name_converter(model._meta.db_table) in tables: continue sql, references = sql_model_create(model, self.style, seen_models) seen_models.add(model) created_models.add(model) for refto, refs in list(references.items()): pending_references.setdefault(refto, []).extend(refs) if refto in seen_models: sql.extend( sql_for_pending_references(refto, self.style, pending_references)) sql.extend( sql_for_pending_references(model, self.style, pending_references)) if verbosity >= 1: print("Creating table %s" % model._meta.db_table) for statement in sql: cursor.execute(statement) tables.append(table_name_converter(model._meta.db_table)) # Create the m2m tables. This must be done after all tables have been created # to ensure that all referred tables will exist. for app in models.get_apps(): app_name = app.__name__.split('.')[-2] model_list = models.get_models(app) for model in model_list: if model in created_models: sql = many_to_many_sql_for_model(model, self.style) if sql: if verbosity >= 2: print( "Creating many-to-many tables for %s.%s model" % (app_name, model._meta.object_name)) for statement in sql: cursor.execute(statement) transaction.commit_unless_managed() # Send the post_syncdb signal, so individual apps can do whatever they need # to do at this point. emit_post_sync_signal(created_models, verbosity, interactive) # The connection may have been closed by a syncdb handler. cursor = connection.cursor() # Install custom SQL for the app (but only if this # is a model we've just created) for app in models.get_apps(): app_name = app.__name__.split('.')[-2] for model in models.get_models(app): if model in created_models: custom_sql = custom_sql_for_model(model) if custom_sql: if verbosity >= 1: print("Installing custom SQL for %s.%s model" % (app_name, model._meta.object_name)) try: for sql in custom_sql: cursor.execute(sql) except Exception as e: sys.stderr.write("Failed to install custom SQL for %s.%s model: %s\n" % \ (app_name, model._meta.object_name, e)) if show_traceback: import traceback traceback.print_exc() transaction.rollback_unless_managed() else: transaction.commit_unless_managed() else: if verbosity >= 2: print("No custom SQL for %s.%s model" % (app_name, model._meta.object_name)) # Install SQL indicies for all newly created models for app in models.get_apps(): app_name = app.__name__.split('.')[-2] for model in models.get_models(app): if model in created_models: index_sql = sql_indexes_for_model(model, self.style) if index_sql: if verbosity >= 1: print("Installing index for %s.%s model" % (app_name, model._meta.object_name)) try: for sql in index_sql: cursor.execute(sql) except Exception as e: sys.stderr.write("Failed to install index for %s.%s model: %s\n" % \ (app_name, model._meta.object_name, e)) transaction.rollback_unless_managed() else: transaction.commit_unless_managed() # Install the 'initial_data' fixture, using format discovery from django.core.management import call_command call_command('loaddata', 'initial_data', verbosity=verbosity)
class Command(NoArgsCommand): option_list = NoArgsCommand.option_list + (make_option( '--noinput', action='store_false', dest='interactive', default=True, help='Tells Django to NOT prompt the user for input of any kind.'), ) help = "Create the database tables for all apps in INSTALLED_APPS whose tables haven't already been created." def handle_noargs(self, **options): from django.db import connection, transaction, models from django.conf import settings from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal verbosity = int(options.get('verbosity', 1)) interactive = options.get('interactive') show_traceback = options.get('traceback', False) self.style = no_style() # Import the 'management' module within each installed app, to register # dispatcher events. for app_name in settings.INSTALLED_APPS: try: __import__(app_name + '.management', {}, {}, ['']) except ImportError, exc: # This is slightly hackish. We want to ignore ImportErrors # if the "management" module itself is missing -- but we don't # want to ignore the exception if the management module exists # but raises an ImportError for some reason. The only way we # can do this is to check the text of the exception. Note that # we're a bit broad in how we check the text, because different # Python implementations may not use the same text. # CPython uses the text "No module named management" # PyPy uses "No module named myproject.myapp.management" msg = exc.args[0] if not msg.startswith( 'No module named') or 'management' not in msg: raise cursor = connection.cursor() # Get a list of already installed *models* so that references work right. tables = connection.introspection.table_names() seen_models = connection.introspection.installed_models(tables) created_models = set() pending_references = {} # Create the tables for each model for app in models.get_apps(): app_name = app.__name__.split('.')[-2] model_list = models.get_models(app) for model in model_list: # Create the model's database table, if it doesn't already exist. if verbosity >= 2: print "Processing %s.%s model" % (app_name, model._meta.object_name) if connection.introspection.table_name_converter( model._meta.db_table) in tables: continue sql, references = connection.creation.sql_create_model( model, self.style, seen_models) seen_models.add(model) created_models.add(model) for refto, refs in references.items(): pending_references.setdefault(refto, []).extend(refs) if refto in seen_models: sql.extend( connection.creation.sql_for_pending_references( refto, self.style, pending_references)) sql.extend( connection.creation.sql_for_pending_references( model, self.style, pending_references)) if verbosity >= 1: print "Creating table %s" % model._meta.db_table for statement in sql: cursor.execute(statement) tables.append( connection.introspection.table_name_converter( model._meta.db_table)) # Create the m2m tables. This must be done after all tables have been created # to ensure that all referred tables will exist. for app in models.get_apps(): app_name = app.__name__.split('.')[-2] model_list = models.get_models(app) for model in model_list: if model in created_models: sql = connection.creation.sql_for_many_to_many( model, self.style) if sql: if verbosity >= 2: print "Creating many-to-many tables for %s.%s model" % ( app_name, model._meta.object_name) for statement in sql: cursor.execute(statement) transaction.commit_unless_managed() # Send the post_syncdb signal, so individual apps can do whatever they need # to do at this point. emit_post_sync_signal(created_models, verbosity, interactive) # The connection may have been closed by a syncdb handler. cursor = connection.cursor() # Install custom SQL for the app (but only if this # is a model we've just created) for app in models.get_apps(): app_name = app.__name__.split('.')[-2] for model in models.get_models(app): if model in created_models: custom_sql = custom_sql_for_model(model, self.style) if custom_sql: if verbosity >= 1: print "Installing custom SQL for %s.%s model" % ( app_name, model._meta.object_name) try: for sql in custom_sql: cursor.execute(sql) except Exception, e: sys.stderr.write("Failed to install custom SQL for %s.%s model: %s\n" % \ (app_name, model._meta.object_name, e)) if show_traceback: import traceback traceback.print_exc() transaction.rollback_unless_managed() else: transaction.commit_unless_managed() else: if verbosity >= 2: print "No custom SQL for %s.%s model" % ( app_name, model._meta.object_name)
def execute_migrations(self, show_traceback=False): migrations = self._filter_down() if not len(migrations): sys.stdout.write("There are no migrations to apply.\n") created_models = set() try: for migration in migrations: migration_path = os.path.join(self.path, migration) fp = open(migration_path, "rb") lines = fp.readlines() fp.close() content = "".join(lines) if migration_path.endswith(".sql"): to_execute = "".join( [l for l in lines if not l.startswith("### New Model: ")] ) connection = connections[self.db] cursor = connection.cursor() sys.stdout.write("Executing %s... " % migration) try: cursor.execute(to_execute) except Exception: sys.stdout.write("failed\n") if show_traceback: traceback.print_exc() raise MigrationError() else: sys.stdout.write("success\n") for l in lines: if l.startswith("### New Model: "): created_models.add( get_model( *l.replace("### New Model: ", "").strip().split(".") ) ) elif migration_path.endswith(".py"): sys.stdout.write("Executing %s... " % migration) module = {} execfile(migration_path, {}, module) if "migrate" in module and callable(module["migrate"]): try: module["migrate"]() except Exception: sys.stdout.write("failed\n") if show_traceback: traceback.print_exc() raise MigrationError() else: sys.stdout.write("success\n") Migration.objects.create( migration_label=migration, content=content, scm_version=self._get_rev(migration_path) ) sys.stdout.write("Emitting post sync signal.\n") emit_post_sync_signal( created_models, self.verbosity, self.interactive, self.db ) sys.stdout.write("Running loaddata for initial_data fixtures.\n") call_command( "loaddata", "initial_data", verbosity=self.verbosity, database=self.db ) except Exception: transaction.rollback(using=self.db) sys.stdout.write("Rolled back all migrations.\n") raise else: transaction.commit(using=self.db)