Skip to content

Commit

Permalink
Bug fixes and improvements to release directives and channels commands (
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-melnacouzi authored Jan 10, 2025
1 parent 056af4e commit 4ca0526
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 290 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ def validate_release_channel(
f"Release channels are not enabled for application package {self.name}."
)
for channel in available_release_channels:
if same_identifiers(release_channel, channel["name"]):
if unquote_identifier(release_channel) == channel["name"]:
return

raise UsageError(
Expand Down Expand Up @@ -736,30 +736,15 @@ def action_release_directive_set(

sanitized_release_channel = self.get_sanitized_release_channel(release_channel)

if (
not same_identifiers(release_directive, DEFAULT_DIRECTIVE)
and not target_accounts
):
# if it is a non-default release directive with no target accounts specified,
# it means that the user wants to modify existing release directive
get_snowflake_facade().modify_release_directive(
package_name=self.name,
release_directive=release_directive,
release_channel=sanitized_release_channel,
version=version,
patch=patch,
role=self.role,
)
else:
get_snowflake_facade().set_release_directive(
package_name=self.name,
release_directive=release_directive,
release_channel=sanitized_release_channel,
target_accounts=target_accounts,
version=version,
patch=patch,
role=self.role,
)
get_snowflake_facade().set_release_directive(
package_name=self.name,
release_directive=release_directive,
release_channel=sanitized_release_channel,
target_accounts=target_accounts,
version=version,
patch=patch,
role=self.role,
)

def action_release_directive_unset(
self,
Expand Down Expand Up @@ -834,7 +819,7 @@ def action_release_channel_list(
channel
for channel in available_channels
if release_channel is None
or same_identifiers(channel["name"], release_channel)
or unquote_identifier(release_channel) == channel["name"]
]

if not filtered_channels:
Expand Down Expand Up @@ -1069,12 +1054,12 @@ def action_publish(

if not available_patches:
raise ClickException(
f"Version {version} does not exist in application package {self.name}."
f"Version {version} does not exist in application package {self.name}. Use --create-version flag to create a new version."
)

if patch not in available_patches:
raise ClickException(
f"Patch {patch} does not exist for version {version} in application package {self.name}."
f"Patch {patch} does not exist for version {version} in application package {self.name}. Use --create-version flag to add a new patch."
)

available_release_channels = get_snowflake_facade().show_release_channels(
Expand Down
111 changes: 25 additions & 86 deletions src/snowflake/cli/_plugins/nativeapp/sf_sql_facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ def create_version_in_package(
if err.errno == MAX_UNBOUND_VERSIONS_REACHED:
raise UserInputError(
f"Maximum unbound versions reached for application package {package_name}. "
"Please drop the other unbound version first, or add it to a release channel."
"Please drop other unbound versions first, or add them to a release channel. "
"Use `snow app version list` to view all versions.",
) from err
if err.errno == APPLICATION_PACKAGE_MAX_VERSIONS_HIT:
raise UserInputError(
Expand Down Expand Up @@ -414,6 +415,8 @@ def add_patch_to_package_version(
)

patch_query = f" {patch}" if patch is not None else ""

# No space between patch and patch{patch_query} to avoid extra space when patch is None
add_patch_query = dedent(
f"""\
alter application package {package_name}
Expand Down Expand Up @@ -670,6 +673,10 @@ def show_release_directives(
f"Insufficient privileges to show release directives for application package {package_name}",
role=role,
) from err
if err.errno == DOES_NOT_EXIST_OR_NOT_AUTHORIZED:
raise UserInputError(
f"Application package {package_name} does not exist or you are not authorized to access it."
) from err
handle_unclassified_error(
err,
f"Failed to show release directives for application package {package_name}.",
Expand Down Expand Up @@ -1027,29 +1034,26 @@ def set_release_directive(
@param [Optional] role: Role to switch to while running this script. Current role will be used if no role is passed in.
"""

if same_identifiers(release_directive, DEFAULT_DIRECTIVE) and target_accounts:
raise UserInputError(
"Default release directive does not support target accounts."
)

if (
not same_identifiers(release_directive, DEFAULT_DIRECTIVE)
and not target_accounts
):
raise UserInputError(
"Non-default release directives require target accounts to be specified."
)

package_name = to_identifier(package_name)
release_channel = to_identifier(release_channel) if release_channel else None
release_directive = to_identifier(release_directive)
version = to_identifier(version)

release_directive_statement = (
"set default release directive"
if same_identifiers(release_directive, DEFAULT_DIRECTIVE)
else f"set release directive {release_directive}"
)
if same_identifiers(release_directive, DEFAULT_DIRECTIVE):
if target_accounts:
raise UserInputError(
"Default release directive does not support target accounts."
)
release_directive_statement = "set default release directive"
else:
if target_accounts:
release_directive_statement = (
f"set release directive {release_directive}"
)
else:
release_directive_statement = (
f"modify release directive {release_directive}"
)

release_channel_statement = (
f"modify release channel {release_channel}" if release_channel else ""
Expand Down Expand Up @@ -1083,74 +1087,9 @@ def set_release_directive(
raise UserInputError(
f"Invalid account passed in.\n{str(err.msg)}"
) from err
_handle_release_directive_version_error(
err,
package_name=package_name,
release_channel=release_channel,
version=version,
patch=patch,
)
handle_unclassified_error(
err,
f"Failed to set release directive {release_directive} for application package {package_name}.",
)

def modify_release_directive(
self,
package_name: str,
release_directive: str,
release_channel: str | None,
version: str,
patch: int,
role: str | None = None,
):
"""
Modifies a release directive for an application package.
Release directive must already exist in the application package.
Accepts both default and non-default release directives.
@param package_name: Name of the application package to alter.
@param release_directive: Name of the release directive to modify.
@param release_channel: Name of the release channel to modify the release directive for.
@param version: Version to modify the release directive for.
@param patch: Patch number to modify the release directive for.
@param [Optional] role: Role to switch to while running this script. Current role will be used if no role is passed in.
"""

package_name = to_identifier(package_name)
release_channel = to_identifier(release_channel) if release_channel else None
release_directive = to_identifier(release_directive)
version = to_identifier(version)

release_directive_statement = (
"modify default release directive"
if same_identifiers(release_directive, DEFAULT_DIRECTIVE)
else f"modify release directive {release_directive}"
)

release_channel_statement = (
f"modify release channel {release_channel}" if release_channel else ""
)

full_query = dedent(
_strip_empty_lines(
f"""\
alter application package {package_name}
{release_channel_statement}
{release_directive_statement}
version = {version} patch = {patch}
"""
)
)

with self._use_role_optional(role):
try:
self._sql_executor.execute_query(full_query)
except Exception as err:
if isinstance(err, ProgrammingError):
if err.errno == RELEASE_DIRECTIVE_DOES_NOT_EXIST:
raise UserInputError(
f"Release directive {release_directive} does not exist in application package {package_name}. Please create it first by specifying the target accounts."
f"Release directive {release_directive} does not exist in application package {package_name}. Please create it first by specifying --target-accounts with the `snow app release-directive set` command."
) from err
_handle_release_directive_version_error(
err,
Expand All @@ -1161,7 +1100,7 @@ def modify_release_directive(
)
handle_unclassified_error(
err,
f"Failed to modify release directive {release_directive} for application package {package_name}.",
f"Failed to set release directive {release_directive} for application package {package_name}.",
)

def unset_release_directive(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# ---
# name: test_given_release_channel_with_no_target_account_or_version_then_show_all_accounts_in_snapshot
'''
channel1
CHANNEL1
Description: desc
Versions: ()
Created on: 2024-12-03 00:00:00.000000 UTC
Expand All @@ -18,7 +18,7 @@
# ---
# name: test_given_release_channels_with_a_selected_channel_to_filter_when_list_release_channels_then_returned_selected_channel
'''
channel1
CHANNEL1
Description: desc
Versions: (v1, v2)
Created on: 2024-12-03 00:00:00.000000 UTC
Expand All @@ -29,13 +29,13 @@
# ---
# name: test_given_release_channels_with_proper_values_when_list_release_channels_then_success
'''
channel1
CHANNEL1
Description: desc
Versions: (v1, v2)
Created on: 2024-12-03 00:00:00.000000 UTC
Updated on: 2024-12-05 00:00:00.000000 UTC
Target accounts: (org1.acc1, org2.acc2)
channel2
CHANNEL2
Description: desc2
Versions: (v3)
Created on: 2024-12-03 00:00:00.000000 UTC
Expand Down
Loading

0 comments on commit 4ca0526

Please sign in to comment.