Commit 4efbb60c authored by Aurélien Campéas's avatar Aurélien Campéas
Browse files

cli: provide a command to allow mass renaming

parent 4f65b689f1cc
......@@ -28,6 +28,11 @@ def engine(request):
return create_engine(DBURI)
@pytest.fixture(scope='session')
def datadir():
return DATADIR
@pytest.fixture(params=['tsh', 'zzz'],
scope='session')
def tsh(request, engine):
......
old,new
afoo,anew-foo
abar,anew-bar
from datetime import datetime
from tshistory.tsio import TimeSerie
from tshistory.testutil import genserie
def test_info(engine, cli):
r = cli('info', engine.url)
......@@ -7,3 +12,21 @@ def test_info(engine, cli):
assert out[0].startswith('changeset count:')
assert out[1].startswith('series count:')
assert out[2].startswith('series names:')
def test_rename(engine, cli, datadir):
serie = genserie(datetime(2020, 1, 1), 'D', 3)
tsh = TimeSerie()
tsh.insert(engine, serie, 'afoo', 'Babar')
tsh.insert(engine, serie, 'abar', 'Babar')
tsh.insert(engine, serie, 'aquux', 'Babar')
r = cli('rename', engine.url, datadir / 'rename.csv')
tsh = TimeSerie()
assert tsh.get(engine, 'afoo') is None
assert tsh.get(engine, 'abar') is None
for name in ('aquux', 'anew-foo', 'anew-bar'):
assert tsh.get(engine, name) is not None
......@@ -15,7 +15,7 @@ from dateutil.parser import parse as temporal
import pandas as pd
from tshistory.tsio import TimeSerie
from tshistory.util import fromjson
from tshistory.util import fromjson, rename_series
from tshistory.db import dump as dump_db, restore as restore_db
import tshistory.schema
......@@ -165,6 +165,27 @@ def info(db_uri):
print(INFOFMT.format(**info))
# series maintenance
@tsh.command()
@click.argument('db-uri')
@click.argument('mapfile', type=click.Path(exists=True))
@click.option('--namespace', default='tsh')
def rename(db_uri, mapfile, namespace='tsh'):
"""rename series by providing a map file (csv format)
map file header must be `old,new`
"""
seriesmap = {
p.old: p.new
for p in pd.read_csv(mapfile).itertuples()
}
engine = create_engine(db_uri)
rename_series(engine, seriesmap, namespace)
# db maintenance
@tsh.command()
@click.argument('db-uri')
@click.argument('dump-path')
......@@ -235,6 +256,8 @@ def check(db_uri, namespace='tsh'):
print(idx, s, 'inserts={}, read-time={}'.format(len(hist), time() - t0))
# migration
@tsh.command(name='migrate-0.3-to-0.4')
@click.argument('db-uri')
@click.option('--namespace', default='tsh')
......
......@@ -135,7 +135,6 @@ def rename_series(engine, serie_map, namespace='tsh'):
reg = schema.registry
with engine.connect() as cn:
for old, new in serie_map.items():
print('{} -> {}'.format(old, new))
sql = reg.update().where(
reg.c.seriename == old
).values(
......
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