Commit 2307252e authored by Aurélien Campéas's avatar Aurélien Campéas
Browse files

cli: add a command to mass-delete series


We also add a print to the renaming operation.
parent 06696a6f6d1c
......@@ -30,3 +30,19 @@ def test_rename(engine, cli, datadir):
for name in ('aquux', 'anew-foo', 'anew-bar'):
assert tsh.get(engine, name) is not None
def test_delete(engine, cli, datadir):
serie = genserie(datetime(2020, 1, 1), 'D', 3)
tsh = TimeSerie()
tsh.insert(engine, serie, 'bfoo', 'Babar')
tsh.insert(engine, serie, 'bbar', 'Babar')
tsh.insert(engine, serie, 'bquux', 'Babar')
r = cli('delete', engine.url, datadir / 'delete.csv')
tsh = TimeSerie()
assert tsh.get(engine, 'bfoo') is None
assert tsh.get(engine, 'bbar') is None
assert tsh.get(engine, 'bquux') is not None
......@@ -14,7 +14,7 @@ from dateutil.parser import parse as temporal
import pandas as pd
from tshistory.tsio import TimeSerie
from tshistory.util import fromjson, rename_series
from tshistory.util import fromjson, rename_series, delete_series
from tshistory.db import dump as dump_db, restore as restore_db
import tshistory.schema
......@@ -182,6 +182,23 @@ def rename(db_uri, mapfile, namespace='tsh'):
rename_series(engine, seriesmap, namespace)
@tsh.command()
@click.argument('db-uri')
@click.argument('deletefile', type=click.Path(exists=True))
@click.option('--namespace', default='tsh')
def delete(db_uri, deletefile, namespace='tsh'):
"""delete series by providing a one-column file (csv format)
file header must be `name`
"""
series = [
p.name
for p in pd.read_csv(deletefile).itertuples()
]
engine = create_engine(db_uri)
delete_series(engine, series, namespace)
# db maintenance
@tsh.command()
......
......@@ -135,6 +135,7 @@ def rename_series(engine, serie_map, namespace='tsh'):
reg = schema.registry
with engine.begin() as cn:
for old, new in serie_map.items():
print('rename', old, '->', new)
sql = reg.update().where(
reg.c.seriename == old
).values(
......@@ -142,3 +143,15 @@ def rename_series(engine, serie_map, namespace='tsh'):
)
cn.execute(sql)
def delete_series(engine, series, namespace='tsh'):
from tshistory.tsio import TimeSerie
tsh = TimeSerie(namespace=namespace)
for name in series:
with engine.begin() as cn:
if not tsh.exists(cn, name):
print('skipping unknown', name)
continue
print('delete', name)
tsh.delete(cn, name)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment