Exemplo n.º 1
0
    def run_backlog(self, models, force, update, using=DEFAULT_DB_ALIAS):
        """Installs the list of models given from the previous backlog

        If the correct dependent views have not been installed, the view
        will be added to the backlog.

        Eventually we get to a point where all dependencies are sorted.
        """
        connection = connections[using]
        backlog = []
        for view_cls in models:
            skip = False
            name = "{}.{}".format(view_cls._meta.app_label, view_cls.__name__)
            for dep in view_cls._dependencies:
                if dep not in self.synced:
                    skip = True
            if skip is True:
                backlog.append(view_cls)
                log.info("Putting pgview at back of queue: %s", name)
                continue  # Skip

            try:
                status = create_view(
                    connection,
                    view_cls._meta.db_table,
                    view_cls.sql,
                    update=update,
                    force=force,
                    materialized=isinstance(view_cls(), MaterializedView),
                    index=view_cls._concurrent_index,
                )
                view_synced.send(
                    sender=view_cls,
                    update=update,
                    force=force,
                    status=status,
                    has_changed=status not in ("EXISTS", "FORCE_REQUIRED"),
                    using=using,
                )
                self.synced.append(name)
            except Exception as exc:
                exc.view_cls = view_cls
                exc.python_name = name
                raise
            else:
                if status == "CREATED":
                    msg = "created"
                elif status == "UPDATED":
                    msg = "updated"
                elif status == "EXISTS":
                    msg = "already exists, skipping"
                elif status == "FORCED":
                    msg = "forced overwrite of existing schema"
                elif status == "FORCE_REQUIRED":
                    msg = "exists with incompatible schema, " "--force required to update"
                log.info("pgview %(python_name)s %(msg)s" % {
                    "python_name": name,
                    "msg": msg
                })
        return backlog
Exemplo n.º 2
0
    def run_backlog(self, models, force, update):
        '''Installs the list of models given from the previous backlog

        If the correct dependent views have not been installed, the view
        will be added to the backlog.

        Eventually we get to a point where all dependencies are sorted.
        '''
        backlog = []
        for view_cls in models:
            skip = False
            name = '{}.{}'.format(view_cls._meta.app_label, view_cls.__name__)
            for dep in view_cls._dependencies:
                if dep not in self.synced:
                    skip = True
            if skip is True:
                backlog.append(view_cls)
                log.info('Putting pgview at back of queue: %s', name)
                continue  # Skip

            try:
                using = router.db_for_write(view_cls)
                connection = connections[using]
                status = create_view(connection,
                                     view_cls._meta.db_table,
                                     view_cls.sql,
                                     update=update,
                                     force=force,
                                     materialized=isinstance(
                                         view_cls(), MaterializedView),
                                     index=view_cls._concurrent_index)
                view_synced.send(sender=view_cls,
                                 update=update,
                                 force=force,
                                 status=status,
                                 has_changed=status
                                 not in ('EXISTS', 'FORCE_REQUIRED'))
                self.synced.append(name)
            except Exception as exc:
                exc.view_cls = view_cls
                exc.python_name = name
                raise
            else:
                if status == 'CREATED':
                    msg = "created"
                elif status == 'UPDATED':
                    msg = "updated"
                elif status == 'EXISTS':
                    msg = "already exists, skipping"
                elif status == 'FORCED':
                    msg = "forced overwrite of existing schema"
                elif status == 'FORCE_REQUIRED':
                    msg = ("exists with incompatible schema, "
                           "--force required to update")
                log.info("pgview %(python_name)s %(msg)s" % {
                    'python_name': name,
                    'msg': msg
                })
        return backlog
Exemplo n.º 3
0
    def run_backlog(self, models, force, update):
        '''Installs the list of models given from the previous backlog

        If the correct dependent views have not been installed, the view
        will be added to the backlog.

        Eventually we get to a point where all dependencies are sorted.
        '''
        backlog = []
        for view_cls in models:
            skip = False
            name = '{}.{}'.format(view_cls._meta.app_label, view_cls.__name__)
            for dep in view_cls._dependencies:
                if dep not in self.synced:
                    skip = True
            if skip is True:
                backlog.append(view_cls)
                log.info('Putting pgview at back of queue: %s', name)
                continue # Skip

            try:
                status = create_view(connection, view_cls._meta.db_table,
                        view_cls.sql, update=update, force=force,
                        materialized=isinstance(view_cls(), MaterializedView),
                        index=view_cls._concurrent_index)
                view_synced.send(
                    sender=view_cls, update=update, force=force, status=status,
                    has_changed=status not in ('EXISTS', 'FORCE_REQUIRED'))
                self.synced.append(name)
            except Exception as exc:
                exc.view_cls = view_cls
                exc.python_name = name
                raise
            else:
                if status == 'CREATED':
                    msg = "created"
                elif status == 'UPDATED':
                    msg = "updated"
                elif status == 'EXISTS':
                    msg = "already exists, skipping"
                elif status == 'FORCED':
                    msg = "forced overwrite of existing schema"
                elif status == 'FORCE_REQUIRED':
                    msg = (
                        "exists with incompatible schema, "
                        "--force required to update")
                log.info("pgview %(python_name)s %(msg)s" % {
                    'python_name': name,
                    'msg': msg})
        return backlog
