Skip to content

Commit

Permalink
Move version create to app package entity
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-fcampbell committed Sep 20, 2024
1 parent 9176f5b commit a5eccc4
Show file tree
Hide file tree
Showing 10 changed files with 620 additions and 658 deletions.
46 changes: 1 addition & 45 deletions src/snowflake/cli/_plugins/nativeapp/application_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
COMMENT_COL,
NAME_COL,
OWNER_COL,
PATCH_COL,
SPECIAL_COMMENT,
VERSION_COL,
)
from snowflake.cli._plugins.nativeapp.exceptions import (
ApplicationPackageDoesNotExistError,
Expand Down Expand Up @@ -59,16 +57,11 @@
NOT_SUPPORTED_ON_DEV_MODE_APPLICATIONS,
ONLY_SUPPORTED_ON_DEV_MODE_APPLICATIONS,
)
from snowflake.cli.api.exceptions import SnowflakeSQLExecutionError
from snowflake.cli.api.project.schemas.entities.common import PostDeployHook
from snowflake.cli.api.project.util import (
extract_schema,
identifier_to_show_like_pattern,
unquote_identifier,
)
from snowflake.cli.api.utils.cursor import find_all_rows
from snowflake.connector import ProgrammingError
from snowflake.connector.cursor import DictCursor

# Reasons why an `alter application ... upgrade` might fail
UPGRADE_RESTRICTION_CODES = {
Expand Down Expand Up @@ -423,7 +416,7 @@ def deploy(
# versioned dev
if version:
try:
version_exists = cls.get_existing_version_info(
version_exists = ApplicationPackageEntity.get_existing_version_info(
version=version,
package_name=package_name,
package_role=package_role,
Expand Down Expand Up @@ -656,40 +649,3 @@ def get_existing_app_info(
return sql_executor.show_specific_object(
"applications", app_name, name_col=NAME_COL
)

@staticmethod
def get_existing_version_info(
version: str,
package_name: str,
package_role: str,
) -> Optional[dict]:
"""
Get the latest patch on an existing version by name in the application package.
Executes 'show versions like ... in application package' query and returns
the latest patch in the version as a single row, if one exists. Otherwise,
returns None.
"""
sql_executor = get_sql_executor()
with sql_executor.use_role(package_role):
try:
query = f"show versions like {identifier_to_show_like_pattern(version)} in application package {package_name}"
cursor = sql_executor.execute_query(query, cursor_class=DictCursor)

if cursor.rowcount is None:
raise SnowflakeSQLExecutionError(query)

matching_rows = find_all_rows(
cursor, lambda row: row[VERSION_COL] == unquote_identifier(version)
)

if not matching_rows:
return None

return max(matching_rows, key=lambda row: row[PATCH_COL])

except ProgrammingError as err:
if err.msg.__contains__("does not exist or not authorized"):
raise ApplicationPackageDoesNotExistError(package_name)
else:
generic_sql_error_handler(err=err, role=package_role)
return None
Loading

0 comments on commit a5eccc4

Please sign in to comment.