Exemplo n.º 1
0
def migrate_app(migrations, target_name=None, merge=False, fake=False, db_dry_run=False, yes=False, verbosity=0, load_initial_data=False, skip=False, database=DEFAULT_DB_ALIAS, delete_ghosts=False, ignore_ghosts=False, interactive=False):
    app_label = migrations.app_label()

    verbosity = int(verbosity)
    # Fire off the pre-migrate signal
    pre_migrate.send(None, app=app_label)
    
    # If there aren't any, quit quizically
    if not migrations:
        print "? You have no migrations for the '%s' app. You might want some." % app_label
        return
    
    # Load the entire dependency graph
    Migrations.calculate_dependencies()
    
    # Check there's no strange ones in the database
    applied = MigrationHistory.objects.filter(applied__isnull=False)
    # If we're using a different database, use that
    if database != DEFAULT_DB_ALIAS:
        applied = applied.using(database)
        south.db.db = south.db.dbs[database]
        # We now have to make sure the migrations are all reloaded, as they'll
        # have imported the old value of south.db.db.
        Migrations.invalidate_all_modules()
    
    south.db.db.debug = (verbosity > 1)
    applied = check_migration_histories(applied, delete_ghosts, ignore_ghosts)
    
    # Guess the target_name
    target = migrations.guess_migration(target_name)
    if verbosity:
        if target_name not in ('zero', None) and target.name() != target_name:
            print " - Soft matched migration %s to %s." % (target_name,
                                                           target.name())
        print "Running migrations for %s:" % app_label
    
    # Get the forwards and reverse dependencies for this target
    direction, problems, workplan = get_direction(target, applied, migrations,
                                                  verbosity, interactive)
    if problems and not (merge or skip):
        raise exceptions.InconsistentMigrationHistory(problems)
    
    # Perform the migration
    migrator = get_migrator(direction, db_dry_run, fake, load_initial_data)
    if migrator:
        migrator.print_title(target)
        success = migrator.migrate_many(target, workplan, database)
        # Finally, fire off the post-migrate signal
        if success:
            post_migrate.send(None, app=app_label)
    elif verbosity:
        # Say there's nothing.
        print '- Nothing to migrate.'
        # If we have initial data enabled, and we're at the most recent
        # migration, do initial data.
        # Note: We use a fake Forwards() migrator here. It's never used really.
        migrator = LoadInitialDataMigrator(migrator=Forwards(verbosity=verbosity))
        migrator.load_initial_data(target)
        # Send signal.
        post_migrate.send(None, app=app_label)
Exemplo n.º 2
0
def migrate_app(migrations,
                target_name=None,
                merge=False,
                fake=False,
                db_dry_run=False,
                yes=False,
                verbosity=0,
                load_initial_data=False,
                skip=False,
                database=DEFAULT_DB_ALIAS,
                delete_ghosts=False):
    app_label = migrations.app_label()

    verbosity = int(verbosity)
    #db.debug = (verbosity > 1)
    # Fire off the pre-migrate signal
    pre_migrate.send(None, app=app_label)

    # If there aren't any, quit quizically
    if not migrations:
        print "? You have no migrations for the '%s' app. You might want some." % app_label
        return

    # Load the entire dependency graph
    Migrations.calculate_dependencies()

    # Check there's no strange ones in the database
    applied = MigrationHistory.objects.filter(applied__isnull=False)
    # If we're using a different database, use that
    if database != DEFAULT_DB_ALIAS:
        applied = applied.using(database)
        south.db.db = south.db.dbs[database]
    applied = check_migration_histories(applied, delete_ghosts)

    # Guess the target_name
    target = migrations.guess_migration(target_name)
    if verbosity:
        if target_name not in ('zero', None) and target.name() != target_name:
            print " - Soft matched migration %s to %s." % (target_name,
                                                           target.name())
        print "Running migrations for %s:" % app_label

    # Get the forwards and reverse dependencies for this target
    direction, problems, workplan = get_direction(target, applied, migrations,
                                                  verbosity)
    if problems and not (merge or skip):
        raise exceptions.InconsistentMigrationHistory(problems)

    # Perform the migration
    migrator = get_migrator(direction, db_dry_run, fake, load_initial_data)
    if migrator:
        migrator.print_title(target)
        success = migrator.migrate_many(target, workplan)
        # Finally, fire off the post-migrate signal
        if success:
            post_migrate.send(None, app=app_label)
    elif verbosity:
        print '- Nothing to migrate.'
        post_migrate.send(None, app=app_label)
