Beispiel #1
0
    def handle(self, app=None, *args, **options):

        # Make sure we have an app
        if not app:
            print "Please specify an app to convert."
            return

        # See if the app exists
        app = app.split(".")[-1]
        try:
            app_module = models.get_app(app)
        except ImproperlyConfigured:
            print "There is no enabled application matching '%s'." % app
            return

        # Try to get its list of models
        model_list = models.get_models(app_module)
        if not model_list:
            print "This application has no models; this command is for applications that already have models syncdb'd."
            print "Make some models, and then use ./manage.py schemamigration %s --initial instead." % app
            return

        # Ask South if it thinks it's already got migrations
        try:
            Migrations(app)
        except NoMigrations:
            pass
        else:
            print "This application is already managed by South."
            return

        # Finally! It seems we've got a candidate, so do the two-command trick
        verbosity = int(options.get('verbosity', 0))
        management.call_command("schemamigration",
                                app,
                                initial=True,
                                verbosity=verbosity)

        # Now, we need to re-clean and sanitise appcache
        hacks.clear_app_cache()
        hacks.repopulate_app_cache()

        # And also clear our cached Migration classes
        Migrations._clear_cache()

        # Now, migrate
        management.call_command(
            "migrate",
            app,
            "0001",
            fake=True,
            verbosity=verbosity,
            ignore_ghosts=options.get("ignore_ghosts", False),
            delete_ghosts=options.get("delete_ghosts", False),
        )

        print
        print "App '%s' converted. Note that South assumed the application's models matched the database" % app
        print "(i.e. you haven't changed it since last syncdb); if you have, you should delete the %s/migrations" % app
        print "directory, revert models.py so it matches the database, and try again."
 def handle(self, app=None, *args, **options):
     
     # Make sure we have an app
     if not app:
         print("Please specify an app to convert.")
         return
     
     # See if the app exists
     app = app.split(".")[-1]
     try:
         app_module = models.get_app(app)
     except ImproperlyConfigured:
         print("There is no enabled application matching '%s'." % app)
         return
     
     # Try to get its list of models
     model_list = models.get_models(app_module)
     if not model_list:
         print("This application has no models; this command is for applications that already have models syncdb'd.")
         print("Make some models, and then use ./manage.py schemamigration %s --initial instead." % app)
         return
     
     # Ask South if it thinks it's already got migrations
     try:
         Migrations(app)
     except NoMigrations:
         pass
     else:
         print("This application is already managed by South.")
         return
     
     # Finally! It seems we've got a candidate, so do the two-command trick
     verbosity = int(options.get('verbosity', 0))
     management.call_command("schemamigration", app, initial=True, verbosity=verbosity)
     
     # Now, we need to re-clean and sanitise appcache
     hacks.clear_app_cache()
     hacks.repopulate_app_cache()
     
     # And also clear our cached Migration classes
     Migrations._clear_cache()
     
     # Now, migrate
     management.call_command(
         "migrate",
         app,
         "0001",
         fake=True,
         verbosity=verbosity,
         ignore_ghosts=options.get("ignore_ghosts", False),
         delete_ghosts=options.get("delete_ghosts", False),
     )
     
     print() 
     print("App '%s' converted. Note that South assumed the application's models matched the database" % app)
     print("(i.e. you haven't changed it since last syncdb); if you have, you should delete the %s/migrations" % app)
     print("directory, revert models.py so it matches the database, and try again.")
Beispiel #3
0
 def __init__(self, cls, app):
     self.default_app = app
     self.cls = cls
     # Try loading the models off the migration class; default to no models.
     self.models = {}
     try:
         self.models_source = cls.models
     except AttributeError:
         return
     
     # Start a 'new' AppCache
     hacks.clear_app_cache()
     
     # Now, make each model's data into a FakeModel
     # We first make entries for each model that are just its name
     # This allows us to have circular model dependency loops
     model_names = []
     for name, data in self.models_source.items():
         # Make sure there's some kind of Meta
         if "Meta" not in data:
             data['Meta'] = {}
         try:
             app_label, model_name = name.split(".", 1)
         except ValueError:
             app_label = self.default_app
             model_name = name
             name = "%s.%s" % (app_label, model_name)
         
         # If there's an object_name in the Meta, use it and remove it
         if "object_name" in data['Meta']:
             model_name = data['Meta']['object_name']
             del data['Meta']['object_name']
         
         name = name.lower()
         self.models[name] = name
         model_names.append((name, app_label, model_name, data))
     
     for name, app_label, model_name, data in model_names:
         self.models[name] = self.make_model(app_label, model_name, data)
     
     # And perform the second run to iron out any circular/backwards depends.
     self.retry_failed_fields()
     
     # Force evaluation of relations on the models now
     for model in self.models.values():
         model._meta.get_all_field_names()
     
     # Reset AppCache
     hacks.unclear_app_cache()
Beispiel #4
0
    def __init__(self, cls, app):
        self.default_app = app
        self.cls = cls
        # Try loading the models off the migration class; default to no models.
        self.models = {}
        try:
            self.models_source = cls.models
        except AttributeError:
            return

        # Start a 'new' AppCache
        hacks.clear_app_cache()

        # Now, make each model's data into a FakeModel
        # We first make entries for each model that are just its name
        # This allows us to have circular model dependency loops
        model_names = []
        for name, data in self.models_source.items():
            # Make sure there's some kind of Meta
            if "Meta" not in data:
                data['Meta'] = {}
            try:
                app_name, model_name = name.split(".", 1)
            except ValueError:
                app_name = self.default_app
                model_name = name
                name = "%s.%s" % (app_name, model_name)

            name = name.lower()
            self.models[name] = name
            model_names.append((name, app_name, model_name, data))

        for name, app_name, model_name, data in model_names:
            self.models[name] = self.make_model(app_name, model_name, data)

        # And perform the second run to iron out any circular/backwards depends.
        self.retry_failed_fields()

        # Force evaluation of relations on the models now
        for model in self.models.values():
            model._meta.get_all_field_names()

        # Reset AppCache
        hacks.unclear_app_cache()
