Beispiel #1
0
def test_is_jupyter_kernel_output():
    class JupyterKernelFakeStream:
        pass

    # implementation detail, aka cheapskate test
    JupyterKernelFakeStream.__module__ = "ipykernel.faked"
    assert not should_strip_ansi(stream=JupyterKernelFakeStream())
Beispiel #2
0
def main(dbname, verbose, apgdiff):
    """
    Compares the structure of the database against what's created from the
    models during `indico db prepare`.

    By default the current database is assumed to be named `indico`, but it
    can be overridden.  The database uses for comparison will be named
    `indico_dbdiff` and may not exist. It will be created and dropped
    automatically.

    Since your user may or may not be a database superuser, the database is
    created from a template database named `indico_template`.  You can create
    it using the following SQL commands:

    \b
        createdb indico_template
        psql indico_template -c 'CREATE EXTENSION unaccent;'
        psql indico_template -c 'CREATE EXTENSION pg_trgm;'

    Since this script uses the command-line PostgreSQL tools any other
    configuration should be done using the various environment variables like
    PGHOST, PGPORT and PGUSER) and your `.pgpass` file.

    apgdiff needs to be installed. If `apgdiff` is in your PATH it will be
    used; otherwise you need to use `--apgdiff` or the `APGDIFF` env var to
    specify the path to `apgdiff` apgdiff or its .jar file.
    """
    temp_dbname = "indico_dbdiff"
    apgdiff_cmd = _get_apgdiff_cmd(apgdiff)
    if not apgdiff_cmd:
        raise click.exceptions.UsageError(
            "Could not find apgdiff in PATH; specify the path to a script or the .jar " "file manually."
        )
    # create database and dump current/new structures
    _checked_call(verbose, ["createdb", "-T", "indico_template", temp_dbname])
    try:
        env_override = {"INDICO_CONF_OVERRIDE": json.dumps({"SQLAlchemyDatabaseURI": _build_conn_string(temp_dbname)})}
        _checked_call(verbose, ["indico", "db", "prepare"], env=env_override)
        dump_current = tempfile.NamedTemporaryFile(suffix=".sql", prefix="dbdiff-current-")
        dump_fresh = tempfile.NamedTemporaryFile(suffix=".sql", prefix="dbdiff-fresh-")
        _checked_call(verbose, ["pg_dump", "-s", "-f", dump_current.name, dbname])
        _checked_call(verbose, ["pg_dump", "-s", "-f", dump_fresh.name, temp_dbname])
    finally:
        _checked_call(verbose, ["dropdb", temp_dbname])
    # compare them
    diff = _checked_call(verbose, apgdiff_cmd + [dump_current.name, dump_fresh.name], return_output=True).strip()
    if not diff:
        click.echo(click.style("No changes found :)", fg="green", bold=True), err=True)
        return
    elif should_strip_ansi(sys.stdout):
        click.echo(diff)
    else:
        pretty_diff = _checked_call(
            verbose,
            ["pygmentize", "-l", "sql", "-f", "terminal256", "-O", "style=native,bg=dark"],
            return_output=True,
            stdin_data=diff,
        )
        click.echo(pretty_diff)
