Skip to content
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

add size to snapshot list #11

Merged
merged 3 commits into from
Sep 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion dslr/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,10 @@ def list():
table = Table(box=box.SIMPLE)
table.add_column("Name", style="cyan")
table.add_column("Created")
table.add_column("Size", justify="right")

for snapshot in sorted(snapshots, key=lambda s: s.created_at, reverse=True):
table.add_row(snapshot.name, timeago.format(snapshot.created_at))
table.add_row(snapshot.name, timeago.format(snapshot.created_at), snapshot.size)

cprint(table)

Expand Down
19 changes: 12 additions & 7 deletions dslr/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def drop_database(dbname: str):
# Snapshot operations
################################################################################

Snapshot = namedtuple("Snapshot", ["dbname", "name", "created_at"])
Snapshot = namedtuple("Snapshot", ["dbname", "name", "created_at", "size"])


def generate_snapshot_db_name(
Expand All @@ -79,22 +79,27 @@ def get_snapshots() -> List[Snapshot]:
dslr_<timestamp>_<snapshot_name>
"""
# Find the snapshot databases
result = exec_sql("SELECT datname FROM pg_database WHERE datname LIKE 'dslr_%'")
result = exec_sql(
"""
SELECT
datname,
pg_size_pretty(pg_database_size(datname))
FROM pg_database
WHERE datname LIKE 'dslr_%'
"""
)

if result is None:
raise RuntimeError("Did not get results from database.")

snapshot_dbnames = sorted([row[0] for row in result])

# Parse the name into a Snapshot
parts = [dbname.split("_") for dbname in snapshot_dbnames]
return [
Snapshot(
dbname=line,
name="_".join(part[2:]),
created_at=datetime.fromtimestamp(int(part[1])),
size=size,
)
for part, line in zip(parts, snapshot_dbnames)
for line, part, size in [(row[0], row[0].split("_"), row[1]) for row in result]
]


Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def stub_exec_sql(*args, **kwargs) -> List[Tuple[Any, ...]]:
"existing-snapshot-2",
created_at=datetime(2020, 1, 2, 0, 0, 0, 0),
)
return [(fake_snapshot_1,), (fake_snapshot_2,)]
return [(fake_snapshot_1, "100 kB"), (fake_snapshot_2, "100 kB")]


@mock.patch.dict(os.environ, {"DATABASE_URL": "postgres://user:pw@test:5432/my_db"})
Expand Down