Beispiel #5
0
 def __init__(self, cls, app):
     self.default_app = app
     self.cls = cls
     # Try loading the models off the migration class; default to no models.
     self.models = {}
     try:
         self.models_source = cls.models
     except AttributeError:
         return
     
     # Start a 'new' AppCache
     hacks.clear_app_cache()
     
     # Now, make each model's data into a FakeModel
     for name, data in self.models_source.items():
         # Make sure there's some kind of Meta
         if "Meta" not in data:
             data['Meta'] = {}
         try:
             app_name, model_name = name.split(".", 1)
         except ValueError:
             app_name = self.default_app
             model_name = name
             name = "%s.%s" % (app_name, model_name)
         
         self.models[name.lower()] = self.make_model(app_name, model_name, data)
     
     # And perform the second run to iron out any circular/backwards depends.
     self.retry_failed_fields()
     
     # Force evaluation of relations on the models now
     for model in self.models.values():
         model._meta.get_all_field_names()
     
     # Reset AppCache
     hacks.unclear_app_cache()
Beispiel #6
0
    def __init__(self, cls, app):
        self.default_app = app
        self.cls = cls
        # Try loading the models off the migration class; default to no models.
        self.models = {}
        try:
            self.models_source = cls.models
        except AttributeError:
            return

        # Start a 'new' AppCache
        hacks.clear_app_cache()

        # Now, make each model's data into a FakeModel
        # We first make entries for each model that are just its name
        # This allows us to have circular model dependency loops
        model_names = []
        for name, data in self.models_source.items():
            # Make sure there's some kind of Meta
            if "Meta" not in data:
                data['Meta'] = {}
            try:
                app_label, model_name = name.split(".", 1)
            except ValueError:
                app_label = self.default_app
                model_name = name

            # If there's an object_name in the Meta, use it and remove it
            if "object_name" in data['Meta']:
                model_name = data['Meta']['object_name']
                del data['Meta']['object_name']

            name = "%s.%s" % (app_label, model_name)
            self.models[name.lower()] = name
            model_names.append((name.lower(), app_label, model_name, data))

        # Loop until model_names is entry, or hasn't shrunk in size since
        # last iteration.
        # The make_model method can ask to postpone a model; it's then pushed
        # to the back of the queue. Because this is currently only used for
        # inheritance, it should thus theoretically always decrease by one.
        last_size = None
        while model_names:
            # First, make sure we've shrunk.
            if len(model_names) == last_size:
                raise ImpossibleORMUnfreeze()
            last_size = len(model_names)
            # Make one run through
            postponed_model_names = []
            for name, app_label, model_name, data in model_names:
                try:
                    self.models[name] = self.make_model(
                        app_label, model_name, data)
                except UnfreezeMeLater:
                    postponed_model_names.append(
                        (name, app_label, model_name, data))
            # Reset
            model_names = postponed_model_names

        # And perform the second run to iron out any circular/backwards depends.
        self.retry_failed_fields()

        # Force evaluation of relations on the models now
        for model in self.models.values():
            model._meta.get_all_field_names()

        # Reset AppCache
        hacks.unclear_app_cache()
Beispiel #7
0
 def __init__(self, cls, app):
     self.default_app = app
     self.cls = cls
     # Try loading the models off the migration class; default to no models.
     self.models = {}
     try:
         self.models_source = cls.models
     except AttributeError:
         return
     
     # Start a 'new' AppCache
     hacks.clear_app_cache()
     
     # Now, make each model's data into a FakeModel
     # We first make entries for each model that are just its name
     # This allows us to have circular model dependency loops
     model_names = []
     for name, data in self.models_source.items():
         # Make sure there's some kind of Meta
         if "Meta" not in data:
             data['Meta'] = {}
         try:
             app_label, model_name = name.split(".", 1)
         except ValueError:
             app_label = self.default_app
             model_name = name
         
         # If there's an object_name in the Meta, use it and remove it
         if "object_name" in data['Meta']:
             model_name = data['Meta']['object_name']
             del data['Meta']['object_name']
         
         name = "%s.%s" % (app_label, model_name)
         self.models[name.lower()] = name
         model_names.append((name.lower(), app_label, model_name, data))
     
     # Loop until model_names is entry, or hasn't shrunk in size since
     # last iteration.
     # The make_model method can ask to postpone a model; it's then pushed
     # to the back of the queue. Because this is currently only used for
     # inheritance, it should thus theoretically always decrease by one.
     last_size = None
     while model_names:
         # First, make sure we've shrunk.
         if len(model_names) == last_size:
             raise ImpossibleORMUnfreeze()
         last_size = len(model_names)
         # Make one zrun through
         postponed_model_names = []
         for name, app_label, model_name, data in model_names:
             try:
                 self.models[name] = self.make_model(app_label, model_name, data)
             except UnfreezeMeLater:
                 postponed_model_names.append((name, app_label, model_name, data))
         # Reset
         model_names = postponed_model_names
     
     # And perform the second zrun to iron out any circular/backwards depends.
     self.retry_failed_fields()
     
     # Force evaluation of relations on the models now
     for model in self.models.values():
         model._meta.get_all_field_names()
     
     # Reset AppCache
     hacks.unclear_app_cache()