diff --git a/src/hii_collector.py b/src/hii_collector.py index 60dd93d..a097cda 100644 --- a/src/hii_collector.py +++ b/src/hii_collector.py @@ -204,13 +204,6 @@ class HiiStore: self.engine = None return False - def _pk(self) -> str: - if self.db_type == "sqlite": - return "INTEGER PRIMARY KEY AUTOINCREMENT" - if self.db_type == "postgresql": - return "BIGSERIAL PRIMARY KEY" - return "BIGINT AUTO_INCREMENT PRIMARY KEY" - def _create_tables(self): from sqlalchemy import text @@ -229,15 +222,16 @@ class HiiStore: updated_at TIMESTAMP ) """, - f""" + # Composite natural PK (no surrogate id): TimescaleDB hypertable + # conversion requires every unique index to include the time column. + """ CREATE TABLE IF NOT EXISTS hii_rainfall ( - id {self._pk()}, station_id INTEGER NOT NULL, timestamp TIMESTAMP NOT NULL, rain_1h NUMERIC(7,2), rain_24h NUMERIC(8,2), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - CONSTRAINT uq_hii_rainfall UNIQUE (station_id, timestamp) + PRIMARY KEY (station_id, timestamp) ) """, f""" @@ -261,9 +255,8 @@ class HiiStore: updated_at TIMESTAMP ) """, - f""" + """ CREATE TABLE IF NOT EXISTS hii_waterlevel ( - id {self._pk()}, station_id INTEGER NOT NULL, timestamp TIMESTAMP NOT NULL, wl_msl NUMERIC(8,3), @@ -274,7 +267,7 @@ class HiiStore: situation_level INTEGER, diff_wl_bank NUMERIC(8,3), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - CONSTRAINT uq_hii_waterlevel UNIQUE (station_id, timestamp) + PRIMARY KEY (station_id, timestamp) ) """, "CREATE INDEX IF NOT EXISTS idx_hii_rainfall_ts ON hii_rainfall(timestamp)",