Skip to content

Commit

Permalink
feat: Added functionality to allow T-SQL agent job steps
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis Feather committed Jun 29, 2022
1 parent 4f11225 commit b5dd11c
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 16 deletions.
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ repos:
hooks:
- id: darglint
args: ["--docstring-style", "sphinx"]
- repo: https://github.com/Lucas-C/pre-commit-hooks-safety
rev: v1.2.4
hooks:
- id: python-safety-dependencies-check
# - repo: https://github.com/Lucas-C/pre-commit-hooks-safety
# rev: v1.2.4
# hooks:
# - id: python-safety-dependencies-check
- repo: https://github.com/commitizen-tools/commitizen
rev: v2.27.1
hooks:
Expand Down
35 changes: 24 additions & 11 deletions src/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,34 @@ def deploy_ssis(

for job_step in ssis_deployment.job.steps:

if job_step._type != "SSIS":
if job_step._type in ["SSIS", "T-SQL"]:

raise NotImplementedError(
"Only job.steps.type==SSIS are currently supported"
"""Only job.steps.type==SSIS and
job.steps.type==T-SQL are currently supported"""
)

db.agent_create_job_step_ssis(
job_name,
job_step.name,
folder_name,
project_name,
job_step.package,
environment_name,
job_step.proxy,
)
if job_step._type == "SSIS":
db.agent_create_job_step_ssis(
job_name,
job_step.name,
folder_name,
project_name,
job_step.package,
environment_name,
job_step.proxy,
job_step.retry_attempts,
job_step.retry_interval,
)

if job_step._type == "T-SQL":
db.agent_create_job_step_tsql(
job_name,
job_step.name,
job_step.sql_string,
job_step.retry_attempts,
job_step.retry_interval,
)

for job_schedule in ssis_deployment.job.schedules:
db.agent_create_job_schedule_occurs_every_n_minutes(
Expand Down
11 changes: 10 additions & 1 deletion src/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,18 @@ class Schedule:
class Step:
name: str
_type: str = field(metadata=config(field_name="type"))
package: str
package: str = ""
sql_string: str = ""
retry_attempts: int = 0
retry_interval: int = 0
proxy: typing.Optional[str] = None

def __post_init__(self):
if self._type == "SSIS" and not self.package:
raise ValueError("'package' must have a value when step type is 'SSIS'")
elif self._type == "T-SQL" and not self.sql_string:
raise ValueError("'sql_string' must have a value when step type is 'T-SQL'")


@dataclass
class Job:
Expand Down
2 changes: 2 additions & 0 deletions src/sql/agent/create_job_step.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ EXEC msdb.dbo.sp_add_jobstep
, @subsystem = $sub_system
, @command = $command
, @database_name = $database_name
, @retry_attempts = $retry_attempts
, @retry_interval = $retry_interval
;
2 changes: 2 additions & 0 deletions src/sql/agent/create_job_step_using_proxy.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ EXEC msdb.dbo.sp_add_jobstep
, @command = $command
, @database_name = $database_name
, @proxy_name = $proxy_name
, @retry_attempts = $retry_attempts,
, @retry_interval = $retry_interval
;
30 changes: 30 additions & 0 deletions src/sql/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ def agent_create_job_step_ssis(
project_name: str,
package: str,
environment_name: str,
retry_attempts: int,
retry_interval: int,
proxy_name: str = None,
):
environment_reference_id = self._execute_sql(
Expand Down Expand Up @@ -189,6 +191,8 @@ def agent_create_job_step_ssis(
"command": command,
"database_name": "master",
"proxy_name": proxy_name,
"retry_attempts": retry_attempts,
"retry_interval": retry_interval,
},
)
else:
Expand All @@ -200,11 +204,37 @@ def agent_create_job_step_ssis(
"sub_system": "SSIS",
"command": command,
"database_name": "master",
"retry_attempts": retry_attempts,
"retry_interval": retry_interval,
},
)

self._agent_reset_job_step_flow(job_name)

def agent_create_job_step_tsql(
self,
job_name: str,
step_name: str,
sql_string: str,
retry_attempts: int,
retry_interval: int,
):

self._execute_sql(
query.agent_create_job_step,
{
"job_name": job_name,
"step_name": step_name,
"sub_system": "TSQL",
"command": sql_string,
"database_name": "master",
"retry_attempts": retry_attempts,
"retry_interval": retry_interval,
},
)

self._agent_reset_job_step_flow(job_name)

def agent_create_job_schedule_occurs_every_n_minutes(
self,
job_name: str,
Expand Down
48 changes: 48 additions & 0 deletions test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,51 @@ def test_SsisDeployment_throws_an_exception_when_schedule_minutes_is_not_an_inte

with pytest.raises(ConfigurationError):
load_configuration(toml.dumps(config))

def test_SsisDeployment_throws_exception_step_type_is_ssis_sql_string_set(
self,
):
"""
Test that if job_step type is set to SSIS but a
SQL String is provided that the config fails.
"""

config = copy.deepcopy(TEST_CONFIG)
config["job"]["steps"][0]["type"] = "SSIS"
del config["job"]["steps"][0]["package"]
config["job"]["steps"][0]["sql_string"] = "SELECT 1 AS number"

with pytest.raises(ConfigurationError):
load_configuration(toml.dumps(config))

def test_SsisDeployment_throws_exception_step_type_is_tsql_package_set(
self,
):
"""
Test that if job_step type is set to T-SQL
but a package is provided that the config fails.
"""

config = copy.deepcopy(TEST_CONFIG)
config["job"]["steps"][0]["type"] = "T-SQL"

with pytest.raises(ConfigurationError):
load_configuration(toml.dumps(config))

def test_SsisDeployment_does_not_throw_exception_step_tsql_sql_string_provided(
self,
):
"""
Test that the config is valid when job step
is t-sql and sql_string is provided.
"""

config = copy.deepcopy(TEST_CONFIG)
config["job"]["steps"][0]["type"] = "T-SQL"
del config["job"]["steps"][0]["package"]
config["job"]["steps"][0]["sql_string"] = "SELECT 1 AS number"

print(config)

actual = load_configuration(toml.dumps(config))
assert_type(actual, SsisDeployment)

0 comments on commit b5dd11c

Please sign in to comment.