Skip to content

Commit

Permalink
Add is_ordered for SEQUENCE; Fix REFRESH_ON_CREATE for ALTER STAGE
Browse files Browse the repository at this point in the history
  • Loading branch information
littleK0i committed Jan 16, 2024
1 parent dd02600 commit 3c8ff84
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 15 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [0.23.0] - 2024-01-16

- Added remaining parameters for `TASK`.
- Added `is_ordered` for `SEQUENCE`.
- Added converter for `TASKS` (thanks to Osborne Hardison).
- Adjusted converter for `TABLE` to ignore event tables.
- Fixed issue with ALTER for `STAGE` objects trying to apply `REFRESH_ON_CREATE` to existing objects, which is not allowed.

## [0.22.1] - 2024-01-06

- Added `error_notification` for `PIPE`.
Expand Down
1 change: 1 addition & 0 deletions snowddl/blueprint/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ class StageFileBlueprint(SchemaObjectBlueprint):
class SequenceBlueprint(SchemaObjectBlueprint):
start: int
interval: int
is_ordered: Optional[bool] = None


class StreamBlueprint(SchemaObjectBlueprint):
Expand Down
4 changes: 4 additions & 0 deletions snowddl/parser/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"interval": {
"type": "integer"
},
"is_ordered": {
"type": "boolean"
},
"comment": {
"type": "string"
}
Expand All @@ -30,6 +33,7 @@ def process_sequence(self, f: ParsedFile):
full_name=SchemaObjectIdent(self.env_prefix, f.database, f.schema, f.name),
start=f.params.get("start", 1),
interval=f.params.get("interval", 1),
is_ordered=f.params.get("is_ordered"),
comment=f.params.get("comment"),
)

Expand Down
18 changes: 18 additions & 0 deletions snowddl/resolver/sequence.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from snowddl.blueprint import SequenceBlueprint
from snowddl.error import SnowDDLUnsupportedError
from snowddl.resolver.abc_schema_object_resolver import AbstractSchemaObjectResolver, ResolveResult, ObjectType


Expand All @@ -25,6 +26,7 @@ def get_existing_objects_in_schema(self, schema: dict):
"owner": r["owner"],
"next_value": r["next_value"],
"interval": r["interval"],
"ordered": bool(r["ordered"] == "Y"),
"comment": r["comment"] if r["comment"] else None,
}

Expand Down Expand Up @@ -57,6 +59,11 @@ def create_object(self, bp: SequenceBlueprint):
},
)

if bp.is_ordered is True:
query.append_nl("ORDER")
elif bp.is_ordered is False:
query.append_nl("NOORDER")

if bp.comment:
query.append_nl(
"COMMENT = {comment}",
Expand All @@ -83,6 +90,17 @@ def compare_object(self, bp: SequenceBlueprint, row: dict):

result = ResolveResult.ALTER

if bp.is_ordered is not None and bp.is_ordered != row["ordered"]:
if bp.is_ordered is False:
self.engine.execute_safe_ddl(
"ALTER SEQUENCE {full_name:i} SET NOORDER",
{
"full_name": bp.full_name,
},
)
else:
raise SnowDDLUnsupportedError("Cannot change NOORDER sequence to ORDER")

if bp.comment != row["comment"]:
self.engine.execute_safe_ddl(
"ALTER SEQUENCE {full_name:i} SET COMMENT = {comment}",
Expand Down
33 changes: 20 additions & 13 deletions snowddl/resolver/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def _compare_directory(self, bp: StageBlueprint, existing_properties):
},
)

query.append_nl(self._build_directory(bp))
query.append_nl(self._build_directory(bp, existing_properties))
self.engine.execute_safe_ddl(query)

return is_alter_required
Expand Down Expand Up @@ -323,22 +323,29 @@ def _refresh_encryption(self, bp: StageBlueprint):

return False

def _build_directory(self, bp: StageBlueprint):
def _build_directory(self, bp: StageBlueprint, existing_properties=None):
query = self.engine.query_builder()

if bp.directory:
query.append("DIRECTORY = (")
adjusted_directory = bp.directory

for k, v in bp.directory.items():
query.append(
"{param_name:r} = {param_value:dp}",
{
"param_name": k,
"param_value": v,
},
)

query.append(")")
# REFRESH_ON_CREATE is only used for CREATE command, but is not available for ALTER, must be skipped
if existing_properties and "REFRESH_ON_CREATE" in adjusted_directory:
del adjusted_directory["REFRESH_ON_CREATE"]

if adjusted_directory:
query.append("DIRECTORY = (")

for k, v in adjusted_directory.items():
query.append(
"{param_name:r} = {param_value:dp}",
{
"param_name": k,
"param_value": v,
},
)

query.append(")")
else:
query.append("DIRECTORY = ( ENABLE = FALSE )")

Expand Down
2 changes: 1 addition & 1 deletion snowddl/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.22.1"
__version__ = "0.23.0"
1 change: 1 addition & 0 deletions test/_config/step1/db1/sc1/sequence/sq002_sq1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
is_ordered: true
1 change: 1 addition & 0 deletions test/_config/step2/db1/sc1/sequence/sq002_sq1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
is_ordered: false
16 changes: 16 additions & 0 deletions test/sequence/sq002.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def test_step1(helper):
sequence = helper.show_sequence("db1", "sc1", "sq002_sq1")

assert sequence["ordered"] == "Y"


def test_step2(helper):
sequence = helper.show_sequence("db1", "sc1", "sq002_sq1")

assert sequence["ordered"] == "N"


def test_step3(helper):
sequence = helper.show_sequence("db1", "sc1", "sq002_sq1")

assert sequence is None
2 changes: 1 addition & 1 deletion test/task/ts001.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
def test_step1(helper):
show = helper.show_task("db1", "sc1", "ts001_ts1")

assert str(show["warehouse"]).startswith(".TS001_WH1")
assert str(show["warehouse"]).endswith("TS001_WH1")
assert show["schedule"] == "60 minutes"
assert show["predecessors"] == "[]"
assert show["state"] == "suspended"
Expand Down

0 comments on commit 3c8ff84

Please sign in to comment.