Exemplo n.º 3
0
 def test_plans(self):
     Migrations.calculate_dependencies()
     circular_a = Migrations("circular_a")
     circular_b = Migrations("circular_b")
     self.assertRaises(exceptions.CircularDependency, circular_a[-1].forwards_plan)
     self.assertRaises(exceptions.CircularDependency, circular_b[-1].forwards_plan)
     self.assertRaises(exceptions.CircularDependency, circular_a[-1].backwards_plan)
     self.assertRaises(exceptions.CircularDependency, circular_b[-1].backwards_plan)
Exemplo n.º 4
0
 def test_plans(self):
     Migrations.calculate_dependencies(force=True)
     circular_a = Migrations('circular_a')
     circular_b = Migrations('circular_b')
     self.assertRaises(
         exceptions.CircularDependency,
         circular_a[-1].forwards_plan,
     )
     self.assertRaises(
         exceptions.CircularDependency,
         circular_b[-1].forwards_plan,
     )
     self.assertRaises(
         exceptions.CircularDependency,
         circular_a[-1].backwards_plan,
     )
     self.assertRaises(
         exceptions.CircularDependency,
         circular_b[-1].backwards_plan,
     )
Exemplo n.º 5
0
 def test_plans(self):
     Migrations.calculate_dependencies()
     circular_a = Migrations('circular_a')
     circular_b = Migrations('circular_b')
     self.assertRaises(
         exceptions.CircularDependency,
         circular_a[-1].forwards_plan,
     )
     self.assertRaises(
         exceptions.CircularDependency,
         circular_b[-1].forwards_plan,
     )
     self.assertRaises(
         exceptions.CircularDependency,
         circular_a[-1].backwards_plan,
     )
     self.assertRaises(
         exceptions.CircularDependency,
         circular_b[-1].backwards_plan,
     )
Exemplo n.º 6
0
 def setUp(self):
     super(TestMigration, self).setUp()
     self.fakeapp = Migrations('fakeapp')
     self.otherfakeapp = Migrations('otherfakeapp')
     Migrations.calculate_dependencies(force=True)
Exemplo n.º 7
0
 def setUp(self):
     super(TestMigrationDependencies, self).setUp()
     self.deps_a = Migrations('deps_a')
     self.deps_b = Migrations('deps_b')
     self.deps_c = Migrations('deps_c')
     Migrations.calculate_dependencies(force=True)
Exemplo n.º 8
0
 def setUp(self):
     super(TestMigration, self).setUp()
     self.fakeapp = Migrations('fakeapp')
     self.otherfakeapp = Migrations('otherfakeapp')
     Migrations.calculate_dependencies()
Exemplo n.º 9
0
 def setUp(self):
     super(TestMigrationDependencies, self).setUp()
     self.deps_a = Migrations('deps_a')
     self.deps_b = Migrations('deps_b')
     self.deps_c = Migrations('deps_c')
     Migrations.calculate_dependencies()
Exemplo n.º 10
0
 def setUp(self):
     super(TestMigration, self).setUp()
     self.fakeapp = Migrations("fakeapp")
     self.otherfakeapp = Migrations("otherfakeapp")
     Migrations.calculate_dependencies()
