Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add afterSQL attribute to execute a query after the JDBC trigger condition was satisfied - ideally, in the same transaction #149

Open
anna-geller opened this issue Aug 1, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@anna-geller
Copy link
Member

Feature description

To avoid never-ending loops in JDBC triggers, users typically need to modify the data in a separate task to avoid the same condition being fired twice (UPDATE or DELETE rows with the same WHERE condition from the trigger sql).

Users currently need to define an extra task:

id: snowflakeQueryTrigger
namespace: blueprint

tasks:
  - id: each
    type: io.kestra.core.tasks.flows.EachSequential
    value: "{{ trigger.rows }}"
    tasks:      
      - id: automatedProcess
        type: io.kestra.plugin.scripts.shell.Commands
        runner: PROCESS
        commands:
          - echo "{{json(taskrun.value)}}"
          - echo "Welcome to Kestra {{json(taskrun.value).FIRST_NAME }} {{json(taskrun.value).LAST_NAME }}"

  - id: update
    type: io.kestra.plugin.jdbc.snowflake.Query
    description: Update rows to avoid double trigger
    sql: |
      UPDATE KESTRA.PUBLIC.EMPLOYEES
      SET UPDATE_TIMESTAMP = SYSDATE()
      WHERE START_DATE = CURRENT_DATE();

taskDefaults:
  - type: io.kestra.plugin.jdbc.snowflake.Trigger
    values:
      url: jdbc:snowflake://your_account_id.snowflakecomputing.com?warehouse=DEMO
      username: your_username
      password: "{{ secret('SNOWFLAKE_PASSWORD') }}"
  
  - type: io.kestra.plugin.jdbc.snowflake.Query
    values:
      url: jdbc:snowflake://your_account_id.snowflakecomputing.com?warehouse=DEMO
      username: your_username
      password: "{{ secret('SNOWFLAKE_PASSWORD') }}"

triggers:
  - id: wait
    type: io.kestra.plugin.jdbc.snowflake.Trigger
    sql: "SELECT * FROM KESTRA.PUBLIC.EMPLOYEES WHERE START_DATE = CURRENT_DATE() and UPDATE_TIMESTAMP IS NULL;"
    interval: "PT1M" # check every 1 minute for the query condition
    fetch: true

It would be great to simplify such a pattern using afterSQL property (name TBD - could be postTriggerSql):

id: snowflakeQueryTrigger
namespace: blueprint

tasks:
  - id: each
    type: io.kestra.core.tasks.flows.EachSequential
    value: "{{ trigger.rows }}"
    tasks:      
      - id: automatedProcess
        type: io.kestra.plugin.scripts.shell.Commands
        runner: PROCESS
        commands:
          - echo "{{json(taskrun.value)}}"
          - echo "Welcome to Kestra {{json(taskrun.value).FIRST_NAME }} {{json(taskrun.value).LAST_NAME }}"

taskDefaults:
  - type: io.kestra.plugin.jdbc.snowflake.Trigger
    values:
      url: jdbc:snowflake://your_account_id.snowflakecomputing.com?warehouse=DEMO
      username: your_username
      password: "{{ secret('SNOWFLAKE_PASSWORD') }}"

triggers:
  - id: wait
    type: io.kestra.plugin.jdbc.snowflake.Trigger
    sql: "SELECT * FROM KESTRA.PUBLIC.EMPLOYEES WHERE START_DATE = CURRENT_DATE() and UPDATE_TIMESTAMP IS NULL;"
    afterSQL: "UPDATE KESTRA.PUBLIC.EMPLOYEES SET UPDATE_TIMESTAMP = SYSDATE() WHERE START_DATE = CURRENT_DATE();"
    interval: "PT1M" # check every 1 minute for the query condition
    fetch: true
@anna-geller anna-geller added the enhancement New feature or request label Aug 1, 2023
@anna-geller anna-geller added this to the v0.15.0 milestone Dec 4, 2023
@anna-geller anna-geller modified the milestones: v0.15.0, v0.17.0 Jan 14, 2024
@anna-geller anna-geller changed the title Add afterSQL attribute to execute a query after the JDBC trigger condition was satisfied Add afterSQL attribute to execute a query after the JDBC trigger condition was satisfied - ideally, in the same transaction Feb 16, 2024
@anna-geller anna-geller modified the milestones: v0.17.0, v0.16.0 Feb 16, 2024
@anna-geller anna-geller modified the milestones: v0.16.0, v0.18.0 Feb 29, 2024
@anna-geller anna-geller removed this from the v0.18.0 milestone Jun 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Backlog
Development

No branches or pull requests

2 participants