Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions sdk/python/feast/feature_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -1998,6 +1998,13 @@ def teardown(self):

self._get_provider().teardown_infra(self.project, tables, entities) # type: ignore[arg-type]

# The infra objects tracked in the registry no longer exist. Clear them
# while the project is still registered (so the registry does not
# re-create it), otherwise the next apply diffs against stale infra and
# skips re-creating the tables. SQL registries drop this record in
# delete_project; the file registry keeps it in the registry proto.
self.registry.update_infra(Infra(), self.project, commit=True)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two added tests cover distinct behaviors, so keeping both seems reasonable.
However, update_infra(Infra(), self.project) clears infrastructure tracking globally; tearing down one project can leave another project’s obsolete tables untracked.
Could we preserve records for surviving tables and extend the teardown test to cover projects sharing a registry with separate databases?


for project in self.list_projects():
self.registry.delete_project(project.name)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sqlite3
from datetime import datetime, timedelta, timezone
from tempfile import mkstemp
from unittest.mock import AsyncMock, Mock, patch
Expand All @@ -23,7 +24,9 @@
from feast.permissions.action import AuthzedAction
from feast.permissions.permission import Permission
from feast.permissions.policy import RoleBasedPolicy
from feast.project import Project
from feast.repo_config import RegistryConfig, RepoConfig
from feast.repo_contents import RepoContents
from feast.saved_dataset import SavedDataset
from feast.stream_feature_view import stream_feature_view
from feast.transformation.pandas_transformation import PandasTransformation
Expand Down Expand Up @@ -1006,6 +1009,100 @@ def feature_store_with_local_registry():
)


def _sqlite_tables(path) -> set:
conn = sqlite3.connect(path)
try:
return {
row[0] for row in conn.execute("SELECT name FROM sqlite_master").fetchall()
}
finally:
conn.close()


def _sqlite_store_config(tmp_path, project: str) -> RepoConfig:
return RepoConfig(
registry=str(tmp_path / "registry.db"),
project=project,
provider="local",
online_store=SqliteOnlineStoreConfig(path=str(tmp_path / "online_store.db")),
entity_key_serialization_version=3,
)


def _plan_and_apply(store: FeatureStore, entity: Entity, feature_views) -> None:
"""Applies like `feast apply` does: plan against the registry, then apply the diffs."""
repo_contents = RepoContents(
projects=[Project(name=store.project)],
data_sources=[fv.batch_source for fv in feature_views],
feature_views=list(feature_views),
on_demand_feature_views=[],
stream_feature_views=[],
label_views=[],
entities=[entity],
feature_services=[],
permissions=[],
)
registry_diff, infra_diff, new_infra = store.plan(repo_contents)
store._apply_diffs(registry_diff, infra_diff, new_infra)


def _driver_stats_view(tmp_path, entity: Entity, name: str) -> FeatureView:
return FeatureView(
name=name,
entities=[entity],
schema=[Field(name="conv_rate", dtype=Float32)],
source=FileSource(
path=str(tmp_path / "driver_stats.parquet"),
timestamp_field="event_timestamp",
),
)


def test_apply_after_teardown_recreates_online_store_tables(tmp_path):
config = _sqlite_store_config(tmp_path, "teardown_apply")
online_store_path = tmp_path / "online_store.db"
entity = Entity(name="driver", join_keys=["driver_id"], value_type=ValueType.INT64)
feature_view = _driver_stats_view(tmp_path, entity, "driver_stats")

store = FeatureStore(config=config)
_plan_and_apply(store, entity, [feature_view])
assert "teardown_apply_driver_stats" in _sqlite_tables(online_store_path)

store.teardown()
assert not online_store_path.exists()

store = FeatureStore(config=config)
_plan_and_apply(store, entity, [feature_view])
assert "teardown_apply_driver_stats" in _sqlite_tables(online_store_path)

store.teardown()


def test_registry_only_project_delete_keeps_tracked_infra(tmp_path):
"""`feast projects delete` only deletes registry metadata, not online store
tables, so the tracked infra must survive it: a later apply of a renamed view
still has to drop the obsolete table."""
config = _sqlite_store_config(tmp_path, "projects_delete")
online_store_path = tmp_path / "online_store.db"
entity = Entity(name="driver", join_keys=["driver_id"], value_type=ValueType.INT64)

store = FeatureStore(config=config)
_plan_and_apply(store, entity, [_driver_stats_view(tmp_path, entity, "old_stats")])
assert "projects_delete_old_stats" in _sqlite_tables(online_store_path)

# Same as `feast projects delete`: registry metadata only, no teardown_infra.
store.registry.delete_project("projects_delete")

store = FeatureStore(config=config)
_plan_and_apply(store, entity, [_driver_stats_view(tmp_path, entity, "new_stats")])

tables = _sqlite_tables(online_store_path)
assert "projects_delete_new_stats" in tables
assert "projects_delete_old_stats" not in tables

store.teardown()


@pytest.mark.parametrize(
"test_feature_store",
[lazy_fixture("feature_store_with_local_registry")],
Expand Down