async def apply_migration(cog, quiet, index, *, downgrade=False):
    try:
        pool = await Table.create_pool(config.postgresql)
    except Exception:
        click.echo(
            f'Could not create PostgreSQL connection pool.\n{traceback.format_exc()}',
            err=True)
        return

    if not cog.startswith('cogs.'):
        cog = f'cogs.{cog}'

    try:
        importlib.import_module(cog)
    except Exception:
        click.echo(f'Could not load {cog}.\n{traceback.format_exc()}',
                   err=True)
        return

    async with pool.acquire() as con:
        tr = con.transaction()
        await tr.start()
        for table in Table.all_tables():
            try:
                await table.migrate(index=index,
                                    downgrade=downgrade,
                                    verbose=not quiet,
                                    connection=con)
            except RuntimeError as e:
                click.echo(f'Could not migrate {table.__tablename__}: {e}',
                           err=True)
                await tr.rollback()
                break
        else:
            await tr.commit()
Example #2
0
def migrate(ctx, cog, quiet):
    """Update the migration file with the newest schema."""

    if not cog.startswith("cogs."):
        cog = f"cogs.{cog}"

    try:
        importlib.import_module(cog)
    except Exception:
        click.echo(f"Could not load {ext}.\n{traceback.format_exc()}", err=True)
        return

    def work(table, *, invoked=False):
        try:
            actually_migrated = table.write_migration()
        except RuntimeError as e:
            click.echo(f"Could not migrate {table.__tablename__}: {e}", err=True)
            if not invoked:
                click.confirm("do you want to create the table?", abort=True)
                ctx.invoke(init, cogs=[cog], quiet=quiet)
                work(table, invoked=True)
            sys.exit(-1)
        else:
            if actually_migrated:
                click.echo(
                    f"Successfully updated migrations for {table.__tablename__}."
                )
            else:
                click.echo(f"Found no changes for {table.__tablename__}.")

    for table in Table.all_tables():
        work(table)

    click.echo(f"Done migrating {cog}.")
Example #3
0
def init(cogs, quiet):
    """This manages the migrations and database creation system for you."""

    run = asyncio.get_event_loop().run_until_complete
    try:
        run(Table.create_pool(config.postgresql))
    except Exception:
        click.echo(f'Could not create PostgreSQL connection pool.\n{traceback.format_exc()}', err=True)
        return

    if not cogs:
        cogs = INITIAL_EXTENSIONS
    else:
        cogs = [f'cogs.{e}' if not e.startswith('cogs.') else e for e in cogs]

    for ext in cogs:
        try:
            importlib.import_module(ext)
        except Exception:
            click.echo(f'Could not load {ext}.\n{traceback.format_exc()}', err=True)
            return

    for table in Table.all_tables():
        try:
            created = run(table.create(verbose=not quiet, run_migrations=False))
        except Exception:
            click.echo(f'Could not create {table.__tablename__}.\n{traceback.format_exc()}', err=True)
        else:
            if created:
                click.echo(f'[{table.__module__}] Created {table.__tablename__}.')
            else:
                click.echo(f'[{table.__module__}] No work needed for {table.__tablename__}.')
Example #4
0
async def remove_databases(pool, cog, quiet):
    async with pool.acquire() as con:
        tr = con.transaction()
        await tr.start()
        for table in Table.all_tables():
            try:
                await table.drop(verbose=not quiet, connection=con)
            except RuntimeError as e:
                click.echo(f"Could not drop {table.__tablename__}: {e}", err=True)
                await tr.rollback()
                break
            else:
                click.echo(f"Dropped {table.__tablename__}.")
        else:
            await tr.commit()
            click.echo(f"successfully removed {cog} tables.")
Example #5
0
def init(cogs, quiet):
    run = asyncio.get_event_loop().run_until_complete

    # noinspection PyBroadException
    try:
        run(Table.create_pool(config.postgresql))
    except Exception:
        click.echo(
            f'Could not create PostgreSQL connection pool.\n'
            f'{traceback.format_exc()}',
            err=True
        )
        return

    if not cogs:
        cogs = initial_extensions
    else:
        cogs = [f'cogs.{e}' if not e.startswith('cogs.') else e for e in cogs]

    for ext in cogs:
        # noinspection PyBroadException
        try:
            importlib.import_module(ext)
        except Exception:
            click.echo(
                f'Could not load {ext}.\n{traceback.format_exc()}',
                err=True
            )
            return

    for table in Table.all_tables():
        # noinspection PyBroadException
        try:
            run(table.create(verbose=not quiet))
        except Exception:
            click.echo(
                f'Could not create {table.__tablename__}.\n'
                f'{traceback.format_exc()}',
                err=True
            )
        else:
            click.echo(
                f'[{table.__module__}] Created {table.__tablename__}.'
            )