Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/update admin script #15

Merged
merged 8 commits into from
Dec 2, 2024
Merged
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
8 changes: 4 additions & 4 deletions sample.datasets.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[warehouses.pg_warehouse]
db_type = "postgresql"
username = "your_username"
password = "your_password"
database = "warehouse"
user = "warehouse_user"
password = "warehouse_password"
host = "localhost"
port = 5432
database = "companies_db"
port = 7654

[datasets.companies_house]
database = "pg_warehouse"
Expand Down
21 changes: 15 additions & 6 deletions src/matchbox/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@
import tomli
from dotenv import find_dotenv, load_dotenv

from matchbox.server import MatchboxDBAdapter, inject_backend
from matchbox.server.base import (
MatchboxSettings,
Source,
)
from matchbox.server.models import SourceWarehouse

logger = logging.getLogger("mb_logic")

dotenv_path = find_dotenv(usecwd=True)
load_dotenv(dotenv_path)


@inject_backend
def index_dataset(backend: MatchboxDBAdapter, dataset: Source) -> None:
backend.index(dataset=dataset)


def load_datasets_from_config(datasets: Path) -> dict[str, Source]:
"""Loads datasets for indexing from the datasets settings TOML file."""
config = tomli.load(datasets)
config = tomli.loads(datasets.read_text())

warehouses: dict[str, SourceWarehouse] = {}
for alias, warehouse_config in config["warehouses"].items():
Expand All @@ -36,17 +43,19 @@ def load_datasets_from_config(datasets: Path) -> dict[str, Source]:
@click.argument(
"datasets", type=click.Path(exists=True, dir_okay=False, path_type=Path)
)
def make_cmf(datasets: Path) -> None:
backend = MatchboxSettings().backend
@inject_backend
def make_matchbox(backend: MatchboxDBAdapter, datasets: Path) -> None:
dataset_dict = load_datasets_from_config(datasets=datasets)

for dataset in dataset_dict.values():
backend.index(dataset=dataset, engine=dataset.database.engine)
logger.info(f"Indexing {dataset}")
index_dataset(dataset)
logger.info(f"Finished indexing {dataset}")


if __name__ == "__main__":
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
make_cmf()
make_matchbox()
4 changes: 4 additions & 0 deletions src/matchbox/server/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
cast,
)

from dotenv import find_dotenv, load_dotenv
from pydantic import Field
from pydantic_settings import BaseSettings, SettingsConfigDict
from rustworkx import PyDiGraph
Expand All @@ -36,6 +37,9 @@
Results = Any


dotenv_path = find_dotenv(usecwd=True)
load_dotenv(dotenv_path, override=True)

R = TypeVar("R")
P = ParamSpec("P")

Expand Down
3 changes: 2 additions & 1 deletion src/matchbox/server/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ def _select(
def to_hash(self) -> bytes:
"""Generate a unique hash based on the table's columns and datatypes."""
table = self.to_table()
schema_representation = ",".join(

schema_representation = f"{str(self)}: " + ",".join(
f"{col.name}:{str(col.type)}" for col in table.columns
)
return HASH_FUNC(schema_representation.encode("utf-8")).digest()
Expand Down