Compare commits

...

2 Commits

Author SHA1 Message Date
3wc
d171cf4c5d Add tidy command to README 2022-11-11 19:26:38 -08:00
3wc
5080e61abb Add categories tidy command 2022-11-11 17:18:23 -08:00
2 changed files with 30 additions and 5 deletions

View File

@ -6,9 +6,9 @@ from the command-line, including:
- find duplicate activities
- list, move and delete categories, activities and facts
Merging, renaming and otherwise tidying-up the database used to be possible, but
currently (as of version 2.0.0.16) doesn't work for me, so I wrote this script
to fill in the gap until the [shiny new version of
Merging, renaming and otherwise tidying-up the database used to be possible in
the Hamster GUI, but currently (as of version 2.0.0.16) doesn't work for me, so
I wrote this script to fill in the gap until the [shiny new version of
hamster](https://github.com/projecthamster/hamster-lib) is ready.
$ pip install .
@ -23,8 +23,9 @@ hamster](https://github.com/projecthamster/hamster-lib) is ready.
activities Show activities for categories specified by IDS
delete Delete categories specified by IDS
list List / search categories
tidy Delete empty categories
$ hamster-tools activities
$ hamster-tools activities --help
Usage: hamster-tools activities [OPTIONS] COMMAND [ARGS]...
Options:

View File

@ -106,7 +106,7 @@ def delete_categories(ids):
results = get_categories(ids)
for r in results:
sql = 'SELECT COUNT(id) FROM activities WHERE category_id = ?'
sql = 'select count(id) from activities where category_id = ?'
count = c.execute(sql, (r[0],)).fetchone()[0]
click.echo('@{0[0]}: {0[1]} ({1} activities)'.format(r, count))
@ -169,6 +169,30 @@ def list_category_activities(ids):
click.echo('@{0[0]}: {0[2]} » {0[1]}'.format(r))
@categories.command('tidy')
def tidy_categories():
""" Remove categories with no activities """
sql = 'SELECT categories.id, categories.name FROM categories LEFT JOIN activities ON categories.id = activities.category_id WHERE activities.id IS NULL'
categories = c.execute(sql).fetchall()
click.echo('Found {0} empty categories:'.format(len(categories)))
for cat in categories:
click.echo('@{0[0]}: {0[1]}'.format(cat))
click.confirm('Do you want to continue?', abort=True)
sql = 'DELETE FROM categories '
sql = sql + 'WHERE id IN ({seq})'.format(
seq=','.join(['?'] * len(categories))
)
c.execute(sql, [cat[0] for cat in categories])
conn.commit()
@cli.group()
def activities():
pass