Exemplo n.º 11
0
def migrate_app(migrations,
                target_name=None,
                merge=False,
                fake=False,
                db_dry_run=False,
                yes=False,
                verbosity=0,
                skip=False,
                database=DEFAULT_DB_ALIAS,
                delete_ghosts=False,
                ignore_ghosts=False,
                interactive=False):
    app_label = migrations.app_label()

    verbosity = int(verbosity)
    # Fire off the pre-migrate signal
    pre_migrate.send(None,
                     app=app_label,
                     verbosity=verbosity,
                     interactive=verbosity,
                     db=database)

    # If there aren't any, quit quizically
    if not migrations:
        print(
            "? You have no migrations for the '%s' app. You might want some." %
            app_label)
        return

    # Load the entire dependency graph
    Migrations.calculate_dependencies()

    # Check there's no strange ones in the database
    applied_all = MigrationHistory.objects.filter(
        applied__isnull=False).order_by('applied').using(database)
    applied = applied_all.filter(app_name=app_label).using(database)
    south.db.db = south.db.dbs[database]
    Migrations.invalidate_all_modules()

    south.db.db.debug = (verbosity > 1)

    if target_name == 'current-1':
        if applied.count() > 1:
            previous_migration = applied[applied.count() - 2]
            if verbosity:
                print(
                    'previous_migration: %s (applied: %s)' %
                    (previous_migration.migration, previous_migration.applied))
            target_name = previous_migration.migration
        else:
            if verbosity:
                print('previous_migration: zero')
            target_name = 'zero'
    elif target_name == 'current+1':
        try:
            first_unapplied_migration = get_unapplied_migrations(
                migrations, applied).next()
            target_name = first_unapplied_migration.name()
        except StopIteration:
            target_name = None

    applied_all = check_migration_histories(applied_all, delete_ghosts,
                                            ignore_ghosts)

    # Guess the target_name
    target = migrations.guess_migration(target_name)
    if verbosity:
        if target_name not in ('zero', None) and target.name() != target_name:
            print(" - Soft matched migration %s to %s." %
                  (target_name, target.name()))
        print("Running migrations for %s:" % app_label)

    # Get the forwards and reverse dependencies for this target
    direction, problems, workplan = get_direction(target, applied_all,
                                                  migrations, verbosity,
                                                  interactive)
    if problems and not (merge or skip):
        raise exceptions.InconsistentMigrationHistory(problems)

    # Perform the migration
    migrator = get_migrator(direction, db_dry_run, fake)
    if migrator:
        migrator.print_title(target)
        success = migrator.migrate_many(target, workplan, database)
        # Finally, fire off the post-migrate signal
        if success:
            post_migrate.send(None,
                              app=app_label,
                              verbosity=verbosity,
                              interactive=verbosity,
                              db=database)
    else:
        if verbosity:
            # Say there's nothing.
            print('- Nothing to migrate.')
        # Send signal.
        post_migrate.send(None,
                          app=app_label,
                          verbosity=verbosity,
                          interactive=verbosity,
                          db=database)
Exemplo n.º 12
0
def migrate_app(migrations, target_name=None, merge=False, fake=False, db_dry_run=False, yes=False, verbosity=0, load_initial_data=False, skip=False, database=DEFAULT_DB_ALIAS, delete_ghosts=False, ignore_ghosts=False, interactive=False):
    app_label = migrations.app_label()

    verbosity = int(verbosity)
    # Fire off the pre-migrate signal
    pre_migrate.send(None, app=app_label)
    
    # If there aren't any, quit quizically
    if not migrations:
        print "? You have no migrations for the '%s' app. You might want some." % app_label
        return
    
    # Load the entire dependency graph
    Migrations.calculate_dependencies()
    
    # Check there's no strange ones in the database
    applied_all = MigrationHistory.objects.filter(applied__isnull=False).order_by('applied').using(database)
    applied = applied_all.filter(app_name=app_label).using(database)
    south.db.db = south.db.dbs[database]
    Migrations.invalidate_all_modules()
    
    south.db.db.debug = (verbosity > 1)

    if target_name == 'current-1':
        if applied.count() > 1:
            previous_migration = applied[applied.count() - 2]
            if verbosity:
                print 'previous_migration: %s (applied: %s)' % (previous_migration.migration, previous_migration.applied)
            target_name = previous_migration.migration
        else:
            if verbosity:
                print 'previous_migration: zero'
            target_name = 'zero'
    elif target_name == 'current+1':
        try:
            first_unapplied_migration = get_unapplied_migrations(migrations, applied).next()
            target_name = first_unapplied_migration.name()
        except StopIteration:
            target_name = None
    
    applied_all = check_migration_histories(applied_all, delete_ghosts, ignore_ghosts)
    
    # Guess the target_name
    target = migrations.guess_migration(target_name)
    if verbosity:
        if target_name not in ('zero', None) and target.name() != target_name:
            print " - Soft matched migration %s to %s." % (target_name,
                                                           target.name())
        print "Running migrations for %s:" % app_label
    
    # Get the forwards and reverse dependencies for this target
    direction, problems, workplan = get_direction(target, applied_all, migrations,
                                                  verbosity, interactive)
    if problems and not (merge or skip):
        raise exceptions.InconsistentMigrationHistory(problems)
    
    # Perform the migration
    migrator = get_migrator(direction, db_dry_run, fake, load_initial_data)
    if migrator:
        migrator.print_title(target)
        success = migrator.migrate_many(target, workplan, database)
        # Finally, fire off the post-migrate signal
        if success:
            post_migrate.send(None, app=app_label)
    else:
        if verbosity:
            # Say there's nothing.
            print '- Nothing to migrate.'
        # If we have initial data enabled, and we're at the most recent
        # migration, do initial data.
        # Note: We use a fake Forwards() migrator here. It's never used really.
        if load_initial_data:
            migrator = LoadInitialDataMigrator(migrator=Forwards(verbosity=verbosity))
            migrator.load_initial_data(target, db=database)
        # Send signal.
        post_migrate.send(None, app=app_label)