Skip to content

Commit 58b2323

Browse files
author
Daniel Thom
authored
Export check_timestamps method (#47)
* Export check_timestamps method This gives applications more explicit control on when timestamps are checked. * Update Spark download link
1 parent b180cd0 commit 58b2323

6 files changed

Lines changed: 39 additions & 6 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
run: |
3131
python -m pip install --upgrade pip
3232
python -m pip install ".[dev,spark]"
33-
wget https://dlcdn.apache.org/spark/spark-3.5.4/spark-3.5.4-bin-hadoop3.tgz
33+
wget https://archive.apache.org/dist/spark/spark-3.5.4/spark-3.5.4-bin-hadoop3.tgz
3434
tar -xzf spark-3.5.4-bin-hadoop3.tgz
3535
export SPARK_HOME=$(pwd)/spark-3.5.4-bin-hadoop3
3636
export PATH=$SPARK_HOME/sbin:$PATH

src/chronify/csv_time_series_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def _ingest_data(self, data: pd.DataFrame, table_name: str, year: int, length: i
124124
assert src_schema is not None
125125
self._store.ingest_pivoted_table(data, src_schema, dst_schema)
126126
case fmt if fmt in UNPIVOTED_TABLES:
127-
self._store.ingest_table(data, dst_schema, bypass_time_checks=True)
127+
self._store.ingest_table(data, dst_schema, skip_time_checks=True)
128128

129129
@staticmethod
130130
def _create_schemas(

src/chronify/schema_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __init__(self, engine: Engine, metadata: MetaData):
5252
table = Table(
5353
self.SCHEMAS_TABLE,
5454
self._metadata,
55-
Column("name", String),
55+
Column("name", String, nullable=False, unique=True),
5656
Column("schema", String), # schema encoded as JSON
5757
)
5858
self._metadata.create_all(self._engine, tables=[table])

src/chronify/store.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,30 @@ def schema_manager(self) -> SchemaManager:
259259
"""Return the store's schema manager."""
260260
return self._schema_mgr
261261

262+
def check_timestamps(self, name: str, connection: Connection | None = None) -> None:
263+
"""Check the timestamps in the table.
264+
265+
This is useful if you call a :meth:`ingest_table` many times with skip_time_checks=True
266+
and then want to check the final table.
267+
268+
Parameters
269+
----------
270+
name
271+
Name of the table to check.
272+
273+
Raises
274+
------
275+
InvalidTable
276+
Raised if the timestamps do not match the schema.
277+
"""
278+
table = self.get_table(name)
279+
schema = self._schema_mgr.get_schema(name)
280+
if connection is None:
281+
with self._engine.connect() as conn:
282+
check_timestamps(conn, table, schema)
283+
else:
284+
check_timestamps(connection, table, schema)
285+
262286
def create_view_from_parquet(
263287
self, path: Path, schema: TableSchema, bypass_checks: bool = False
264288
) -> None:
@@ -771,13 +795,13 @@ def _ingest_tables(
771795
conn: Connection,
772796
data: Iterable[pd.DataFrame | DuckDBPyRelation],
773797
schema: TableSchema,
774-
bypass_time_checks: bool = False,
798+
skip_time_checks: bool = False,
775799
) -> bool:
776800
created_table = False
777801
for table in data:
778802
if self._ingest_table(conn, table, schema):
779803
created_table = True
780-
if not bypass_time_checks:
804+
if not skip_time_checks:
781805
check_timestamps(conn, Table(schema.name, self._metadata), schema)
782806
return created_table
783807

tests/test_mapper_column_representative_to_datetime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def test_NYMDPV_mapper(time_series_NYMDPV, iter_store: Store):
146146
)
147147

148148
data = pd.read_csv(time_series_NYMDPV)
149-
iter_store.ingest_table(data, from_schema, bypass_time_checks=True)
149+
iter_store.ingest_table(data, from_schema, skip_time_checks=True)
150150

151151
metadata = MetaData()
152152
metadata.reflect(iter_store.engine, views=True)

tests/test_store.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,3 +770,12 @@ def test_read_raw_query(iter_stores_by_engine: Store, one_week_per_month_by_hour
770770
with store.engine.connect() as conn:
771771
df2 = store.read_raw_query(query, params=params, connection=conn)
772772
assert df2.equals(df[df["id"] == 2].reset_index(drop=True))
773+
774+
775+
def test_check_timestamps(iter_stores_by_engine: Store, one_week_per_month_by_hour_table) -> None:
776+
store = iter_stores_by_engine
777+
df, _, schema = one_week_per_month_by_hour_table
778+
store.ingest_table(df, schema)
779+
store.check_timestamps(schema.name)
780+
with store.engine.begin() as conn:
781+
store.check_timestamps(schema.name, connection=conn)

0 commit comments

Comments
 (0)