Beispiel #3
0
def main(dbname, verbose, apgdiff):
    """
    Compares the structure of the database against what's created from the
    models during `indico db prepare`.

    By default the current database is assumed to be named `indico`, but it
    can be overridden.  The database uses for comparison will be named
    `indico_dbdiff` and may not exist. It will be created and dropped
    automatically.

    Since your user may or may not be a database superuser, the database is
    created from a template database named `indico_template`.  You can create
    it using the following SQL commands:

    \b
        createdb indico_template
        psql indico_template -c 'CREATE EXTENSION unaccent;'
        psql indico_template -c 'CREATE EXTENSION pg_trgm;'

    Since this script uses the command-line PostgreSQL tools any other
    configuration should be done using the various environment variables like
    PGHOST, PGPORT and PGUSER) and your `.pgpass` file.

    apgdiff needs to be installed. If `apgdiff` is in your PATH it will be
    used; otherwise you need to use `--apgdiff` or the `APGDIFF` env var to
    specify the path to `apgdiff` apgdiff or its .jar file.
    """
    temp_dbname = 'indico_dbdiff'
    apgdiff_cmd = _get_apgdiff_cmd(apgdiff)
    if not apgdiff_cmd:
        raise click.exceptions.UsageError('Could not find apgdiff in PATH; specify the path to a script or the .jar '
                                          'file manually.')
    # create database and dump current/new structures
    _checked_call(verbose, ['createdb', '-T', 'indico_template', temp_dbname])
    try:
        env_override = {'INDICO_CONF_OVERRIDE': json.dumps({'SQLAlchemyDatabaseURI': _build_conn_string(temp_dbname)})}
        _checked_call(verbose, ['indico', 'db', 'prepare'], env=env_override)
        dump_current = tempfile.NamedTemporaryFile(suffix='.sql', prefix='dbdiff-current-')
        dump_fresh = tempfile.NamedTemporaryFile(suffix='.sql', prefix='dbdiff-fresh-')
        _checked_call(verbose, ['pg_dump', '-s', '-f', dump_current.name, dbname])
        _checked_call(verbose, ['pg_dump', '-s', '-f', dump_fresh.name, temp_dbname])
    finally:
        _checked_call(verbose, ['dropdb', temp_dbname])
    # compare them
    diff = _checked_call(verbose, apgdiff_cmd + [dump_current.name, dump_fresh.name],
                         return_output=True).strip()
    if not diff:
        click.echo(click.style('No changes found :)', fg='green', bold=True), err=True)
        return
    elif should_strip_ansi(sys.stdout):
        click.echo(diff)
    else:
        pretty_diff = _checked_call(verbose, ['pygmentize', '-l', 'sql', '-f', 'terminal256',
                                              '-O', 'style=native,bg=dark'], return_output=True, stdin_data=diff)
        click.echo(pretty_diff)
Beispiel #4
0
def main(dbname, verbose, apgdiff):
    """
    Compares the structure of the database against what's created from the
    models during `indico db prepare`.

    By default the current database is assumed to be named `indico`, but it
    can be overridden.  The database uses for comparison will be named
    `indico_dbdiff` and may not exist. It will be created and dropped
    automatically.

    Since your user may or may not be a database superuser, the database is
    created from a template database named `indico_template`.  You can create
    it using the following SQL commands:

    \b
        createdb indico_template
        psql indico_template -c 'CREATE EXTENSION unaccent;'
        psql indico_template -c 'CREATE EXTENSION pg_trgm;'

    Since this script uses the command-line PostgreSQL tools any other
    configuration should be done using the various environment variables like
    PGHOST, PGPORT and PGUSER) and your `.pgpass` file.

    apgdiff needs to be installed. If `apgdiff` is in your PATH it will be
    used; otherwise you need to use `--apgdiff` or the `APGDIFF` env var to
    specify the path to `apgdiff` apgdiff or its .jar file.
    """
    temp_dbname = 'indico_dbdiff'
    apgdiff_cmd = _get_apgdiff_cmd(apgdiff)
    if not apgdiff_cmd:
        raise click.exceptions.UsageError('Could not find apgdiff in PATH; specify the path to a script or the .jar '
                                          'file manually.')
    # create database and dump current/new structures
    _checked_call(verbose, ['createdb', '-T', 'indico_template', temp_dbname])
    try:
        env_override = {'INDICO_CONF_OVERRIDE': repr({'SQLALCHEMY_DATABASE_URI': _build_conn_string(temp_dbname)})}
        _checked_call(verbose, ['indico', 'db', 'prepare'], env=env_override)
        dump_current = tempfile.NamedTemporaryFile(suffix='.sql', prefix='dbdiff-current-')
        dump_fresh = tempfile.NamedTemporaryFile(suffix='.sql', prefix='dbdiff-fresh-')
        _checked_call(verbose, ['pg_dump', '-s', '-f', dump_current.name, dbname])
        _checked_call(verbose, ['pg_dump', '-s', '-f', dump_fresh.name, temp_dbname])
    finally:
        _checked_call(verbose, ['dropdb', temp_dbname])
    # compare them
    diff = _checked_call(verbose, apgdiff_cmd + [dump_current.name, dump_fresh.name],
                         return_output=True).strip()
    if not diff:
        click.echo(click.style('No changes found :)', fg='green', bold=True), err=True)
        return
    elif should_strip_ansi(sys.stdout):
        click.echo(diff)
    else:
        pretty_diff = _checked_call(verbose, ['pygmentize', '-l', 'sql', '-f', 'terminal256',
                                              '-O', 'style=native,bg=dark'], return_output=True, stdin_data=diff)
        click.echo(pretty_diff)
