Skip to content
This repository was archived by the owner on Aug 19, 2024. It is now read-only.

Prod #38

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open

Prod #38

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 28 additions & 18 deletions codefresh/CI/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,35 +139,45 @@ steps:
any:
isMaster: "'${{CF_BRANCH}}' == 'master'"
isApproved: 'includes("${{STAGING_APPROVED}}", "{{STAGING_APPROVED}}") == false'

approve_prod:
type: pending-approval
title: Approve Before Production
description: Step description
timeout:
timeUnit: 'minutes'
duration: 15
finalState: denied
fail_fast: false
stage: wait-before-prod

when:
condition:
all:
isMaster: "'${{CF_BRANCH}}' == 'master'"
myCondition: deploy_staging.result == 'success'

production:
type: parallel
title: Deploy production
stage: production
steps:
production_argo:
production_use1:
title: Deploy production use1
type: codefresh-run
title: 'Deploy production using ArgoCD'
arguments:
PIPELINE_ID: infra-core/argocd
TRIGGER_ID: production-bigbrain
BRANCH: master
PIPELINE_ID: redash/CD
TRIGGER_ID: run-prod-use1
BRANCH: '${{CF_BRANCH}}'
SHA: '${{CF_REVISION}}'
ENABLE_NOTIFICATIONS: true
FOLLOW_LOGS: false
VARIABLE:
- REPO_NAME=${{CF_REPO_NAME}}
- REVISION=${{CF_REVISION}}
- BUILD_ID=${{CF_BUILD_ID}}
- APP_TYPE="infra"
- CHECK_LOCK="false"
when:
condition:
all:
isMaster: "'${{CF_BRANCH}}' == 'master'"
myCondition: deploy_staging.result == 'success'
isProd: '"${{TRIGGER_TYPE}}" == "prod"'


steps:
- name: approve_prod
'on':
- approved

push_tag:
title: Push latest docker tag
type: push
Expand Down
39 changes: 36 additions & 3 deletions redash/query_runner/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
BaseSQLQueryRunner,
register,
)
from redash.utils import json_dumps
from redash.utils import json_dumps, json_loads
import re

TYPES_MAP = {
0: TYPE_INTEGER,
Expand All @@ -33,7 +34,35 @@
}


class Snowflake(BaseSQLQueryRunner):
def _query_restrictions(query):
if query.find("/*laspha*/") > 0:
return True, None
query_without_comments = ''
for line in query.split('\n'):
line = line.strip()
if line.find('--') != -1:
line = line[:line.find('--')]
query_without_comments += ' ' + line # creates one line query
query = ' ' + query_without_comments.lower() + ' '
# replace multiple spaces with one space
query = re.sub(' +', ' ', query)
# remove /* */ comments
query = re.sub('\/\*.*\*\/', '', query)
# get rid of prefix like bigbrain. or final.
query = re.sub('bigbrain.', '', re.sub('final.', '', re.sub('raw.', '', query)))
occurrences = re.findall(" from events ", query) + re.findall(" join events ", query)
# print("num of occurrences : ", len(occurrences))
if len(occurrences) > 1:
return False, f'Querying events table multiple times is forbidden.The query contains {len(occurrences)} occurrences of the table events. '

if occurrences:
if query.find("created_at") + query.find("ingestion_time") == -2:
return False, 'Querying events table should always be with time constraint (by created_at for ' \
'FINAL.events & ingestion_time for RAW.events) '
return True, None


class Snowflake(BaseQueryRunner):
noop_query = "SELECT 1"

@classmethod
Expand Down Expand Up @@ -126,14 +155,18 @@ def _parse_results(self, cursor):
def run_query(self, query, user, query_id=None):
connection = self._get_connection()
cursor = connection.cursor()
passed, error = _query_restrictions(query)

if not passed:
return None, error

try:
cursor.execute("USE WAREHOUSE {}".format(self.configuration["warehouse"]))
cursor.execute("USE {}".format(self.configuration["database"]))

user_id = "redash" if user is None else user.email
query_id = str(query_id) if query_id else ''
query += "-- REDASH USER: " + user_id + " QUERY ID: " + query_id
query += '-- {"REDASH USER": "' + user_id + '" , "QUERY ID": "' + query_id + '"}'

cursor.execute(query)

Expand Down
5 changes: 4 additions & 1 deletion redash/tasks/queries/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ def run(self):
annotated_query = self._annotate_query(query_runner)

try:
data, error = query_runner.run_query(annotated_query, self.user, self.query_id)
if self.data_source.type.lower() == "snowflake":
data, error = query_runner.run_query(annotated_query, self.user, self.query_id)
else:
data, error = query_runner.run_query(annotated_query, self.user)
except Exception as e:
if isinstance(e, JobTimeoutException):
error = TIMEOUT_MESSAGE
Expand Down
Loading