Skip to content

Commit dde91d2

Browse files
authored
Fix: Make sure start_at / end_at are strings when storing an environment record in state sync (#870)
1 parent 2babe3d commit dde91d2

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

sqlmesh/core/state_sync/engine_adapter.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
from sqlmesh.core.snapshot.definition import _parents_from_model
4444
from sqlmesh.core.state_sync.base import SCHEMA_VERSION, StateSync, Versions
4545
from sqlmesh.core.state_sync.common import CommonStateSyncMixin, transactional
46-
from sqlmesh.utils.date import now_timestamp
46+
from sqlmesh.utils.date import now_timestamp, time_like_to_str
4747
from sqlmesh.utils.errors import SQLMeshError
4848

4949
logger = logging.getLogger(__name__)
@@ -617,8 +617,8 @@ def _environment_to_df(environment: Environment) -> pd.DataFrame:
617617
{
618618
"name": environment.name,
619619
"snapshots": json.dumps([snapshot.dict() for snapshot in environment.snapshots]),
620-
"start_at": environment.start_at,
621-
"end_at": environment.end_at,
620+
"start_at": time_like_to_str(environment.start_at),
621+
"end_at": time_like_to_str(environment.end_at) if environment.end_at else None,
622622
"plan_id": environment.plan_id,
623623
"previous_plan_id": environment.previous_plan_id,
624624
"expiration_ts": environment.expiration_ts,

sqlmesh/utils/date.py

+8
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,11 @@ def validate_date_range(
257257
raise ValueError(
258258
f"Start date / time ({start}) can't be greater than end date / time ({end})"
259259
)
260+
261+
262+
def time_like_to_str(time_like: TimeLike) -> str:
263+
if isinstance(time_like, str):
264+
return time_like
265+
if is_date(time_like):
266+
return to_ds(time_like)
267+
return to_datetime(time_like).isoformat()

tests/core/test_state_sync.py

+31
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,37 @@ def test_delete_expired_environments(state_sync: EngineAdapterStateSync, make_sn
551551
assert state_sync.get_environment(env_b.name) == env_b
552552

553553

554+
def test_environment_start_as_timestamp(
555+
state_sync: EngineAdapterStateSync, make_snapshot: t.Callable
556+
):
557+
snapshot = make_snapshot(
558+
SqlModel(
559+
name="a",
560+
query=parse_one("select a, ds"),
561+
),
562+
)
563+
snapshot.categorize_as(SnapshotChangeCategory.BREAKING)
564+
565+
state_sync.push_snapshots([snapshot])
566+
567+
now_ts = now_timestamp()
568+
569+
env = Environment(
570+
name="test_environment_a",
571+
snapshots=[snapshot.table_info],
572+
start_at=now_ts,
573+
end_at=None,
574+
plan_id="test_plan_id",
575+
previous_plan_id="test_plan_id",
576+
expiration_ts=now_ts - 1000,
577+
)
578+
state_sync.promote(env)
579+
580+
stored_env = state_sync.get_environment(env.name)
581+
assert stored_env
582+
assert stored_env.start_at == to_datetime(now_ts).isoformat()
583+
584+
554585
def test_missing_intervals(sushi_context_pre_scheduling: Context) -> None:
555586
sushi_context = sushi_context_pre_scheduling
556587
state_sync = sushi_context.state_reader

0 commit comments

Comments
 (0)