Commit 34e2b840 authored by Aurélien Campéas's avatar Aurélien Campéas
Browse files

command: provide a command line tool to query a given tshistory database

parent 2298b007e513
import click
from sqlalchemy import create_engine
from tshistory.tsio import TimeSerie
FMT = """
revision: {rev}
author: {author}
date: {date}
def format_rev(rev):
fmt = FMT + '\n'
if rev.get('diff'):
fmt += 'series: {names}\n\n'
lines = []
for ts in rev['diff'].values():
fmt += '\n'.join(lines)
fmt += 'series: {names}'
return fmt.format(**rev)
@click.option('--limit', '-l', default=None)
@click.option('--show-diff', is_flag=True, default=False)
def log(db_uri, limit, show_diff):
engine = create_engine(db_uri)
tsh = TimeSerie()
for rev in tsh.log(engine, limit=limit, diff=show_diff):
rev['names'] = ','.join(rev['names'])
if __name__ == '__main__':
......@@ -187,15 +187,20 @@ class TimeSerie(object):
).where(tstable.c.csid ==
return cnx.execute(sql).scalar()
def log(self, cnx, diff=False):
def log(self, cnx, limit=0, diff=False):
"""Build a structure showing the history of all the series in the db,
per changeset, in chronological order.
log = []
cset, cset_series, reg = schema.changeset, schema.changeset_series, schema.registry
sql = select([,, cset.c.insertion_date,]
).where( == cset_series.c.csid
if limit:
sql = sql.limit(limit)
sql = sql.where( == cset_series.c.csid
).where(cset_series.c.serie ==
rset = cnx.execute(sql)
......@@ -211,6 +216,7 @@ class TimeSerie(object):
rev['diff'] = {name: self._diff(cnx, rev['rev'], name)
for name in rev['names']}
log.sort(key=lambda rev: rev['rev'])
return log
# /API
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