-
Notifications
You must be signed in to change notification settings - Fork 0
Support streaming PostgreSQL dump and restore #52
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
Open
Copilot
wants to merge
3
commits into
main
Choose a base branch
from
copilot/add-cli-dump-and-restore
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| """A basic command-line interface for PostgreSQL database transfer. | ||
|
|
||
| For example, stream a schema between databases: | ||
|
|
||
| database-transfer --database "$SOURCE_DATABASE" dump -n temp - \ | ||
| | database-transfer --database "$TARGET_DATABASE" restore - | ||
| """ | ||
|
|
||
| import asyncio | ||
| from pathlib import Path | ||
|
|
||
| import click | ||
| from sqlalchemy import create_engine | ||
|
|
||
| from .dump_database import pg_dump_to_file | ||
| from .restore_database import pg_restore_from_file | ||
|
|
||
|
|
||
| @click.group() | ||
| @click.option( | ||
| "--database", | ||
| envvar="DATABASE_URL", | ||
| required=True, | ||
| help="PostgreSQL connection URL.", | ||
| ) | ||
| @click.pass_context | ||
| def cli(ctx, database): | ||
| """Dump and restore PostgreSQL databases.""" | ||
| ctx.ensure_object(dict) | ||
| ctx.obj["database"] = database | ||
|
|
||
|
|
||
| def _engine(ctx): | ||
| return create_engine(ctx.obj["database"]) | ||
|
|
||
|
|
||
| @cli.command() | ||
| @click.option("-n", "--schema", multiple=True, help="Schema to dump.") | ||
| @click.argument("destination") | ||
| @click.pass_context | ||
| def dump(ctx, schema, destination): | ||
| """Dump to DESTINATION, or '-' for standard output.""" | ||
| args = [arg for name in schema for arg in ("--schema", name)] | ||
| engine = _engine(ctx) | ||
| try: | ||
| asyncio.run( | ||
| pg_dump_to_file( | ||
| engine, None if destination == "-" else Path(destination), args=args | ||
| ) | ||
| ) | ||
| finally: | ||
| engine.dispose() | ||
|
|
||
|
|
||
| @cli.command() | ||
| @click.argument("source") | ||
| @click.pass_context | ||
| def restore(ctx, source): | ||
| """Restore from SOURCE, or '-' for standard input.""" | ||
| engine = _engine(ctx) | ||
| try: | ||
| asyncio.run( | ||
| pg_restore_from_file(None if source == "-" else Path(source), engine) | ||
| ) | ||
| finally: | ||
| engine.dispose() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| cli() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import asyncio | ||
| from types import SimpleNamespace | ||
|
|
||
| from click.testing import CliRunner | ||
|
|
||
| from macrostrat.database.transfer import dump_database, restore_database, stream_utils | ||
| from macrostrat.database.transfer.cli import cli | ||
|
|
||
|
|
||
| def test_dump_to_stdout_inherits_standard_output(monkeypatch): | ||
| calls = {} | ||
|
|
||
| async def fake_dump(engine, **kwargs): | ||
| calls.update(kwargs) | ||
| return SimpleNamespace(stderr=object(), wait=fake_wait) | ||
|
|
||
| async def fake_wait(): | ||
| calls["waited"] = True | ||
|
|
||
| async def fake_print_stderr(stream): | ||
| calls["stderr"] = stream | ||
|
|
||
| monkeypatch.setattr(dump_database, "pg_dump", fake_dump) | ||
| monkeypatch.setattr(dump_database, "print_stderr", fake_print_stderr) | ||
|
|
||
| asyncio.run(dump_database.pg_dump_to_file(object(), None)) | ||
|
|
||
| assert calls["stdout"] is None | ||
| assert calls["stderr"] is not None | ||
| assert calls["waited"] is True | ||
|
|
||
|
|
||
| def test_restore_from_stdin_inherits_standard_input(monkeypatch): | ||
| calls = {} | ||
|
|
||
| async def fake_restore(engine, **kwargs): | ||
| calls.update(kwargs) | ||
| return SimpleNamespace(stderr=object(), wait=fake_wait) | ||
|
|
||
| async def fake_wait(): | ||
| calls["waited"] = True | ||
|
|
||
| async def fake_print_stderr(stream): | ||
| calls["stderr"] = stream | ||
|
|
||
| monkeypatch.setattr(restore_database, "pg_restore", fake_restore) | ||
| monkeypatch.setattr(restore_database, "print_stderr", fake_print_stderr) | ||
|
|
||
| asyncio.run(restore_database.pg_restore_from_file(None, object())) | ||
|
|
||
| assert calls["stdin"] is None | ||
| assert calls["stderr"] is not None | ||
| assert calls["waited"] is True | ||
|
|
||
|
|
||
| def test_cli_streams_schema_dump_to_standard_output(monkeypatch): | ||
| calls = {} | ||
|
|
||
| async def fake_dump(engine, destination, **kwargs): | ||
| calls["url"] = str(engine.url) | ||
| calls["destination"] = destination | ||
| calls["args"] = kwargs["args"] | ||
|
|
||
| monkeypatch.setattr( | ||
| "macrostrat.database.transfer.cli.pg_dump_to_file", fake_dump | ||
| ) | ||
|
|
||
| result = CliRunner().invoke( | ||
| cli, ["--database", "postgresql://localhost/source", "dump", "-n", "temp", "-"] | ||
| ) | ||
|
|
||
| assert result.exit_code == 0, result.output | ||
| assert calls == { | ||
| "url": "postgresql://localhost/source", | ||
| "destination": None, | ||
| "args": ["--schema", "temp"], | ||
| } | ||
|
|
||
|
|
||
| def test_cli_restores_from_standard_input(monkeypatch): | ||
| calls = {} | ||
|
|
||
| async def fake_restore(source, engine): | ||
| calls["source"] = source | ||
| calls["url"] = str(engine.url) | ||
|
|
||
| monkeypatch.setattr( | ||
| "macrostrat.database.transfer.cli.pg_restore_from_file", fake_restore | ||
| ) | ||
|
|
||
| result = CliRunner().invoke( | ||
| cli, ["--database", "postgresql://localhost/target", "restore", "-"] | ||
| ) | ||
|
|
||
| assert result.exit_code == 0, result.output | ||
| assert calls == {"source": None, "url": "postgresql://localhost/target"} | ||
|
|
||
|
|
||
| def test_print_stderr_does_not_write_to_standard_output(capsysbinary): | ||
| async def write_stderr(): | ||
| stream = asyncio.StreamReader() | ||
| stream.feed_data(b"pg_dump: warning\n") | ||
| stream.feed_eof() | ||
| await stream_utils.print_stderr(stream) | ||
|
|
||
| asyncio.run(write_stderr()) | ||
|
|
||
| captured = capsysbinary.readouterr() | ||
| assert captured.out == b"" | ||
| assert captured.err == b"pg_dump: warning\n" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this CLI to the testing area, and convert it to Typer (fitting with Macrostrat's standard practice).