Skip to content

Commit

Permalink
fix: standardize "" and None alphanum_key behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Zane committed Oct 18, 2024
1 parent fc0d0f1 commit 6b1c1b3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
8 changes: 5 additions & 3 deletions schemachange/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ def alphanum_convert(text: str):
# Each number is converted to and integer and string parts are left as strings
# This will enable correct sorting in python when the lists are compared
# e.g. get_alphanum_key('1.2.2') results in ['', 1, '.', 2, '.', 2, '']
def get_alphanum_key(key):
def get_alphanum_key(key: str | int | None) -> list:
if key == "" or key is None:
return []
alphanum_key = [alphanum_convert(c) for c in re.split("([0-9]+)", key)]
return alphanum_key

Expand Down Expand Up @@ -100,7 +102,7 @@ def deploy(config: DeployConfig, session: SnowflakeSession):
script_metadata = versioned_scripts.get(script.name)

if (
max_published_version != ""
max_published_version is not None
and get_alphanum_key(script.version) <= max_published_version
):
if script_metadata is None:
Expand All @@ -113,7 +115,7 @@ def deploy(config: DeployConfig, session: SnowflakeSession):
else:
script_log.debug(
"Script has already been applied",
max_published_version=str(max_published_version),
max_published_version=max_published_version,
)
if script_metadata["checksum"] != checksum_current:
script_log.info("Script checksum has drifted since application")
Expand Down
10 changes: 3 additions & 7 deletions schemachange/session/SnowflakeSession.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,7 @@ def get_script_metadata(

self.logger.info(
"Max applied change script version %(max_published_version)s"
% {
"max_published_version": max_published_version
if max_published_version != ""
else "None"
}
% {"max_published_version": max_published_version}
)
return change_history, r_scripts_checksum, max_published_version

Expand Down Expand Up @@ -251,10 +247,10 @@ def fetch_versioned_scripts(

# Collect all the results into a list
versioned_scripts: dict[str, dict[str, str | int]] = defaultdict(dict)
versions: list[str | int] = []
versions: list[str | int | None] = []
for cursor in results:
for version, script, checksum in cursor:
versions.append(version)
versions.append(version if version != "" else None)
versioned_scripts[script] = {
"version": version,
"script": script,
Expand Down
6 changes: 5 additions & 1 deletion tests/test_cli_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ def test_alphanum_convert_given__lowercase():


def test_get_alphanum_key_given__empty_string():
assert get_alphanum_key("") == [""]
assert get_alphanum_key("") == []


def test_get_alphanum_key_given__none():
assert get_alphanum_key(None) == []


def test_get_alphanum_key_given__numbers_only():
Expand Down

0 comments on commit 6b1c1b3

Please sign in to comment.