-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathSet-SynapseEnvironment.ps1
685 lines (583 loc) · 24.9 KB
/
Set-SynapseEnvironment.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
<#
.SYNOPSIS
Create database on Synapse SQL pool, and create default EXTERNAL TABLEs and VIEWs for customers.
.DESCRIPTION
Create database on Synapse SQL pool, and create default EXTERNAL TABLEs and VIEWs for customers.
Customers can query data on created EXTERNAL TABLEs and VIEWs on Synapse.
.PARAMETER SynapseWorkspaceName
Name of Synapse workspace instance, will create tag on it and create EXTERNAL TABLEs and VIEWs in its serverless SQL pool.
.PARAMETER Database
Default: fhirdb
Name of database to be created on Synapse serverless SQL server pool.
.PARAMETER StorageName
Name of storage where parquet FHIR data be exported to.
.PARAMETER Container
Default: fhir
Name of container on storage where parquet FHIR data be exported to.
.PARAMETER ResultPath
Default: result
Path to the parquet FHIR data.
.PARAMETER DataSourceType
Default: FHIR
Source data type, "FHIR", "DICOM".
.PARAMETER FhirVersion
Default: R4
The fhir version of Parquet data on the storage.
.PARAMETER SqlScriptCollectionPath
Default: sql/Resources
Path to the sql scripts directory to create EXTERNAL TABLEs and VIEWs.
.PARAMETER MasterKey
Default: "FhirSynapseLink0!"
Master key that will be set in created database. Database need to have master key then we can create EXTERNAL TABLEs and VIEWs on it.
.PARAMETER Concurrent
Default: 15
Max concurrent tasks number that will be used to upload placeholder files and execute SQL scripts.
.PARAMETER CustomizedSchemaImage
Customized schema image reference.
#>
[cmdletbinding()]
Param(
[parameter(Mandatory=$true)]
[string]$SynapseWorkspaceName,
[string]$Database = "fhirdb",
[parameter(Mandatory=$true)]
[string]$StorageName,
[string]$Container = "fhir",
[string]$ResultPath = "result",
[string]$DataSourceType = 'FHIR',
[string]$FhirVersion = "R4",
[string]$SqlScriptCollectionPath = "sql",
[string]$MasterKey = "FhirSynapseLink0!",
[int]$Concurrent = 15,
[string]$CustomizedSchemaImage
)
# TODO: Align Tags here and ARM template, maybe save schemas in Storage/ACR and run remotely.
$Tags = @{
"FhirAnalyticsPipeline" = "FhirToDataLake"
"FhirSchemaVersion" = "v0.6.2"
}
$JobName = "FhirSynapseJob"
$PlaceHolderName = ".readme.txt"
$CustomizedSchemaDirectoryPrefix = "CustomizedSchema_"
$OrasDirectoryPath = "Oras"
$OrasAppPath = "oras.exe"
$OrasWinUrl = "https://github.com/deislabs/oras/releases/download/v0.16.0/oras_0.16.0_windows_amd64.zip"
$ErrorActionPreference = "Stop"
$DataSourceType = $DataSourceType.ToUpper()
if ($DataSourceType -eq "FHIR")
{
$FhirVersion = $FhirVersion.ToUpper()
if ($FhirVersion -eq "R4")
{
$SqlScriptCollectionPath = Join-Path $SqlScriptCollectionPath "r4" | Join-Path -ChildPath "Resources"
}
elseif ($FhirVersion -eq "R5")
{
$SqlScriptCollectionPath = Join-Path $SqlScriptCollectionPath "r5" | Join-Path -ChildPath "Resources"
}
else
{
throw "The FHIR version '$FhirVersion' is not supported."
}
}
elseif ($DataSourceType -eq "DICOM")
{
$SqlScriptCollectionPath = Join-Path $SqlScriptCollectionPath "dicom"
}
else
{
throw "The data source type '$DataSourceType' is not supported."
}
function Start-Retryable-Job {
[CmdletBinding()]
Param(
[string]$Name, [scriptblock]$ScriptBlock, [Object[]]$ArgumentList
)
$prefixBlock = '
$retryCount = 0
$retryMax = 2
$delay = 300
do {
try
{
'
$suffixBlock = '
return
}
catch [Exception]
{
$retryCount++
if ($retryCount -le $retryMax)
{
Start-Sleep -Milliseconds $delay
}
else
{
throw
}
}
} while ($true)
'
$executeBlock = [ScriptBlock]::Create($prefixBlock + $ScriptBlock.ToString() + $suffixBlock)
Start-Job -Name $Name -ScriptBlock $executeBlock -ArgumentList $ArgumentList | Out-Null
}
function Execute_File
{
param([string]$fileName, [string[]]$argumentList)
& $fileName $argumentList
if ($LastExitCode -ne 0) {
throw "Failed for executing '$fileName $argumentList'."
}
Write-Host "Finish executing '$fileName $argumentList'." -ForegroundColor Green
}
function New-FhirDatabase
{
param([string]$serviceEndpoint, [string]$databaseName)
$sqlAccessToken = (Get-AzAccessToken -ResourceUrl https://database.windows.net).Token
try {
Invoke-Sqlcmd -ServerInstance $serviceEndpoint -Database "master" -AccessToken $sqlAccessToken `
-Query "CREATE DATABASE $databaseName" -ErrorAction Stop
}
catch {
Write-Host "Create database '$databaseName' on '$serviceEndpoint' failed: $($_.ToString())"
throw
}
}
function Remove-FhirDatabase
{
param([string]$serviceEndpoint, [string]$databaseName)
[System.Data.SqlClient.SqlConnection]::ClearAllPools()
$sqlAccessToken = (Get-AzAccessToken -ResourceUrl https://database.windows.net).Token
try {
Invoke-Sqlcmd -ServerInstance $serviceEndpoint -Database "master" -AccessToken $sqlAccessToken `
-Query "DROP DATABASE $databaseName" -ErrorAction Stop
}
catch {
Write-Host "Remove database '$databaseName' on '$serviceEndpoint' failed: $($_.ToString())"
throw
}
}
function Set-InitializeEnvironment
{
param([string]$serviceEndpoint, [string]$databaseName, [string]$masterKey, [string]$storageName, [string]$container, [string]$resultPath, [string]$dataSourceType)
$locationPath = "https://$storageName.blob.core.windows.net/$container/$resultPath"
$schema = $dataSourceType.ToLower()
$initializeEnvironmentSql = "
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '$masterKey';
CREATE DATABASE SCOPED CREDENTIAL SynapseIdentity
WITH IDENTITY = 'Managed Identity';
CREATE EXTERNAL DATA SOURCE ParquetSource WITH (
LOCATION = '$locationPath',
CREDENTIAL = SynapseIdentity
);
CREATE EXTERNAL FILE FORMAT ParquetFormat WITH ( FORMAT_TYPE = PARQUET );
GO
CREATE SCHEMA $schema;
GO
USE [master]"
$sqlAccessToken = (Get-AzAccessToken -ResourceUrl https://database.windows.net).Token
try {
Invoke-Sqlcmd -ServerInstance $serviceEndpoint -Database $databaseName -AccessToken $sqlAccessToken `
-Query $initializeEnvironmentSql -ErrorAction Stop
}
catch {
Write-Host "Initialize environment on '$databaseName' of '$serviceEndpoint' failed: $($_.ToString())"
throw
}
}
function New-ContainerIfNotExists
{
param([string]$storageName, [string]$containerName)
$storageContext = New-AzStorageContext -StorageAccountName $storageName -UseConnectedAccount -ErrorAction stop
if(Get-AzStorageContainer -Name $containerName -Context $storageContext -ErrorAction SilentlyContinue) {
Write-Host "Container '$containerName' already exists." -ForegroundColor Green
}
else{
Write-Host "Create container '$containerName'." -ForegroundColor Green
New-AzStorageContainer -Name $containerName -Context $storageContext -ErrorAction Stop
}
}
function New-PlaceHolderBlobs
{
param([string]$storageName, [string]$container, [string]$resultPath, [string[]]$schemaTypes)
$storageContext = New-AzStorageContext -StorageAccountName $storageName -UseConnectedAccount -ErrorAction stop
$expiryTime = (Get-Date).AddMinutes(30)
$sasToken = New-AzStorageContainerSASToken -Name $container -Permission rwd -ExpiryTime $expiryTime -context $storageContext -ErrorAction stop
foreach ($schemaType in $schemaTypes) {
$blobName = "$resultPath/$schemaType/.readme.txt"
$jobs = @(Get-Job -Name $JobName -ErrorAction Ignore)
if ($jobs.Count -ge $Concurrent) {
$finishedJob = (Get-Job -Name $JobName | Wait-Job -Any)
if (($finishedJob.State -eq 'Failed') -or ($finishedJob.ChildJobs[0].State -eq 'Failed')) {
Write-Host "$($finishedJob.ChildJobs[0].JobStateInfo.Reason.Message)" -ForegroundColor Red
Get-Job -Name $JobName | Wait-Job | Remove-Job | Out-Null
throw "Creating placeholder blob failed: $($finishedJob.ChildJobs[0].JobStateInfo.Reason.Message)"
}
else {
Write-Host $finishedJob.ChildJobs[0].Information[0].MessageData.Message
Remove-Job -Job $finishedJob
}
}
# Create placeholder blobs
Write-Host " -> Begin to create placeholder blob '$blobName'."
Start-Retryable-Job -Name $JobName -ScriptBlock{
$storageContext = New-AzStorageContext -StorageAccountName $args[3] -SasToken $args[4] -ErrorAction stop
Set-AzStorageBlobContent `
-File $args[0]`
-Container $args[1] `
-Blob $args[2] `
-Context $storageContext `
-Force `
-ErrorAction stop
Write-Host " -> Finished creating placeholder blob '$($args[2])'."
} -ArgumentList "$(Get-Location)/$PlaceHolderName", $container, $blobName, $storageName, $sasToken
}
foreach ($finishedJob in (Get-Job -Name $JobName | Wait-Job)) {
if (($finishedJob.State -eq 'Failed') -or ($finishedJob.ChildJobs[0].State -eq 'Failed')) {
Write-Host "$($finishedJob.ChildJobs[0].JobStateInfo.Reason.Message)" -ForegroundColor Red
Get-Job -Name $JobName | Wait-Job | Remove-Job | Out-Null
throw "Creating placeholder blob failed: $($finishedJob.ChildJobs[0].JobStateInfo.Reason.Message)"
}
else {
Write-Host $finishedJob.ChildJobs[0].Information[0].MessageData.Message
Remove-Job -Job $finishedJob
}
}
}
function New-TableAndViewsForResources
{
param([string]$serviceEndpoint, [string]$databaseName, [string]$masterKey, [string]$storageName, [string]$container)
$files = Get-ChildItem $SqlScriptCollectionPath -Filter "*.sql"
$sqlAccessToken = (Get-AzAccessToken -ResourceUrl https://database.windows.net).Token
Write-Host "Start to create default TABLEs and VIEWs on '$databaseName' of '$serviceEndpoint'" -ForegroundColor Green
foreach ($file in $files) {
$jobs = @(Get-Job -Name $JobName -ErrorAction Ignore)
if ($jobs.Count -ge $Concurrent) {
$finishedJob = (Get-Job -Name $JobName | Wait-Job -Any)
if (($finishedJob.State -eq 'Failed') -or ($finishedJob.ChildJobs[0].State -eq 'Failed')) {
Write-Host "$($finishedJob.ChildJobs[0].JobStateInfo.Reason.Message)" -ForegroundColor Red
Get-Job -Name $JobName | Wait-Job | Remove-Job | Out-Null
throw "Creating Table and Views job failed: $($finishedJob.ChildJobs[0].JobStateInfo.Reason.Message)"
}
else {
Write-Host $finishedJob.ChildJobs[0].Information[0].MessageData.Message
Remove-Job -Job $finishedJob
}
}
$filePath = $file.FullName
# Create TABLES and VIEWs for resouces
Write-Host " -> Begin to execute script '$filePath'"
Start-Job -Name $JobName -ScriptBlock{
Invoke-Sqlcmd `
-ServerInstance $args[0] `
-Database $args[1] `
-AccessToken $args[2] `
-InputFile $args[3] `
-ConnectionTimeout 120 `
-ErrorAction Stop
Write-Host " -> Finished executing script '$($args[3])'"
} -ArgumentList $serviceEndpoint, $databaseName, $sqlAccessToken, $filePath | Out-Null
}
foreach ($finishedJob in (Get-Job -Name $JobName | Wait-Job)) {
if (($finishedJob.State -eq 'Failed') -or ($finishedJob.ChildJobs[0].State -eq 'Failed')) {
Write-Host "$($finishedJob.ChildJobs[0].JobStateInfo.Reason.Message)" -ForegroundColor Red
Get-Job -Name $JobName | Wait-Job | Remove-Job | Out-Null
throw "Creating Table and Views job failed: $($finishedJob.ChildJobs[0].JobStateInfo.Reason.Message)"
}
else {
Write-Host $finishedJob.ChildJobs[0].Information[0].MessageData.Message
Remove-Job -Job $finishedJob
}
}
}
function Get-OrasExeApp {
param ([string]$orasDirectoryPath, [string]$orasAppPath, [string]$orasUrl)
$orasGzFile = "oras.tar.gz"
# Check if oras.exe have already been downloaded
if ((test-Path -Path $orasAppPath))
{
Write-Host "Oras application have already exist" -ForegroundColor Green
return
}
Write-Host "Start to download oras application from $orasUrl" -ForegroundColor Green
try {
Invoke-WebRequest $orasUrl -OutFile $orasGzFile -ErrorAction Stop
}
catch {
Write-Host "Download oras application from $orasUrl failed: $($_.ToString())" -BackgroundColor Red
throw
}
# Create Oras directory for unpacking Oras artifact
if (!(test-path -path $orasDirectoryPath))
{
new-item -path $orasDirectoryPath -Itemtype Directory | Out-Null
}
$unpackParameters = @(
'-xvf'
$orasGzFile
'-C'
$orasDirectoryPath
)
Execute_File -fileName 'tar' -argumentList $unpackParameters
Copy-Item "$orasDirectoryPath/$orasAppPath" -Destination "."
Remove-Item $orasGzFile
Write-Host "Finish download oras application from $orasUrl" -ForegroundColor Green
}
function Get-ImageDigest {
param ([string]$orasAppPath, [string]$schemaImageReference)
$registryName = $schemaImageReference.Substring(0, $schemaImageReference.IndexOf('.azurecr.io'))
Connect-AzContainerRegistry -Name $registryName -ErrorAction stop | Out-Null
$orasParameters = @(
'manifest'
"fetch"
'--descriptor'
$schemaImageReference
)
$descriptor = & "./$orasAppPath" $orasParameters
$digest = ($descriptor | ConvertFrom-Json).digest
Write-Information "Got image digest $digest from $schemaImageReference" -InformationAction Continue
return $digest
}
function Get-CustomizedSchemaImage {
param ([string]$orasAppPath, [string]$schemaImageReference, [string]$customizedSchemaDirectory, [string]$templateDirectory)
if (!(Test-Path $customizedSchemaDirectory)){
New-Item $customizedSchemaDirectory -ItemType Directory
}
$registryName = $schemaImageReference.Substring(0, $schemaImageReference.IndexOf('.azurecr.io'))
Connect-AzContainerRegistry -Name $registryName -ErrorAction stop
# Leverage the oras to pull the image from Container Registry.
$orasParameters = @(
'pull'
$schemaImageReference
'-o'
$customizedSchemaDirectory
)
Execute_File -fileName "./$orasAppPath" -argumentList $orasParameters
$compressPackages = Get-ChildItem -Path (Join-Path $customizedSchemaDirectory '*') -Include '*.tar.gz' | % { $_.FullName }
Write-Host "Successfully pull the customized schema image: $compressPackages" -ForegroundColor Green
# Unpack the image compressed package to customized template directory
if (!(Test-Path $templateDirectory)){
New-Item $templateDirectory -ItemType Directory
}
foreach ($compressPackage in $compressPackages){
$unpackParameters = @(
'-xvf'
$compressPackage
'-C'
$templateDirectory
)
Execute_File -fileName 'tar' -argumentList $unpackParameters
}
}
function Get-CustomizedSchemaType {
param ([string]$resourceType)
return "$($resourceType)_Customized"
}
function Get-CustomizedTableSql {
param ([string]$schemaType, [PSCustomObject]$schemaObject, [string]$dataSourceType)
$customizedTableProperties = ""
foreach ($property in $schemaObject.properties.psobject.properties){
$sqlType = switch($property.Value.type)
{
'number' { 'float' ; Break }
'integer' { 'bigint' ; Break }
'boolean' { 'bit' ; Break }
'string' { 'NVARCHAR(4000)' ; Break }
Default {
Write-Host "Invalid property type in '$schemaType.$($property.Name)': $($property.Value.type)"
throw
}
}
$customizedTableProperties += " [$($property.Name)] $sqlType,"
}
$dataSource = $dataSourceType.ToLower()
$createCustomizedTableSql = "CREATE EXTERNAL TABLE [$dataSource].[$schemaType] (
$customizedTableProperties
) WITH (
LOCATION='/$schemaType/**',
DATA_SOURCE = ParquetSource,
FILE_FORMAT = ParquetFormat
);"
return $createCustomizedTableSql
}
function New-CustomizedTables
{
param([string]$serviceEndpoint, [string]$databaseName, [string]$masterKey, [string]$schemaDirectory, [string]$dataSourceType)
$schemaFiles = Get-ChildItem -Path $(Join-Path -Path $schemaDirectory -ChildPath *) -Include '*.schema.json' -Name
$sqlAccessToken = (Get-AzAccessToken -ResourceUrl https://database.windows.net).Token
Write-Host "Start to create customized Tables on '$databaseName' of '$serviceEndpoint'" -ForegroundColor Green
foreach ($schemaFile in $schemaFiles){
$jobs = @(Get-Job -Name $JobName -ErrorAction Ignore)
if ($jobs.Count -ge $Concurrent) {
$finishedJob = (Get-Job -Name $JobName | Wait-Job -Any)
if (($finishedJob.State -eq 'Failed') -or ($finishedJob.ChildJobs[0].State -eq 'Failed')) {
Write-Host "$($finishedJob.ChildJobs[0].JobStateInfo.Reason.Message)" -ForegroundColor Red
Get-Job -Name $JobName | Wait-Job | Remove-Job | Out-Null
throw "Create customized Table failed: $($finishedJob.ChildJobs[0].JobStateInfo.Reason.Message)"
}
else {
Write-Host $finishedJob.ChildJobs[0].Information[0].MessageData.Message
Remove-Job -Job $finishedJob
}
}
$schemaFilePath = Join-Path -Path $schemaDirectory -ChildPath $schemaFile
$schemaObject = Get-Content $schemaFilePath | Out-String | ConvertFrom-Json -ErrorAction stop
$resourceType = $schemaFile.Substring(0, $schemaFile.IndexOf('.schema.json'))
$schemaType = Get-CustomizedSchemaType -resourceType $resourceType
$sql = Get-CustomizedTableSql -schemaType $schemaType -schemaObject $schemaObject -dataSourceType $dataSourceType -ErrorAction stop
Write-Host "Begin to create customized table for $schemaType" -ForegroundColor Green
Start-Job -Name $JobName -ScriptBlock{
Invoke-Sqlcmd `
-ServerInstance $args[0] `
-Database $args[1] `
-AccessToken $args[2] `
-Query $args[3] `
-ConnectionTimeout 120 `
-ErrorAction Stop
Write-Host "Finished creating customized table for $($args[4])"
} -ArgumentList $serviceEndpoint, $databaseName, $sqlAccessToken, $sql, $schemaType | Out-Null
}
foreach ($finishedJob in (Get-Job -Name $JobName | Wait-Job)) {
if (($finishedJob.State -eq 'Failed') -or ($finishedJob.ChildJobs[0].State -eq 'Failed')) {
Write-Host "$($finishedJob.ChildJobs[0].JobStateInfo.Reason.Message)" -ForegroundColor Red
Get-Job -Name $JobName | Wait-Job | Remove-Job | Out-Null
throw "Create customized Table failed: $($finishedJob.ChildJobs[0].JobStateInfo.Reason.Message)"
}
else {
Write-Host $finishedJob.ChildJobs[0].Information[0].MessageData.Message
Remove-Job -Job $finishedJob
}
}
}
$stopwatch = [system.diagnostics.stopwatch]::StartNew()
Get-Job -Name $JobName -ErrorAction Ignore | Remove-Job | Out-Null
###
# Try get Synapse instance.
###
try {
$synapse = Get-AzSynapseWorkspace -Name $SynapseWorkspaceName -ErrorAction Stop
}
catch {
Write-Host "Get Synapse instance '$synapseWorkspaceName' failed: $($_.ToString())."
throw
}
# Get synapse serverless SQL server endpoint
$synapseSqlServerEndpoint = $synapse.ConnectivityEndpoints.sqlOnDemand
# Test connection to Synapse SQL server.
try {
$sqlAccessToken = (Get-AzAccessToken -ResourceUrl https://database.windows.net).Token
$dbId = Invoke-Sqlcmd -ServerInstance $synapseSqlServerEndpoint -Database "master" -AccessToken $sqlAccessToken `
-Query "SELECT DB_ID('$Database')" -ErrorAction Stop
}
catch {
Write-Host "Failed to connect to '$synapseSqlServerEndpoint': $($_.ToString())."
throw
}
# Throw exception if database already exists.
if ([string]$dbId.Column1)
{
throw "Database '$Database' already exist, please use another database name or drop it."
}
# Create tag on Synapse
Update-AzTag -ResourceId $synapse.Id -Tag $Tags -Operation "Merge" | Out-Null
Write-Host "Created Tags on Synapse '$synapseWorkspaceName'." -ForegroundColor Green
###
# 1.Create container on Storage if not exists.
###
try {
New-ContainerIfNotExists `
-storageName $StorageName `
-containerName $Container
}
catch {
Write-Host "Create container '$Container' on '$StorageName' failed: $($_.ToString())."
throw
}
###
# 2.Create Tables and Views for default schema data.
###
# a). Create placeholder blobs for default schema data.
try{
$sqlFiles = Get-ChildItem $SqlScriptCollectionPath -Filter "*.sql" -Name
$defaultSchemaTypes = $sqlFiles | ForEach-Object {$($_ -split "\.")[0]}
New-PlaceHolderBlobs -storage $StorageName -container $Container -resultPath $ResultPath -schemaTypes $defaultSchemaTypes
}
catch
{
Write-Host "Create placeholder blobs for default schema data failed: $($_.ToString())."
throw
}
# b). Create database on SQL pool.
New-FhirDatabase `
-databaseName $Database `
-serviceEndpoint $synapseSqlServerEndpoint
# Try to create TABLEs and VIEWs for all resource types.
# And will try to drop database if failed to create TABLEs and VIEWs.
try{
# c). Initialize database environment.
Set-InitializeEnvironment `
-serviceEndpoint $synapseSqlServerEndpoint `
-databaseName $Database `
-masterKey $MasterKey `
-storage $StorageName `
-container $Container `
-resultPath $ResultPath `
-dataSourceType $DataSourceType
# d). Create TABLEs and VIEWs on Synapse.
New-TableAndViewsForResources `
-serviceEndpoint $synapseSqlServerEndpoint `
-databaseName $Database `
-masterKey $MasterKey `
-storage $StorageName `
-container $Container
}
catch{
Write-Host "Create TABLEs and VIEWs for default schema data failed, will try to drop database '$Database'." -ForegroundColor Red
Remove-FhirDatabase -serviceEndpoint $synapseSqlServerEndpoint -databaseName $Database
throw
}
###
# 3. Create Tables for customized schema data.
###
if ($CustomizedSchemaImage) {
Write-Host "Create Tables for customized schema data, use schema image from $($CustomizedSchemaImage)." -ForegroundColor Green
try {
# a). Download oras application if it not exist.
Get-OrasExeApp `
-orasDirectoryPath $OrasDirectoryPath `
-orasAppPath $OrasAppPath `
-orasUrl $OrasWinUrl
$digest = Get-ImageDigest `
-orasAppPath $OrasAppPath `
-schemaImageReference $CustomizedSchemaImage
$customizedSchemaDirectory = $CustomizedSchemaDirectoryPrefix + $digest.Substring($digest.Length - 10)
$templateDirectory = Join-Path -Path $customizedSchemaDirectory -ChildPath "template"
$schemaDirectory = Join-Path -Path $templateDirectory -ChildPath "Schema"
# b). Pull and parse customized schema from Container Registry.
Get-CustomizedSchemaImage `
-orasAppPath $OrasAppPath `
-schemaImageReference $CustomizedSchemaImage `
-customizedSchemaDirectory $customizedSchemaDirectory `
-templateDirectory $templateDirectory
# c). Create placeholder blobs for customized schema data.
$sqlFiles = Get-ChildItem $schemaDirectory -Filter "*.schema.json" -Name
$customizedSchemaTypes = $sqlFiles | ForEach-Object { Get-CustomizedSchemaType -resourceType $($_ -split "\.")[0] }
New-PlaceHolderBlobs -storage $StorageName -container $Container -resultPath $ResultPath -schemaTypes $customizedSchemaTypes
# d). Create customized TABLEs Synapse.
New-CustomizedTables `
-serviceEndpoint $synapseSqlServerEndpoint `
-databaseName $Database `
-masterKey $MasterKey `
-schemaDirectory $schemaDirectory `
-dataSourceType $DataSourceType
}
catch{
Write-Host "Create TABLEs for customized schema data failed, will try to drop database '$Database'." -ForegroundColor Red
Remove-FhirDatabase -serviceEndpoint $synapseSqlServerEndpoint -databaseName $Database
throw
}
}
$stopwatch.Stop()
$time = $stopwatch.Elapsed.TotalSeconds
Write-Host "Create Synapse environment finished with $time seconds." -ForegroundColor Green
[System.Data.SqlClient.SqlConnection]::ClearAllPools()