Skip to content

Commit

Permalink
fix package scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-cgorrie committed Dec 14, 2023
1 parent c749e44 commit 6509f5d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
11 changes: 6 additions & 5 deletions src/snowcli/cli/appify/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,13 @@ def appify(
dest="./metadata",
)
)
artifacts.append(
dict(
src=str(dumper.stages_path.relative_to(project.path)),
dest="./stages",
if dumper.referenced_stage_ids:
artifacts.append(
dict(
src=str(dumper.stages_path.relative_to(project.path)),
dest="./stages",
)
)
)
snowflake_yml["native_app"]["artifacts"] = as_document(artifacts)

# add the package script, if we created one
Expand Down
18 changes: 16 additions & 2 deletions src/snowcli/cli/appify/generate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Generator, List, Tuple

import re
import logging
from textwrap import dedent
from contextlib import contextmanager
from pathlib import Path
Expand All @@ -17,6 +18,8 @@
)
from snowcli.cli.project.schemas.project_definition import project_schema

log = logging.getLogger(__name__)

APP_PUBLIC = "app_public"

CALLABLE_LOWER_KINDS = ["function", "procedure"]
Expand Down Expand Up @@ -156,13 +159,14 @@ def _rewrite_imports(stmt: str, suffix: str = "") -> str:

# qualify the identifier with the schema it lives within in the app
expr = re.compile(
f"^(create or replace [a-zA-Z_]+ {IDENTIFIER})\s*[(\n]", re.IGNORECASE
f"^(create or replace ([a-zA-Z_]+) {IDENTIFIER})\s*[(\n]", re.IGNORECASE
)
if match := expr.match(ddl_statement):
finalpos = len(match.group(1))
actual_kind = match.group(2) # XXX: otherwise views become tables
# FIXME: likely quoting is wrong here.
ddl_statement = (
f"create or replace {kind} {schema}.{bare_object_name}"
f"create or replace {actual_kind} {schema}.{bare_object_name}"
+ ddl_statement[finalpos:]
)
else:
Expand Down Expand Up @@ -216,6 +220,11 @@ def generate_setup_statements(

for fqn in ordering:
(_db, schema, object_name) = split_fqn_id(fqn)

if fqn not in catalog:
log.debug(f"Setup script: skipping {fqn} (not in catalog)")
continue

kind = catalog[fqn]["kind"]
# XXX: is this correct quoting?
yield f"execute immediate from './metadata/{schema}/{object_name}.sql';"
Expand Down Expand Up @@ -260,6 +269,11 @@ def generate_package_statements(
yield f"create schema if not exists {JINJA_PACKAGE_NAME}.{REF_PACKAGE_SCHEMA_NAME};"
yield f"grant usage on schema {JINJA_PACKAGE_NAME}.{REF_PACKAGE_SCHEMA_NAME} to share in application package {JINJA_PACKAGE_NAME};"

# grant reference_usage to all unique databases our external tables live in
seen_external_dbs = set([split_fqn_id(fqn)[0] for fqn in seen_external_tables])
for db in seen_external_dbs:
yield f"grant reference_usage on database {db} to share in application package {JINJA_PACKAGE_NAME};"

# generate a view with SELECT privileges for each external table referenced by a view
for external_fqn in seen_external_tables:
(schema, view_name) = get_reference_usage_view_id(external_fqn)
Expand Down
1 change: 0 additions & 1 deletion src/snowcli/cli/appify/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"view",
"function",
"procedure",
"stage",
"streamlit",
]

Expand Down

0 comments on commit 6509f5d

Please sign in to comment.