Beispiel #1
0
    def seed_migrations(self, stop_at=None):
        # @@@ the command-line interface needs to be re-thinked
        # TODO: this needs to be able to handle multi-db when you're
        #       specifying stop_at
        if stop_at is None and self.args:
            stop_at = self.args[0]

        if stop_at:
            try:
                stop_at = int(stop_at)
            except ValueError:
                raise CommandError("Invalid --seed migration")
            except IndexError:
                raise CommandError(
                    "Usage: ./manage.py upgradedb --seed [stop_at]")

        all_migrations = get_pending_migrations(self.path,
                                                self.databases,
                                                stop_at=stop_at)
        for db, migrations in all_migrations.iteritems():
            for migration in migrations:
                migration_path = self._get_migration_path(db, migration)

                m, created = Migration.objects.using(db).get_or_create(
                    migration_label=os.path.split(migration)[-1],
                    content=open(migration_path, "rb").read())
                if created:
                    # this might have been executed prior to committing
                    m.scm_version = self._get_rev(migration)
                    m.save()
                    print "%s:%s has been seeded" % (db, m.migration_label)
                else:
                    print "%s:%s was already applied" % (db, m.migration_label)
Beispiel #2
0
 def list_migrations(self):
     all_migrations = get_pending_migrations(self.path, self.databases)
     if len(all_migrations) == 0:
         print "There are no migrations to apply."
         return
     
     print "Migrations to Apply:"
     for database, migrations in all_migrations.iteritems():
         for script in migrations:
             print "\t%s: %s" % (database, script)
Beispiel #3
0
    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,
                )
Beispiel #4
0
    def test_handles_duplicate_migration_numbers(self, get_applied_migrations, get_all_migrations):
        get_applied_migrations.return_value = {
            'dupes': ['0001.sql', '0002_foo.sql'],
        }
        get_all_migrations.return_value = {
            'dupes': [(1, '0001.sql'), (2, '0002_bar.sql'), (2, '0002_foo.sql')],
        }

        path = join(mig_root, 'multidb')
        results = dict(get_pending_migrations(path))

        get_applied_migrations.assert_called_once_with(None)
        get_all_migrations.assert_called_once_with(path, None)

        self.assertEquals(len(results), 1)
        self.assertTrue('dupes' in results)
        self.assertEquals(len(results['dupes']), 1)
        self.assertTrue('0002_bar.sql' in results['dupes'])
Beispiel #5
0
    def seed_migrations(self, stop_at=None):
        # @@@ the command-line interface needs to be re-thinked
        # TODO: this needs to be able to handle multi-db when you're
        #       specifying stop_at
        if stop_at is None and self.args:
            stop_at = self.args[0]
        
        if stop_at:
            try:
                stop_at = int(stop_at)
            except ValueError:
                raise CommandError("Invalid --seed migration")
            except IndexError:
                raise CommandError(
                    "Usage: ./manage.py upgradedb --seed [stop_at]"
                )

        all_migrations = get_pending_migrations(
            self.path, self.databases, stop_at=stop_at
        )
        for db, migrations in all_migrations.iteritems():
            for migration in migrations:
                migration_path = self._get_migration_path(db, migration)

                m, created = Migration.objects.using(db).get_or_create(
                    migration_label=os.path.split(migration)[-1],
                    content=open(migration_path, "rb").read()
                )
                if created:
                    # this might have been executed prior to committing
                    m.scm_version = self._get_rev(migration)
                    m.save()
                    print "%s:%s has been seeded" % (db, m.migration_label)
                else:
                    print "%s:%s was already applied" % (
                        db, m.migration_label
                    )