Skip to content

Commit

Permalink
Add support for restoring different types of data from a snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
tomach committed Aug 30, 2023
1 parent 05012c2 commit 597c651
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Changes for croud
Unreleased
==========

- Added support for restoring different types of data from a snapshot.

1.6.0 - 2023/08/24
==================

Expand Down
16 changes: 15 additions & 1 deletion croud/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,10 +680,24 @@
"If not specified the ``--cluster-id`` CrateDB"
" cluster will be used as the source.",
),
Argument(
"--type", type=str, required=False,
choices=["all", "metadata", "tables", "sections"],
help="The type of data to be restored from the "
"snapshot.",
),
Argument(
"--tables", type=str, required=False,
help="The list of tables to restore, comma separated. "
"If not specified all tables will be restored.",
"Only valid together with ``--type tables``.",
),
Argument(
"--sections", type=str, required=False,
help="The list of data sections to restore, comma "
"separated. Only valid together with ``--type "
"sections``. Valid sections are ``tables``, "
"``views``, ``users``, ``privileges``, "
"``analyzers`` or ``udfs``.",
),
],
"resolver": clusters_snapshots_restore,
Expand Down
8 changes: 8 additions & 0 deletions croud/clusters/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,11 +619,19 @@ def clusters_snapshots_restore(args: Namespace) -> None:
"repository": args.repository,
"snapshot": args.snapshot,
}
if args.type:
body["type"] = args.type
else:
# fallback for backwards compatibility
body["type"] = "tables"
if args.source_cluster_id:
body["source_cluster_id"] = args.source_cluster_id
if args.tables:
tables = args.tables.strip().split(",")
body["tables"] = [x.strip() for x in tables]
if args.sections:
sections = args.sections.strip().split(",")
body["sections"] = [s.strip() for s in sections]

client = Client.from_args(args)
data, errors = client.post(url, body=body)
Expand Down
2 changes: 1 addition & 1 deletion docs/commands/clusters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ Example
.. code-block:: console
sh$ ❯ croud clusters snapshots restore --cluster-id 705a7012-3f89-441d-a10e-b3749d05e993 \
--repository system_backup_20221002123456 --snapshot 20221210123456
--repository system_backup_20221002123456 --snapshot 20221210123456 --type all
==> Info: Restoring the snapshot. Depending on the amount of data you have, this might take a very long time.
==> Success: Operation completed.
+------------------------+-------------------------------+-------------------+
Expand Down
6 changes: 6 additions & 0 deletions tests/commands/test_clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,7 @@ def mock_call(*args, **kwargs):
body = {
"snapshot": "the-snapshot-name",
"repository": "a-repository",
"type": "all",
}

call_command(
Expand All @@ -1396,6 +1397,8 @@ def mock_call(*args, **kwargs):
body["repository"],
"--snapshot",
body["snapshot"],
"--type",
body["type"],
)
assert_rest(
mock_request,
Expand Down Expand Up @@ -1439,6 +1442,7 @@ def mock_call(*args, **kwargs):
"snapshot": "the-snapshot-name",
"repository": "a-repository",
"source_cluster_id": "another-cluster-id",
"type": "tables",
"tables": ["table1", "table2"],
}

Expand All @@ -1455,6 +1459,8 @@ def mock_call(*args, **kwargs):
body["snapshot"],
"--source-cluster-id",
body["source_cluster_id"],
"--type",
"tables",
"--tables",
" table1 , table2 ", # Ensure the parameter is properly trimmed
)
Expand Down

0 comments on commit 597c651

Please sign in to comment.