class DatabaseRouter:
    """
    A router to control all database operations for specific apps
    targeting separate databases.
    """

    route_app_labels = {
        "legacy": "OB2011DB",
        "trans": "MGT_Trans",
    }

    def db_for_read(self, model, **hints):
        """
        Direct read operations to the appropriate database.
        """
        return self.route_app_labels.get(model._meta.app_label, "default")

    def db_for_write(self, model, **hints):
        """
        Direct write operations to the appropriate database.
        """
        return self.route_app_labels.get(model._meta.app_label, "default")

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if both objects belong to the same database.
        """
        db_set = {"default", "OB2011DB", "MGT_Trans"}
        if obj1._state.db in db_set and obj2._state.db in db_set:
            return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        Ensure migrations are applied only to the appropriate database.
        """
        if app_label in self.route_app_labels:
            return db == self.route_app_labels[app_label]
        return db == "default"
