Skip to content

Commit

Permalink
refactor: move shared changelog utilities
Browse files Browse the repository at this point in the history
- Move functions shared by multiple changelog scripts to util/changelog.py from the individual scripts
- Reset changeset id for changelog tests
  • Loading branch information
nelsonwmoore committed Aug 8, 2023
1 parent 5387381 commit 699b46c
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 101 deletions.
35 changes: 2 additions & 33 deletions python/scripts/make_diff_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
an MDB in Neo4J to update the model from the old version to the new one.
"""

import configparser
import logging
from pathlib import Path
from typing import Dict, Generator, List, Optional, Tuple, Union
from typing import Dict, List, Optional, Tuple, Union

import click
from bento_mdf.diff import diff_models
from bento_mdf.mdf import MDF
from bento_meta.entity import Entity
from bento_meta.objects import Edge, Node, Property, Term
from bento_meta.util.changelog import changeset_id_generator, update_config_changeset_id
from bento_meta.util.cypher.clauses import (
Delete,
DetachDelete,
Expand Down Expand Up @@ -291,37 +291,6 @@ def convert_diff_segment_to_cypher_statement(
return stmt


def get_initial_changeset_id(config_file_path: str) -> int:
"""Gets initial changeset id from changelog config file"""
config = configparser.ConfigParser()
config.read(config_file_path)
try:
return config.getint(section="changelog", option="changeset_id")
except (configparser.NoSectionError, configparser.NoOptionError) as error:
print(error)
raise


def changeset_id_generator(config_file_path: str) -> Generator[int, None, None]:
"""
Iterator for changeset_id. Gets latest changeset id from changelog.ini
and provides up-to-date changeset id as
"""
i = get_initial_changeset_id(config_file_path)
while True:
yield i
i += 1


def update_config_changeset_id(config_file_path: str, new_changeset_id: int) -> None:
"""Updates changelog config file with new changeset id"""
config = configparser.ConfigParser()
config.read(config_file_path)
config.set(section="changelog", option="changeset_id", value=str(new_changeset_id))
with open(file=config_file_path, mode="w", encoding="UTF-8") as config_file:
config.write(fp=config_file)


def convert_diff_to_changelog(
diff, model_handle: str, author: str, config_file_path: str
) -> Changelog:
Expand Down
36 changes: 3 additions & 33 deletions python/scripts/make_mapping_changelog.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""converts mapping MDF to changelog"""
import configparser

from ast import literal_eval
from pathlib import Path
from typing import Dict, Generator, List, Optional, Union
from typing import Dict, List, Optional, Union

