Commit 26ce5b06 authored by Aurélien Campéas's avatar Aurélien Campéas
Browse files

tsio: fix the table name handling to avoid a name squatting issue after a series rename

parent 9d10852b8ec8
......@@ -1379,8 +1379,19 @@ def test_rename(engine, tsh):
assert tsh.get(engine, 'bar') is None
for name in ('quux', 'new-foo', 'new-bar'):
assert tsh.exists(engine, name)
assert tsh.get(engine, name) is not None
# check we can safely re-use 'foo'
serie = genserie(datetime(2025, 1, 1), 'D', 3)
tsh.insert(engine, serie, 'foo', 'Babar')
ts = tsh.get(engine, 'foo')
assert_df("""
2025-01-01 0.0
2025-01-02 1.0
2025-01-03 2.0
""", ts)
def test_index_order(engine, tsh):
ts = genserie(datetime(2020, 1, 1), 'D', 3)
......
from datetime import datetime
import logging
import hashlib
import uuid
import pandas as pd
......@@ -541,6 +543,23 @@ class TimeSerie(SeriesServices):
# serie table handling
def _make_tablename(self, cn, seriename):
""" compute the unqualified (no namespace) table name
from a serie name, to allow arbitrary serie names
"""
# postgresql table names are limited to 63 chars.
if len(seriename) > 63:
seriename = hashlib.sha1(seriename.encode('utf-8')).hexdigest()
# collision detection (collision can happen after a rename)
if cn.execute(f'select table_name '
f'from "{self.namespace}".registry '
f'where table_name = %(seriename)s',
seriename=seriename).scalar():
return str(uuid.uuid4())
return seriename
def _serie_to_tablename(self, cn, seriename):
tablename = self.serie_tablename.get(seriename)
if tablename is not None:
......
import os
import math
import zlib
import hashlib
import logging
import threading
import tempfile
......@@ -167,18 +166,6 @@ class SeriesServices(object):
return pd.concat([diff_overlap, diff_new])
# serialization
def _make_tablename(self, seriename):
""" compute the unqualified (no namespace) table name
from a serie name, to allow arbitrary serie names
"""
# postgresql table names are limited to 63 chars.
if len(seriename) > 63:
return hashlib.sha1(seriename.encode('utf-8')).hexdigest()
return seriename
def rename_series(engine, serie_map, namespace='tsh'):
from tshistory.schema import tsschema
schema = tsschema(namespace)
......
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