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

YAML vocab files #70

Merged
merged 8 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,9 @@ sqlsynthgen/config.ini
# sphinx
docs/build/*
docs/temp/*

# vim swap files
*.swp

ssg.py
orm.py
10 changes: 5 additions & 5 deletions sqlsynthgen/base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Base generator classes."""
import csv
from dataclasses import dataclass
from pathlib import Path
from typing import Any

import yaml
from sqlalchemy import insert


Expand All @@ -15,9 +15,9 @@ class FileUploader:

def load(self, connection: Any) -> None:
"""Load the data from file."""
with Path(self.table.fullname + ".csv").open(
with Path(self.table.fullname + ".yaml").open(
"r", newline="", encoding="utf-8"
) as csvfile:
reader = csv.DictReader(csvfile)
stmt = insert(self.table).values(list(reader))
) as yamlfile:
rows = yaml.load(yamlfile, Loader=yaml.Loader)
stmt = insert(self.table).values(list(rows))
connection.execute(stmt)
8 changes: 7 additions & 1 deletion sqlsynthgen/create.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Functions and classes to create and populate the target database."""
import logging
from typing import Any, Dict

from sqlalchemy import create_engine, insert
from sqlalchemy.exc import IntegrityError
from sqlalchemy.schema import CreateSchema

from sqlsynthgen.settings import get_settings
Expand Down Expand Up @@ -42,7 +44,11 @@ def create_db_vocab(vocab_dict: Dict[str, Any]) -> None:

with dst_engine.connect() as dst_conn:
for vocab_table in vocab_dict.values():
vocab_table.load(dst_conn)
try:
vocab_table.load(dst_conn)
except IntegrityError as e:
logging.warning("Loading the vocabulary table %s failed:", vocab_table)
logging.warning(e)


def create_db_data(sorted_tables: list, generator_dict: dict, num_passes: int) -> None:
Expand Down
1 change: 1 addition & 0 deletions sqlsynthgen/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def make_generators(
Must be in the current working directory.
ssg_file (str): Path to write the generators file to.
config_file (str): Path to configuration file.
stats_file (str): Path to source stats file (output of make-stats).
"""
ssg_file_path = Path(ssg_file)
if ssg_file_path.exists():
Expand Down
2 changes: 1 addition & 1 deletion sqlsynthgen/make.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"from mimesis.locales import Locale",
"from sqlsynthgen.base import FileUploader",
"",
"generic = Generic(locale=Locale.EN)",
"generic = Generic(locale=Locale.EN_GB)",
"",
)
)
Expand Down
20 changes: 8 additions & 12 deletions sqlsynthgen/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Utility functions."""
import csv
import os
import sys
from importlib import import_module
Expand Down Expand Up @@ -44,22 +43,19 @@ def import_file(file_name: str) -> ModuleType:


def download_table(table: Any, engine: Any) -> None:
"""Download a Table and store it as a .csv file."""
csv_file_name = table.fullname + ".csv"
csv_file_path = Path(csv_file_name)
if csv_file_path.exists():
print(f"{str(csv_file_name)} already exists. Exiting...", file=stderr)
"""Download a Table and store it as a .yaml file."""
yaml_file_name = table.fullname + ".yaml"
yaml_file_path = Path(yaml_file_name)
if yaml_file_path.exists():
print(f"{str(yaml_file_name)} already exists. Exiting...", file=stderr)
sys.exit(1)

stmt = select([table])
with engine.connect() as conn:
result = list(conn.execute(stmt))
result = [dict(row.items()) for row in conn.execute(stmt)]

with csv_file_path.open("w", newline="", encoding="utf-8") as csvfile:
writer = csv.writer(csvfile, delimiter=",")
writer.writerow([x.name for x in table.columns])
for row in result:
writer.writerow(row)
with yaml_file_path.open("w", newline="", encoding="utf-8") as yamlfile:
yamlfile.write(yaml.dump(result))


def create_engine_with_search_path(postgres_dsn: PostgresDsn, schema_name: str) -> Any:
Expand Down
4 changes: 0 additions & 4 deletions tests/examples/basetable.csv

This file was deleted.

3 changes: 3 additions & 0 deletions tests/examples/basetable.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- id: 1
- id: 2
- id: 3
4 changes: 4 additions & 0 deletions tests/examples/example_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ tables:
- visit_duration_seconds
concept:
vocabulary_table: true
concept_type:
vocabulary_table: true
mitigation_type:
vocabulary_table: true
1 change: 1 addition & 0 deletions tests/examples/expected.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- id: 1
2 changes: 1 addition & 1 deletion tests/examples/expected_ssg.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from mimesis.locales import Locale
from sqlsynthgen.base import FileUploader

generic = Generic(locale=Locale.EN)
generic = Generic(locale=Locale.EN_GB)

from sqlsynthgen.providers import BytesProvider
generic.add_provider(BytesProvider)
Expand Down
Loading