Skip to content

Commit

Permalink
Drop Synapse schemas (#3599)
Browse files Browse the repository at this point in the history
* Drop schemas

* Add mixin

* Lint and checks

* Lint fixes

* Lint fixes
  • Loading branch information
LizaShak authored Jul 2, 2023
1 parent 8c82fd7 commit 435fa39
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 32 deletions.
19 changes: 18 additions & 1 deletion templates/workspace_services/ohdsi/porter.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
schemaVersion: 1.0.0
name: tre-workspace-service-ohdsi
version: 0.1.94
version: 0.2.0
description: "An OHDSI workspace service"
registry: azuretre
dockerfile: Dockerfile.tmpl
Expand Down Expand Up @@ -70,6 +70,7 @@ mixins:
clientVersion: 1.4.6
- az:
clientVersion: 2.37.0
- exec

outputs:
- name: connection_uri
Expand Down Expand Up @@ -130,6 +131,14 @@ install:
- name: webapi_uri
- name: authentication_callback_uri
- name: is_exposed_externally
- exec:
description: "Init Synapse Schemas"
command: ./synapse_runner.sh
envs:
DATA_SOURCE_CONFIG: ${ bundle.parameters.data_source_config }
DATA_SOURCE_DIAMONS: ${ bundle.parameters.data_source_daimons }
TRE_RESOURCE_ID: ${ bundle.parameters.id }
SCRIPT_PATH: sql/init_synapse_schemas.sql
upgrade:
- az:
description: "Set Azure Cloud Environment"
Expand Down Expand Up @@ -183,3 +192,11 @@ uninstall:
storage_account_name: ${ bundle.parameters.tfstate_storage_account_name }
container_name: ${ bundle.parameters.tfstate_container_name }
key: tre-workspace-service-ohdsi-${ bundle.parameters.id }
- exec:
description: "Drop Synapse Schemas"
command: ./synapse_runner.sh
envs:
DATA_SOURCE_CONFIG: ${ bundle.parameters.data_source_config }
DATA_SOURCE_DIAMONS: ${ bundle.parameters.data_source_daimons }
TRE_RESOURCE_ID: ${ bundle.parameters.id }
SCRIPT_PATH: sql/drop_synapse_schemas.sql
12 changes: 0 additions & 12 deletions templates/workspace_services/ohdsi/scripts/synapse_runner.sh

This file was deleted.

30 changes: 30 additions & 0 deletions templates/workspace_services/ohdsi/sql/drop_synapse_schemas.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/****** Copy Data ******/
CREATE TABLE #tbl
WITH
( DISTRIBUTION = ROUND_ROBIN
)
AS
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS Sequence
, [name]
, 'DROP TABLE ' + N'$(RESULTS_SCHEMA_NAME)' + '.' + name AS sql_code
FROM sys.tables
WHERE schema_id = (select schema_id from sys.schemas where name = N'$(RESULTS_SCHEMA_NAME)')
;

DECLARE @nbr_statements INT = (SELECT COUNT(*) FROM #tbl)
, @i INT = 1
;

WHILE @i <= @nbr_statements
BEGIN
DECLARE @sql_code NVARCHAR(4000) = (SELECT sql_code FROM #tbl WHERE Sequence = @i);
EXEC sp_executesql @sql_code;
SET @i +=1;
END

DROP TABLE #tbl;

/****** Drop Schemas ******/

DROP SCHEMA [$(RESULTS_SCHEMA_NAME)];
DROP SCHEMA [$(TEMP_SCHEMA_NAME)];
46 changes: 46 additions & 0 deletions templates/workspace_services/ohdsi/synapse_runner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash
set -o errexit
set -o pipefail
set -o nounset

if [[ -z ${DATA_SOURCE_CONFIG:-} ]] || [[ -z ${DATA_SOURCE_DIAMONS:-} ]]; then
printf 'No data source or daimons configured.'
exit 0
else
# Parse Data source
ds_config="$(echo "$DATA_SOURCE_CONFIG" | base64 --decode)"
ds_daimons="$(echo "$DATA_SOURCE_DIAMONS" | base64 --decode)"
dialect="$(jq -r '.dialect' <<< "$ds_config")"

if [[ $dialect != "Azure Synapse" ]]; then
printf 'Not a Synapse data source, no action required.'
exit 0
fi

origin_results_schema_name="$(jq -r '.daimon_results' <<< "$ds_daimons")"
origin_temp_schema_name="$(jq -r '.daimon_temp' <<< "$ds_daimons")"
if [[ -z $origin_results_schema_name ]] || [[ -z $origin_temp_schema_name ]]; then
printf 'Results and temp schemas are not configured.'
exit 0
fi

# Parse required info
admin_user="$(jq -r '.username' <<< "$ds_config")"
jdbc_connection_string="$(jq -r '.connection_string' <<< "$ds_config")"
synapse_server="$([[ $jdbc_connection_string =~ jdbc:sqlserver://(.*):1433 ]] && echo "${BASH_REMATCH[1]}")"
synapse_db="$([[ $jdbc_connection_string =~ database=(.*)(;user) ]] && echo "${BASH_REMATCH[1]}")"
origin_results_schema_name="$(jq -r '.daimon_results' <<< "$ds_daimons")"
origin_temp_schema_name="$(jq -r '.daimon_temp' <<< "$ds_daimons")"
parsed_resource_id="$(echo "$TRE_RESOURCE_ID" | tr - _ )"
results_schema_name="${origin_results_schema_name}_${parsed_resource_id}"
temp_schema_name="${origin_temp_schema_name}_${parsed_resource_id}"

# Export password as required by sqlcmd tool
# shellcheck disable=SC2155
export SQLCMDPASSWORD="$(jq -r '.password' <<< "$ds_config")"

printf 'Execute Synapse SQL script'
sqlcmd -U "${admin_user}" -S "${synapse_server}" -d "${synapse_db}" -W -v RESULTS_SCHEMA_NAME="${results_schema_name}" -v TEMP_SCHEMA_NAME="${temp_schema_name}" -v ORIGIN_RESULTS_SCHEMA_NAME="${origin_results_schema_name}" -i "${SCRIPT_PATH}"
printf 'Execute Synapse SQL script: done.'
exit 0
fi
19 changes: 0 additions & 19 deletions templates/workspace_services/ohdsi/terraform/data_source.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,3 @@ resource "terraform_data" "add_data_source" {

depends_on = [terraform_data.deployment_atlas_security]
}

resource "terraform_data" "init_synapse_schemas" {
count = local.is_synapse_data_source && local.daimon_results != null && local.daimon_temp != null ? 1 : 0

provisioner "local-exec" {
environment = {
ADMIN_USERNAME = local.data_source_config.username
ADMIN_USER_PASSWORD = local.data_source_config.password
SCHEMA_NAME = local.short_service_id
SQL_FILE_PATH = "../sql/init_results_and_temp_schemas.sql"
SYNAPSE_SERVER = tostring(regex("(?:jdbc:sqlserver://)(.*)(:?:1433)", local.data_source_config.connection_string)[0])
SYNAPSE_DATABASE = tostring(regex("(?:database=)(.*)(:?;user)", local.data_source_config.connection_string)[0])
RESULTS_SCHEMA_NAME = local.results_schema_name
TEMP_SCHEMA_NAME = local.temp_schema_name
ORIGIN_RESULTS_SCHEMA_NAME = local.data_source_daimons.daimon_results
}
command = "../scripts/synapse_runner.sh"
}
}

0 comments on commit 435fa39

Please sign in to comment.