import click
import yaml
from bento_meta.util.changelog import changeset_id_generator, update_config_changeset_id
from bento_meta.util.cypher.clauses import (
Case,
Create,
Expand All @@ -21,37 +22,6 @@
from liquichange.changelog import Changelog, Changeset, CypherChange


def get_initial_changeset_id(config_file_path: str) -> int:
"""Gets initial changeset id from changelog config file"""
config = configparser.ConfigParser()
config.read(config_file_path)
try:
return config.getint(section="changelog", option="changeset_id")
except (configparser.NoSectionError, configparser.NoOptionError) as error:
print(error)
raise


def changeset_id_generator(config_file_path: str) -> Generator[int, None, None]:
"""
Iterator for changeset_id. Gets latest changeset id from changelog.ini
and provides up-to-date changeset id as
"""
i = get_initial_changeset_id(config_file_path)
while True:
yield i
i += 1


def update_config_changeset_id(config_file_path: str, new_changeset_id: int) -> None:
"""Updates changelog config file with new changeset id"""
config = configparser.ConfigParser()
config.read(config_file_path)
config.set(section="changelog", option="changeset_id", value=str(new_changeset_id))
with open(file=config_file_path, mode="w", encoding="UTF-8") as config_file:
config.write(fp=config_file)


def load_yaml(
file: str,
) -> Dict[
Expand Down
32 changes: 2 additions & 30 deletions python/scripts/make_model_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,24 @@
Script takes one MDF file representing a model and produces a Liquibase
Changelog with the necessary changes to add the model to an MDB
"""
import configparser
import logging
from pathlib import Path
from typing import Dict, Generator, List, Optional, Union
from typing import Dict, List, Optional, Union

import click
from bento_mdf.mdf import MDF
from bento_meta.entity import Entity
from bento_meta.mdb.mdb import make_nanoid
from bento_meta.model import Model
from bento_meta.objects import Concept, Term, ValueSet
from bento_meta.util.changelog import changeset_id_generator, update_config_changeset_id
from bento_meta.util.cypher.clauses import Create, Match, Merge, OnCreateSet, Statement
from bento_meta.util.cypher.entities import N, R, T, _plain_var
from liquichange.changelog import Changelog, Changeset, CypherChange

logger = logging.getLogger(__name__)


def get_initial_changeset_id(config_file_path: str) -> int:
"""Gets initial changeset id from changelog config file"""
config = configparser.ConfigParser()
config.read(config_file_path)
try:
return config.getint(section="changelog", option="changeset_id")
except (configparser.NoSectionError, configparser.NoOptionError) as error:
logger.error(f"Reading changeset ID failed: {error}")
raise


def changeset_id_generator(config_file_path: str) -> Generator[int, None, None]:
"""Generates sequential changeset IDs by reading the latest ID from a config file."""
i = get_initial_changeset_id(config_file_path)
while True:
yield i
i += 1


def update_config_changeset_id(config_file_path: str, new_changeset_id: int) -> None:
"""Updates changelog config file with new changeset id."""
config = configparser.ConfigParser()
config.read(config_file_path)
config.set(section="changelog", option="changeset_id", value=str(new_changeset_id))
with open(file=config_file_path, mode="w", encoding="UTF-8") as config_file:
config.write(fp=config_file)


def escape_quotes_in_attr(entity: Entity) -> None:
"""
Escapes quotes in entity attributes.
Expand Down
1 change: 1 addition & 0 deletions python/src/bento_meta/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class Property(Entity):
"units": "units",
"item_domain": "item_domain",
"is_required": "is_required",
"parent_handle": "parent_handle",
},
"relationship": {
"concept": {"rel": ":has_concept>", "end_cls": "Concept"},
Expand Down
3 changes: 2 additions & 1 deletion python/src/bento_meta/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
bento_meta.util.cypher - Represent Cypher queries programmatically
bento_meta.util.makeq - Create Cypher queries from API endpoint paths
bento_meta.util.changelog - Utilities for working with Liquibase Changelogs
"""

from . import cypher
from . import changelog, cypher
38 changes: 38 additions & 0 deletions python/src/bento_meta/util/changelog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
bento_meta.util.changelog
Common functions shared by changelog generation scripts
"""
import configparser
import logging
from typing import Generator, Optional

logger = logging.getLogger(__name__)


def get_initial_changeset_id(config_file_path: str) -> int:
"""Gets initial changeset id from changelog config file"""
config = configparser.ConfigParser()
config.read(config_file_path)
try:
return config.getint(section="changelog", option="changeset_id")
except (configparser.NoSectionError, configparser.NoOptionError) as error:
logger.error(f"Reading changeset ID failed: {error}")
raise


def changeset_id_generator(config_file_path: str) -> Generator[int, None, None]:
"""Generates sequential changeset IDs by reading the latest ID from a config file."""
i = get_initial_changeset_id(config_file_path)
while True:
yield i
i += 1


def update_config_changeset_id(config_file_path: str, new_changeset_id: int) -> None:
"""Updates changelog config file with new changeset id."""
config = configparser.ConfigParser()
config.read(config_file_path)
config.set(section="changelog", option="changeset_id", value=str(new_changeset_id))
with open(file=config_file_path, mode="w", encoding="UTF-8") as config_file:
config.write(fp=config_file)
2 changes: 1 addition & 1 deletion python/tests/samples/test_changelog.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[changelog]
changeset_id = 6855
changeset_id = 1

7 changes: 4 additions & 3 deletions python/tests/test_014changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from bento_mdf.diff import diff_models
from bento_mdf.mdf import MDF
from bento_meta.objects import Property
from bento_meta.util.changelog import update_config_changeset_id
from scripts.make_diff_changelog import convert_diff_to_changelog
from scripts.make_mapping_changelog import convert_mappings_to_changelog
from scripts.make_model_changelog import (
Expand Down Expand Up @@ -33,6 +34,7 @@ def test_make_model_changelog():
changelog = convert_model_to_changelog(
model=mdf.model, author=AUTHOR, config_file_path=TEST_CHANGELOG_CONFIG
)
update_config_changeset_id(TEST_CHANGELOG_CONFIG, 1)
assert len(changelog.subelements) == 46


Expand All @@ -47,6 +49,7 @@ def test_make_diff_changelog():
author=AUTHOR,
config_file_path=TEST_CHANGELOG_CONFIG,
)
update_config_changeset_id(TEST_CHANGELOG_CONFIG, 1)
assert len(changelog.subelements) == 33


Expand All @@ -58,6 +61,7 @@ def test_make_mapping_changelog():
config_file_path=TEST_CHANGELOG_CONFIG,
_commit=_COMMIT,
)
update_config_changeset_id(TEST_CHANGELOG_CONFIG, 1)
assert len(changelog.subelements) == 6


Expand All @@ -69,6 +73,3 @@ def test_escape_quotes_in_attr():

assert prop.handle == r"""Quote\'s Handle"""
assert prop.desc == r"""quote\'s quote\'s \"quotes\""""


test_make_model_changelog()

0 comments on commit 699b46c

Please sign in to comment.