Skip to content

Mechanism to make memory DB created by sqlite-utils memory available to other commands #643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
simonw opened this issue Nov 8, 2024 · 3 comments
Labels
enhancement New feature or request plugins

Comments

@simonw
Copy link
Owner

simonw commented Nov 8, 2024

For an experimental feature for https://github.com/simonw/sqlite-utils-ask I found myself wanting to invoke the existing sqlite-utils memory command and then do something else with the memory database it created.

I can do this with a sqlite_utils.Database(memory_name="x") but I need to ensure the variable doesn't go out of scope or it will be dropped by the sqlite3 library.

@simonw simonw added enhancement New feature or request plugins labels Nov 8, 2024
@simonw simonw closed this as completed in 8906f57 Nov 8, 2024
@simonw
Copy link
Owner Author

simonw commented Nov 8, 2024

Documented a pattern using this here: https://github.com/simonw/sqlite-utils/blob/8906f57740aaf3e9ea77af3de0b061a64a2f5d3b/docs/plugins.rst#register_commandscli

from contextlib import redirect_stdout
import io

@cli.command()
@click.pass_context
@click.argument(
    "paths",
    type=click.Path(file_okay=True, dir_okay=False, allow_dash=True),
    required=False,
    nargs=-1,
)
def show_schema_for_files(ctx, paths):
    from sqlite_utils.cli import memory
    with redirect_stdout(io.StringIO()):
        ctx.invoke(memory, paths=paths, sql="select 1")
    db = sqlite_utils.Database(memory_name="sqlite_utils_memory")
    # Now do something with that database
    click.echo(db.schema)

@simonw
Copy link
Owner Author

simonw commented Nov 8, 2024

OK, I massively over-engineered this. There's a much neater pattern:

from sqlite_utils.cli import memory
db = ctx.invoke(memory, paths=paths, return_db=True)

@simonw simonw reopened this Nov 8, 2024
@simonw simonw changed the title Hack to make memory DB created by sqlite-utils memory available to other commands Mechanism to make memory DB created by sqlite-utils memory available to other commands Nov 8, 2024
@simonw simonw closed this as completed in 2258b43 Nov 8, 2024
@simonw
Copy link
Owner Author

simonw commented Nov 8, 2024

Much neater:

@cli.command()
@click.pass_context
@click.argument(
    "paths",
    type=click.Path(file_okay=True, dir_okay=False, allow_dash=True),
    required=False,
    nargs=-1,
)
def show_schema_for_files(ctx, paths):
    from sqlite_utils.cli import memory
    db = ctx.invoke(memory, paths=paths, return_db=True)
    # Now do something with that database
    click.echo(db.schema)

simonw added a commit that referenced this issue Nov 8, 2024
simonw added a commit to simonw/sqlite-utils-ask that referenced this issue Nov 8, 2024
simonw added a commit that referenced this issue Nov 23, 2024
simonw added a commit to simonw/sqlite-utils-ask that referenced this issue Nov 24, 2024
Closes #10. Uses simonw/sqlite-utils#643

Depend on sqlite-utils>=3.38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request plugins
Projects
None yet
Development

No branches or pull requests

1 participant