Exemple #1
0
    def invoke(self, ctx):
        from splitgraph.engine import get_engine

        engine = get_engine()
        try:
            result = super(click.Group, self).invoke(ctx)
            engine.commit()
            return result
        except Exception as exc:
            engine.rollback()
            if isinstance(
                    exc,
                (click.exceptions.ClickException, click.exceptions.Abort,
                 click.exceptions.Exit),
            ):
                raise
            # Can't seem to be able to get the click_log verbosity option
            # value so have to get it indirectly. Basically, if we're in
            # DEBUG mode, output the whole stacktrace.
            if logger.getEffectiveLevel() == logging.DEBUG:
                logger.error(traceback.format_exc())
            else:
                logger.error("%s: %s" % (get_exception_name(exc), exc))
            ctx.exit(code=2)
        finally:
            _do_version_check()
            engine.close()
Exemple #2
0
    def preview(self, tables: Optional[TableInfo]) -> PreviewResult:
        # Preview data in tables mounted by this FDW / data source

        # Local import here since this data source gets imported by the commandline entry point
        from splitgraph.core.common import get_temporary_table_id

        tmp_schema = get_temporary_table_id()
        try:
            # Seed the result with the tables that failed to mount
            result = PreviewResult({
                e.table_name: e
                for e in self.mount(tmp_schema, tables=tables) or []
            })

            # Commit so that errors don't cancel the mount
            self.engine.commit()
            for t in self.engine.get_all_tables(tmp_schema):
                if t in result:
                    continue
                try:
                    result[t] = self._preview_table(tmp_schema, t)
                except psycopg2.DatabaseError as e:
                    logging.warning("Could not preview data for table %s",
                                    t,
                                    exc_info=e)
                    result[t] = MountError(table_name=t,
                                           error=get_exception_name(e),
                                           error_text=str(e).strip())
            return result
        finally:
            self.engine.rollback()
            self.engine.delete_schema(tmp_schema)
            self.engine.commit()
Exemple #3
0
    def invoke(self, ctx):
        from splitgraph.engine import get_engine
        import psycopg2.extensions
        import psycopg2.extras

        # Allow users to send SIGINT to quickly terminate sgr (instead of waiting for a PG
        # statement to finish)
        psycopg2.extensions.set_wait_callback(psycopg2.extras.wait_select)

        engine = get_engine()
        try:
            result = super(click.Group, self).invoke(ctx)
            engine.commit()
            return result
        except Exception as exc:
            engine.rollback()
            if isinstance(
                    exc,
                (click.exceptions.ClickException, click.exceptions.Abort,
                 click.exceptions.Exit),
            ):
                raise
            # Can't seem to be able to get the click_log verbosity option
            # value so have to get it indirectly. Basically, if we're in
            # DEBUG mode, output the whole stacktrace.
            if logger.getEffectiveLevel() == logging.DEBUG:
                logger.error(traceback.format_exc())
            else:
                logger.error("%s: %s" % (get_exception_name(exc), exc))
            ctx.exit(code=2)
        finally:
            _do_version_check()
            engine.close()
Exemple #4
0
def report_errors(table_name: str):
    """Context manager that ignores exceptions and serializes them to JSON using PG's notice
    mechanism instead. The data source is meant to load these to report on partial failures
    (e.g. failed to load one table, but not others)."""
    try:
        yield
    except Exception as e:
        logging.error(
            "Error scanning %s, ignoring: %s: %s",
            table_name,
            get_exception_name(e),
            e,
            exc_info=e,
        )
        log_to_postgres("SPLITGRAPH: " +
                        json.dumps({
                            "table_name": table_name,
                            "error": get_exception_name(e),
                            "error_text": str(e),
                        }))