Commit 8c36a987 authored by Aurélien Campéas's avatar Aurélien Campéas
Browse files

sql: correctly handle fancy named namespaces

Since supervision uses one, we'd better do this right.
Was lots in the transition from sqlalchemy to plain sql.
parent 4b58e592d6e0
......@@ -33,7 +33,7 @@ def datadir():
return DATADIR
@pytest.fixture(params=['tsh', 'zzz'],
@pytest.fixture(params=['tsh', 'z-z'],
scope='session')
def tsh(request, engine):
namespace = request.param
......@@ -41,7 +41,7 @@ def tsh(request, engine):
sch.destroy(engine)
schema.init_schemas(engine, namespace)
if namespace == 'zzz':
if namespace == 'z-z':
Snapshot._max_bucket_size = 5
yield tsio.timeseries(namespace)
......
......@@ -214,7 +214,7 @@ def test_chunks(engine, tsh):
def test_append(engine, tsh):
if tsh.namespace == 'zzz':
if tsh.namespace == 'z-z':
return
for x, dt in enumerate(pd.date_range(start=utcdt(2018, 1, 1),
......@@ -252,7 +252,7 @@ insertion_date value_date
def test_prepend(engine, tsh):
if tsh.namespace == 'zzz':
if tsh.namespace == 'z-z':
return
serie = genserie(datetime(2010, 1, 1), 'D', 40)
......@@ -282,8 +282,8 @@ def test_get_from_to(engine, tsh):
tsh.insert(engine, ts, 'quitelong', 'aurelien.campeas@pythonian.fr')
snap = Snapshot(engine, tsh, 'quitelong')
if tsh.namespace == 'zzz':
sql = 'select id, parent from "zzz.snapshot".quitelong order by id'
if tsh.namespace == 'z-z':
sql = 'select id, parent from "z-z.snapshot".quitelong order by id'
chunks = engine.execute(sql).fetchall()
# should be perfectly chained
chunks = {
......
......@@ -301,7 +301,7 @@ def test_differential(engine, tsh):
with engine.begin() as cn:
cn.execute(
f'set search_path to "{tsh.namespace}.timeserie", {tsh.namespace}, public'
f'set search_path to "{tsh.namespace}.timeserie", "{tsh.namespace}", public'
)
allts = pd.read_sql("select seriename, table_name from registry "
"where seriename in ('ts_test', 'ts_mixte')",
......@@ -1114,7 +1114,7 @@ def test_serie_deletion(engine, tsh):
'value_dtype': '<f8'
}
sql = f"""select count(*) from {tsh.namespace}.changeset
sql = f"""select count(*) from "{tsh.namespace}".changeset
where (metadata::jsonb ->> 'tshistory.info')
like 'belonged to deleted series `deleteme`' """
count = engine.execute(sql).scalar()
......@@ -1201,7 +1201,7 @@ insertion_date value_date
alllogs = tsh.log(engine, names=['xserie', 'yserie'])
assert len(alllogs) == 6
sql = f"""select count(*) from {tsh.namespace}.changeset
sql = f"""select count(*) from "{tsh.namespace}".changeset
where (metadata::jsonb ->> 'tshistory.info')
like 'got stripped%%' """
count = engine.execute(sql).scalar()
......
......@@ -164,13 +164,13 @@ class Snapshot(SeriesServices):
select chunks.id as cid,
chunks.parent as parent,
chunks.chunk as chunk
from {namespace}."{table}" as chunks
from "{namespace}"."{table}" as chunks
where chunks.id in ({heads})
union
select chunks.id as cid,
chunks.parent as parent,
chunks.chunk as chunk
from {namespace}."{table}" as chunks
from "{namespace}"."{table}" as chunks
join allchunks on chunks.id = allchunks.parent
{where}
)
......@@ -183,7 +183,7 @@ class Snapshot(SeriesServices):
where = 'where chunks.cend >= %(start)s '
sql = self.rawsql.format(
namespace=f'"{self.tsh.namespace}.snapshot"',
namespace=f'{self.tsh.namespace}.snapshot',
table=self.name,
heads=','.join([str(head)]),
where=where
......@@ -213,7 +213,7 @@ class Snapshot(SeriesServices):
sql = [
'select ts.cset, ts.snapshot '
f'from "{self.tsh.namespace}.timeserie"."{tablename}" as ts, '
f' {self.tsh.namespace}.changeset as cset'
f' "{self.tsh.namespace}".changeset as cset'
' where cset.id = ts.cset '
]
......@@ -248,7 +248,7 @@ class Snapshot(SeriesServices):
where = 'where chunks.cend >= %(start)s '
sql = self.rawsql.format(
namespace=f'"{self.tsh.namespace}.snapshot"',
namespace=f'{self.tsh.namespace}.snapshot',
table=self.name,
heads=','.join(str(head) for head in heads),
where=where
......
......@@ -130,7 +130,7 @@ class timeseries(SeriesServices):
"""Return metadata dict of timeserie."""
if seriename in self.metadatacache:
return self.metadatacache[seriename]
sql = (f'select metadata from {self.namespace}.registry '
sql = (f'select metadata from "{self.namespace}".registry '
'where seriename = %(seriename)s')
meta = cn.execute(sql, seriename=seriename).scalar()
if meta is not None:
......@@ -638,7 +638,7 @@ class timeseries(SeriesServices):
def _register_serie(self, cn, seriename, ts):
index = ts.index
inames = [name for name in index.names if name]
sql = (f'insert into {self.namespace}.registry '
sql = (f'insert into "{self.namespace}".registry '
'(seriename, table_name, metadata) '
'values (%s, %s, %s) '
'returning id')
......@@ -670,7 +670,7 @@ class timeseries(SeriesServices):
if insertion_date is not None:
assert insertion_date.tzinfo is not None
idate = pd.Timestamp(insertion_date or datetime.utcnow(), tz='UTC')
sql = (f'insert into {self.namespace}.changeset '
sql = (f'insert into "{self.namespace}".changeset '
'(author, metadata, insertion_date) '
'values (%s, %s, %s) '
'returning id')
......
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