Exemplo n.º 4
0
    def run_backlog(self, backlog, *, force, update, using,
                    materialized_views_check_sql_changed, **kwargs):
        """Installs the list of models given from the previous backlog

        If the correct dependent views have not been installed, the view
        will be added to the backlog.

        Eventually we get to a point where all dependencies are sorted.
        """
        new_backlog = []
        for view_cls in backlog:
            skip = False
            name = "{}.{}".format(view_cls._meta.app_label, view_cls.__name__)
            for dep in view_cls._dependencies:
                if dep not in self.finished:
                    skip = True
                    break

            if skip is True:
                new_backlog.append(view_cls)
                logger.info("Putting pgview at back of queue: %s", name)
                continue  # Skip

            try:
                connection = view_cls.get_view_connection(using=using)
                if not connection:
                    logger.info(
                        "Skipping pgview %s (migrations not allowed on %s)",
                        name, using)
                    continue  # Skip
                if isinstance(view_cls(), MaterializedView):
                    status = create_materialized_view(
                        connection,
                        view_cls,
                        check_sql_changed=materialized_views_check_sql_changed)
                else:
                    status = create_view(
                        connection,
                        view_cls._meta.db_table,
                        view_cls.get_sql(),
                        update=update,
                        force=force,
                    )

                view_synced.send(
                    sender=view_cls,
                    update=update,
                    force=force,
                    status=status,
                    has_changed=status not in ("EXISTS", "FORCE_REQUIRED"),
                    using=using,
                )
                self.finished.append(name)
            except Exception as exc:
                exc.view_cls = view_cls
                exc.python_name = name
                raise
            else:
                use_logger = logger

                if status == "CREATED":
                    msg = "created"
                elif status == "UPDATED":
                    msg = "updated"
                elif status == "EXISTS":
                    msg = "already exists, skipping"
                    use_logger = exists_logger
                elif status == "FORCED":
                    msg = "forced overwrite of existing schema"
                elif status == "FORCE_REQUIRED":
                    msg = "exists with incompatible schema, --force required to update"
                else:
                    msg = status

                use_logger.info("pgview %s %s", name, msg)
        return new_backlog
Exemplo n.º 5
0
    def run_backlog(self, models, force, update):
        """Installs the list of models given from the previous backlog

        If the correct dependent views have not been installed, the view
        will be added to the backlog.

        Eventually we get to a point where all dependencies are sorted.
        """
        backlog = []
        for view_cls in models:
            skip = False
            name = "{}.{}".format(view_cls._meta.app_label, view_cls.__name__)
            for dep in view_cls._dependencies:
                if dep not in self.synced:
                    skip = True
            if skip is True:
                backlog.append(view_cls)
                log.info("Putting pgview at back of queue: %s", name)
                continue  # Skip

            try:
                app_label = ContentType.objects.get_for_model(
                    view_cls).app_label
                if hasattr(
                        settings,
                        "TENANT_APPS") and app_label in settings.TENANT_APPS:
                    from tenant_schemas.utils import get_public_schema_name, get_tenant_model, schema_exists

                    tenants = (get_tenant_model().objects.exclude(
                        schema_name=get_public_schema_name()).values_list(
                            "schema_name", flat=True))
                else:
                    tenants = ["public"]
                status = "EXISTS"
                for tenant in tenants:
                    try:
                        connection.set_schema(tenant)
                        log.info("Switched to %s schema for %s", tenant,
                                 view_cls._meta.db_table)
                    except:
                        pass
                    status = create_view(
                        connection,
                        view_cls._meta.db_table,
                        string.Template(
                            view_cls.sql).safe_substitute(tenant=tenant),
                        update=update,
                        force=force,
                        materialized=isinstance(view_cls(), MaterializedView),
                        index=view_cls._concurrent_index,
                        column_indexes=view_cls._column_indexes,
                        tenant_schema=tenant,
                        is_function=isinstance(view_cls(), PLPGSQLFunction),
                        function_signature=view_cls._function_signature)
                    try:
                        connection.set_schema_to_public()
                    except:
                        pass

                view_synced.send(
                    sender=view_cls,
                    update=update,
                    force=force,
                    status=status,
                    has_changed=status not in ("EXISTS", "FORCE_REQUIRED"),
                )
                self.synced.append(name)
            except Exception as exc:
                exc.view_cls = view_cls
                exc.python_name = name
                msg = "failer, skipping"
                raise
            else:
                if status == "CREATED":
                    msg = "created"
                elif status == "UPDATED":
                    msg = "updated"
                elif status == "EXISTS":
                    msg = "already exists, skipping"
                elif status == "FORCED":
                    msg = "forced overwrite of existing schema"
                elif status == "FORCE_REQUIRED":
                    msg = "exists with incompatible schema, " "--force required to update"
                log.info("pgview %(python_name)s %(msg)s" % {
                    "python_name": name,
                    "msg": msg
                })
        return backlog