Beispiel #5
0
def main(dbname, verbose, reverse):
    """
    Compare the structure of the database against what's created from the
    models during `indico db prepare`.

    By default the current database is assumed to be named `indico`, but it
    can be overridden.  The database uses for comparison will be named
    `indico_dbdiff` and may not exist. It will be created and dropped
    automatically.

    Since your user may or may not be a database superuser, the database is
    created from a template database named `indico_template`.  You can create
    it using the following SQL commands:

    \b
        createdb indico_template
        psql indico_template -c 'CREATE EXTENSION unaccent;'
        psql indico_template -c 'CREATE EXTENSION pg_trgm;'

    Since this script uses the command-line PostgreSQL tools any other
    configuration should be done using the various environment variables like
    PGHOST, PGPORT and PGUSER) and your `.pgpass` file.
    """
    temp_dbname = 'indico_dbdiff'
    base_conn = None
    target_conn = None

    # create database and dump current/new structures
    _checked_call(verbose, ['createdb', '-T', 'indico_template', temp_dbname])
    try:
        env_override = {
            'INDICO_CONF_OVERRIDE':
            repr({'SQLALCHEMY_DATABASE_URI': _build_conn_string(temp_dbname)})
        }
        _checked_call(verbose, ['indico', 'db', 'prepare'], env=env_override)

        # create SQLAlchemy engines/connections for base and target db
        base_eng = create_engine(_build_conn_string(temp_dbname))
        target_eng = create_engine(_build_conn_string(dbname))
        base_conn = base_eng.connect()
        target_conn = target_eng.connect()

        version = base_conn.execute(
            "SELECT current_setting('server_version_num')::int").scalar()
        if version < 100000:
            click.echo(click.style(
                '!! This utility requires at least Postgres 10',
                fg='red',
                bold=True),
                       err=True)
            sys.exit(1)

        if verbose:
            click.echo(click.style('** Calculating differences', fg='cyan'),
                       err=True)
        # use migra to figure out the SQL diff
        m = Migration(base_conn, target_conn) if reverse else Migration(
            target_conn, base_conn)
        m.set_safety(False)
        m.add_all_changes()
        diff = m.sql
    finally:
        # clean up connections and engines, so that no open connection remains
        # (otherwise the DROPDB operation won't work)
        if base_conn:
            base_conn.close()
            base_eng.dispose()
        if target_conn:
            target_conn.close()
            target_eng.dispose()
        _checked_call(verbose, ['dropdb', temp_dbname])

    if not diff:
        click.echo(click.style('No changes found :)', fg='green', bold=True),
                   err=True)
        return
    elif should_strip_ansi(sys.stdout):
        click.echo(diff)
    else:
        pretty_diff = _checked_call(verbose, [
            'pygmentize', '-l', 'sql', '-f', 'terminal256', '-O',
            'style=native,bg=dark'
        ],
                                    return_output=True,
                                    input=diff)
        click.echo(pretty_diff)