Skip to content
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
15 changes: 13 additions & 2 deletions pum/config_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,22 @@ class DemoDataModel(PumCustomBaseModel):
DemoDataModel represents a configuration for demo data.

Attributes:
files: Optional list of named demo data files.
name: Name of the demo data.
file: Optional path to a single demo data file.
files: Optional list of paths to multiple demo data files.
"""

name: str = Field(..., description="Name of the demo data.")
file: str = Field(..., description="Path to the demo data file.")

file: Optional[str] = None
files: Optional[List[str]] = None

@model_validator(mode="after")
def validate_args(self):
file, files = self.file, self.files
if (file and files) or (not file and not files):
raise PumConfigError("Exactly one of 'file' or 'files' must be set in a demo data set.")
return self


class DependencyModel(PumCustomBaseModel):
Expand Down
7 changes: 5 additions & 2 deletions pum/pum_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,12 @@ def post_hook_handlers(self) -> list[HookHandler]:
else []
)

def demo_data(self) -> dict[str, str]:
def demo_data(self) -> dict[str, list[str]]:
"""Return a dictionary of demo data files defined in the configuration."""
return {dm.name: dm.file for dm in self.config.demo_data}
demo_data_files = {}
for dm in self.config.demo_data:
demo_data_files[dm.name] = dm.files or [dm.file]
return demo_data_files

def __del__(self):
# Cleanup temporary directories and sys.path modifications
Expand Down
15 changes: 8 additions & 7 deletions pum/upgrader.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,21 @@ def install_demo_data(
if name not in self.config.demo_data():
raise PumException(f"Demo data '{name}' not found in the configuration.")

demo_data_file = self.config.base_path / self.config.demo_data()[name]
logger.info("Installing demo data from %s", demo_data_file)
logger.info(f"Installing demo data {name}")

for pre_hook in self.config.pre_hook_handlers():
pre_hook.execute(connection=connection, commit=False, parameters=parameters)

connection.commit()

parameters_literals = SqlContent.prepare_parameters(parameters)
SqlContent(sql=demo_data_file).execute(
connection=connection,
commit=False,
parameters=parameters_literals,
)
for demo_data_file in self.config.demo_data()[name]:
demo_data_file = self.config.base_path / demo_data_file
SqlContent(sql=demo_data_file).execute(
connection=connection,
commit=False,
parameters=parameters_literals,
)

connection.commit()

Expand Down
7 changes: 7 additions & 0 deletions test/data/demo_data_multi/.pum.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


demo_data:
- name: some cool demo dataset
files:
- demo_data/demo_data_1.sql
- demo_data/demo_data_2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE SCHEMA IF NOT EXISTS pum_test_data;

CREATE TABLE pum_test_data.some_table (
id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
created_date DATE DEFAULT CURRENT_DATE,
is_active BOOLEAN DEFAULT TRUE,
amount NUMERIC(10,2)
);
6 changes: 6 additions & 0 deletions test/data/demo_data_multi/demo_data/demo_data_1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

INSERT INTO pum_test_data.some_table (id, name, created_date, is_active, amount)
VALUES
(1, 'Test Item 1', '2023-01-01', TRUE, 100.00),
(2, 'Test Item 2', '2023-01-02', FALSE, 200.00)
;
6 changes: 6 additions & 0 deletions test/data/demo_data_multi/demo_data/demo_data_2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

INSERT INTO pum_test_data.some_table (id, name, created_date, is_active, amount)
VALUES
(3, 'Test Item 3', '2023-01-03', TRUE, 300.00),
(4, 'Test Item 4', '2023-01-04', TRUE, 400.00)
;
18 changes: 18 additions & 0 deletions test/test_upgrader.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,24 @@ def test_demo_data(self) -> None:
count = cursor.fetchone()[0]
self.assertEqual(count, 4)

def test_demo_data_multi(self) -> None:
"""Test the installation of demo data."""
test_dir = Path("test") / "data" / "demo_data_multi"
cfg = PumConfig.from_yaml(test_dir / ".pum.yaml")
sm = SchemaMigrations(cfg)
with psycopg.connect(f"service={self.pg_service}") as conn:
self.assertFalse(sm.exists(conn))
upgrader = Upgrader(config=cfg)
upgrader.install(connection=conn)
with self.assertRaises(PumException):
upgrader.install_demo_data(connection=conn, name="nope, nothing here fella")
upgrader.install_demo_data(connection=conn, name="some cool demo dataset")
self.assertTrue(sm.exists(conn))
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM pum_test_data.some_table;")
count = cursor.fetchone()[0]
self.assertEqual(count, 4)

def test_dependencies(self) -> None:
"""Test the installation of dependencies."""
test_dir = Path("test") / "data" / "dependencies"
Expand Down