BAG sync working scripts

This commit is contained in:
2025-03-19 14:54:37 +01:00
parent fce8651a01
commit 03dfeb47e1
4 changed files with 411 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
/*
12.03.2025, TSC
*/
USE [dba_reporting]
DECLARE @q NVARCHAR(MAX)='';
SELECT @q = @q + 'DELETE FROM '+SCHEMA_NAME(t.[schema_id])+'.'+t.[name]+';'+CHAR(13)+CHAR(10)
--SELECT name, [t].[schema_id], 'DELETE FROM '+SCHEMA_NAME(t.[schema_id])+'.'+t.[name]+';'+CHAR(13)+CHAR(10) AS cmd
FROM sys.[tables] t
WHERE t.[name] LIKE 'sym_%';
PRINT @q
--EXEC(@q)

View File

@@ -0,0 +1,204 @@
# Variables
$tenantId = "7844775a-a9cc-4c33-a5ae-36dcf6660f45" #Galenica
$clientId = "d28076dd-2108-4718-802e-cd3c35fd5505" #pcpl-BAGSpezListePrd-DBBackup
$supersetToCopy="product_superset"
$supersetToCopy="Artikel_History_Superset"
$skipBackup = "auto" #auto = check age and skip if backup is less than 1 day old, $true to force skip backup, $false to force backup
<#
backup product_superset__0 : 00:13:00
restore product_superset : 00:05:40
#>
$mod = Get-Module -Name Az.Accounts
if($null -eq $mod){
Install-Module -Name Az.KeyVault -Scope CurrentUser
}
$mod = Get-Module -Name Az.Storage
if($null -eq $mod){
Install-Module -Name Az.Storage -Scope CurrentUser
}
$mod = Get-Module -Name sqlserver
if($null -eq $mod){
Install-Module -Name sqlserver -Scope CurrentUser -AllowClobber
}
##do not alter below
$serverInstance = "SWMDATASQLPRD01.centralinfra.net"
$databaseName = $null
$storageAccountName = "stbagspezlisteprdsql"
$containerName = "sqlbakfiles"
$resourceGroupName="rg-BAGSpezListePrd-SQL"
$vaultName="kv-BAGSpezListePrd-bkp"
$secretName="superuser"
$blobPermissions="rwd"
$expiryTime = (Get-Date).AddHours(1) # SAS token valid for 1 hour
$backupFileName = "$supersetToCopy.bak"
#Managed instance related
$MILogin='superuser'
$MIPwd=$null #fetched from key vault later
$MIInstance="sqlmi-bagspezlisteprd-sqlinstance.75ff9425ac13.database.windows.net"
#check for the origin of product_superset
$query="
SELECT s.name AS superset, d.name AS srcDb
FROM sys.databases s
JOIN sys.databases d ON d.[database_id] = s.[source_database_id]
WHERE s.name='$supersetToCopy'
"
$res = Invoke-Sqlcmd -ServerInstance $serverInstance -Query $query
$databaseName = $res.srcDb
if($null -eq $databaseName){
#superset is not a db snapshot...
$databaseName = $supersetToCopy
}
# Log in to Azure
$null = Connect-AzAccount -ServicePrincipal -ApplicationId $clientId -Tenant $tenantId -Subscription BAGSpezListePrd -CertificateThumbprint 7bd45f67999015c7742db25efc86bae97590c57d
# Get the storage account context
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName
$context = $storageAccount.Context
# Get the blob properties
$backupFile = "https://$storageAccountName.blob.core.windows.net/$containerName/$backupFileName"
if($skipBackup -eq "auto")
{
try{
$blob = Get-AzStorageBlob -Container $containerName -Blob $backupFileName -Context $context
}
catch{
$blob = $null
}
if($null -eq $blob){
$skipBackup=$false
Write-Output "Blob does not exists, forcing backup"
}
else{
# Calculate the age of the blob
$currentDate = Get-Date
$blobLastModified = $blob | Select-Object -ExpandProperty LastModified
$blobLastModifiedDateTime = [DateTime]::Parse($blobLastModified.ToString())
$blobAge = $currentDate - $blobLastModifiedDateTime
if($blobAge.Days -eq 0){
$skipBackup=$true
Write-Output "Blob exists and is less than a day old, skipping backup"
}
else{
$skipBackup=$false
Write-Output "Blob exists and is older than a day, forcing backup"
}
}
}
# Generate the SAS token
$sasToken = New-AzStorageBlobSASToken -Context $context -Container $containerName -Blob $backupFileName -Permission $blobPermissions -ExpiryTime $expiryTime
# Save token in db
$sqlQuery = "
IF NOT EXISTS (SELECT * FROM sys.credentials WHERE name = 'https://$storageAccountName.blob.core.windows.net/$containerName')
BEGIN
CREATE CREDENTIAL [https://$storageAccountName.blob.core.windows.net/$containerName]
WITH IDENTITY = 'SHARED ACCESS SIGNATURE', SECRET = '$sasToken';
END
ELSE
BEGIN
ALTER CREDENTIAL [https://$storageAccountName.blob.core.windows.net/$containerName]
WITH IDENTITY = 'SHARED ACCESS SIGNATURE', SECRET = '$sasToken';
END
"
Invoke-Sqlcmd -ServerInstance $serverInstance -Query $sqlQuery
Write-Output "Credential refreshed on $serverInstance"
#Back Up Database
$url=$backupFile
$sqlQuery = "
BACKUP DATABASE [$databaseName]
TO URL = N'$url'
WITH FORMAT, MEDIANAME = 'SQLServerBackups', NAME = 'Full Backup of $databaseName';
"
if($false -eq $skipBackup){
Invoke-Sqlcmd -ServerInstance $serverInstance -Query $sqlQuery
Write-Output "backup done"
}
else{
write-output "Backup skipped."
}
#fetch the Managed Instance password
$MIPwd = Get-AzKeyVaultSecret -VaultName $vaultName -Name $secretName -AsPlainText
#craft credential for MI connection
$securePassword = ConvertTo-SecureString $MIPwd -AsPlainText -Force
$credentialMI = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $MILogin, $securePassword
#add sas token
$sqlCred="
IF NOT EXISTS (SELECT * FROM sys.credentials WHERE name = 'https://$storageAccountName.blob.core.windows.net/$containerName')
BEGIN
CREATE CREDENTIAL [https://$storageAccountName.blob.core.windows.net/$containerName]
WITH IDENTITY = 'SHARED ACCESS SIGNATURE', SECRET = '$sasToken';
END
ELSE
BEGIN
ALTER CREDENTIAL [https://$storageAccountName.blob.core.windows.net/$containerName]
WITH IDENTITY = 'SHARED ACCESS SIGNATURE', SECRET = '$sasToken';
END
"
Invoke-Sqlcmd -ServerInstance $MIInstance -Query $sqlCred -Credential $credentialMI
Write-Output "Credential refreshed on $MIInstance"
#drop existing db
$sqlDrop="
IF EXISTS(
SELECT 1
FROM sys.databases d
WHERE d.name ='$supersetToCopy'
)
BEGIN
DROP DATABASE $supersetToCopy;
END
"
Invoke-Sqlcmd -ServerInstance $MIInstance -Query $sqlDrop -Credential $credentialMI
Write-Output "Dropped existing $supersetToCopy db (if needed)"
#restore superset
$url=$backupFile
$sqlRestore="
RESTORE DATABASE [$supersetToCopy] FROM URL = N'$url'
"
Invoke-Sqlcmd -ServerInstance $MIInstance -Query $sqlRestore -Credential $credentialMI
write-output "Restored $supersetToCopy"
#create user for sql-au_bag_apv
$sqlUser="
IF NOT EXISTS (
SELECT *
FROM sys.database_principals s
WHERE s.name='sql-au_bag_apv'
)
BEGIN
CREATE USER [sql-au_bag_apv] FOR LOGIN [sql-au_bag_apv];
END
"
Invoke-Sqlcmd -ServerInstance $MIInstance -Query $sqlUser -Credential $credentialMI -Database $supersetToCopy
write-output "Created user for login [sql-au_bag_apv] in $supersetToCopy"
#give db_datareader
$sqlPerms="
ALTER ROLE [db_datareader] ADD MEMBER [sql-au_bag_apv];
GRANT EXECUTE TO [sql-au_bag_apv];
"
Invoke-Sqlcmd -ServerInstance $MIInstance -Query $sqlPerms -Credential $credentialMI -Database $supersetToCopy
write-output "Added user [sql-au_bag_apv] with read and execute permissions to $supersetToCopy"

View File

@@ -0,0 +1,193 @@
/*
12.03.2025, TSC
*/
USE [dba_reporting]
SELECT * from SYM_NODE
--SELECT * FROM SYM_NODE_GROUP
SELECT * FROM SYM_NODE_GROUP_LINK
SELECT * FROM sym_channel
SELECT * FROM SYM_ROUTER
SELECT * FROM SYM_TRIGGER ORDER BY [channel_id]
SELECT * FROM SYM_TRIGGER_ROUTER ORDER BY [router_id]
--SELECT * FROM [dbo].[sym_outgoing_batch]
RETURN
--set push to cloud, pull to onprem
UPDATE [dbo].[sym_node_group_link] SET [data_event_action]='W' WHERE [source_node_group_id]='cloud'
UPDATE [dbo].[sym_node_group_link] SET [data_event_action]='P' WHERE [source_node_group_id]='onprem'
--set router target schemas. sl2007 is the target on cloud, sl2007_azure is the target on prem
UPDATE dbo.[sym_router] SET [target_catalog_name]='sl2007', [target_schema_name]='dbo' WHERE [router_id] = 'onprem to cloud'
UPDATE dbo.[sym_router] SET [target_catalog_name]='sl2007_azure', [target_schema_name]='dbo' WHERE [router_id] = 'cloud to onprem'
--create a single channel for all tables to push to cloud
INSERT INTO dbo.SYM_CHANNEL (channel_id, processing_order, max_batch_size, max_batch_to_send,extract_period_millis, batch_algorithm, enabled, description,data_loader_type)
SELECT channel_id, processing_order, max_batch_size, max_batch_to_send,extract_period_millis, batch_algorithm, enabled, description,data_loader_type
FROM(
VALUES ('sl2007_push', 10, 1000, 10, 0, 'default', 1, 'sl2007 data to push to cloud','bulk')
,('sl2007_pull',20,1000,10,0,'default',1,'sl2007 push from cloud to on-prem','bulk')
)x(channel_id, processing_order, max_batch_size, max_batch_to_send,extract_period_millis, batch_algorithm, enabled, description,data_loader_type)
WHERE NOT EXISTS(SELECT 1 FROM [dbo].[sym_channel] c WHERE c.[channel_id] = x.[channel_id]);
--Add tables triggers for push
--DELETE FROM [dbo].[sym_trigger_router] WHERE router_id='onprem to cloud'
--DELETE FROM [dbo].[sym_trigger] WHERE channel_id='sl2007_push'
INSERT INTO dbo.SYM_TRIGGER (trigger_id, source_table_name,channel_id, last_update_time, create_time, source_catalog_name, source_schema_name, excluded_column_names)
SELECT trigger_id, source_table_name,channel_id, last_update_time, create_time, source_catalog_name, source_schema_name,excluded_column_names
FROM (
VALUES
('push_PARTNER_MUTATION', 'PARTNER_MUTATION', 'sl2007_push', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','time_stamp')
,('push_PARTNER', 'PARTNER', 'sl2007_push', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','time_stamp')
,('push_PACKPARTNER_MUTATION', 'PACKPARTNER_MUTATION', 'sl2007_push', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','time_stamp')
,('push_ATCDESCR', 'ATCDESCR', 'sl2007_push', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','time_stamp')
,('push_PREPARATION_IGNORE', 'PREPARATION_IGNORE', 'sl2007_push', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','time_stamp')
,('push_PACK_IGNORE', 'PACK_IGNORE', 'sl2007_push', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','time_stamp')
,('push_PACK_NEW', 'PACK_NEW', 'sl2007_push', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','time_stamp')
,('push_PREPARATION_NEW', 'PREPARATION_NEW', 'sl2007_push', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','time_stamp')
,('push_PACK_MUTATION', 'PACK_MUTATION', 'sl2007_push', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','time_stamp')
,('push_PREPARATION_MUTATION', 'PREPARATION_MUTATION', 'sl2007_push', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','time_stamp')
,('push_PREPARATION', 'PREPARATION', 'sl2007_push', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','time_stamp')
,('push_PACK', 'PACK', 'sl2007_push', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','time_stamp')
,('push_SUBSTANCE', 'SUBSTANCE', 'sl2007_push', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','time_stamp')
,('push_PREPSUB', 'PREPSUB', 'sl2007_push', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','time_stamp')
,('push_PrepGenGroup', 'PrepGenGroup', 'sl2007_push', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',null)
,('push_COM_CHAIN', 'COM_CHAIN', 'sl2007_push', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',null)
,('push_ITDESCR', 'ITDESCR', 'sl2007_push', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','time_stamp')
)x(trigger_id, source_table_name,channel_id, last_update_time, create_time,source_catalog_name, source_schema_name,excluded_column_names)
WHERE NOT EXISTS (SELECT 1 FROM [dbo].[sym_trigger] c WHERE c.[trigger_id]=x.[trigger_id] AND c.[channel_id]=x.[channel_id]);
--trigger router for push
INSERT INTO [dbo].[sym_trigger_router] ([trigger_id],[router_id],[enabled],[initial_load_order],[create_time],[last_update_by],[last_update_time],[description],[data_refresh_type])
SELECT
[trigger_id]
,'onprem to cloud' AS [router_id]
,1 AS [enabled]
,1 AS [initial_load_order]
,CURRENT_TIMESTAMP AS [create_time]
,'TSC' AS [last_update_by]
,CURRENT_TIMESTAMP AS [last_update_time]
,'Push of data to cloud' AS [description]
,'auto' AS [data_refresh_type]
FROM [dbo].[sym_trigger] x
WHERE x.[channel_id]='sl2007_push'
AND NOT EXISTS (SELECT 1 FROM [dbo].[sym_trigger_router] c WHERE c.[trigger_id]=x.[trigger_id] AND c.[router_id]='onprem to cloud')
--Add tables triggers for pull
/*
to create the VALUES below, from the db to sync run the following query and take the "out" column values:
select
t.name,
STUFF(CONVERT(VARCHAR(MAX),f.lst),1,1,'') AS lst,
' ,('''+t.name+''', '''+t.name+''', ''sl2007_pull'', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,''sl2007'',''dbo'','+COALESCE(''''+STUFF(CONVERT(VARCHAR(MAX),f.lst),1,1,'')+'''','NULL')+')' as out
from sys.tables t
OUTER APPLY(
SELECT ','+[c].[COLUMN_NAME]
FROM [INFORMATION_SCHEMA].[COLUMNS] c
WHERE c.[TABLE_NAME] = [t].[name]
AND [c].[DATA_TYPE]='timestamp'
FOR XML PATH(''),TYPE
) f(lst)
ORDER BY t.name
*/
--DELETE FROM [dbo].[sym_trigger_router] WHERE router_id='cloud to onprem'
--DELETE FROM [dbo].[sym_trigger] WHERE channel_id='sl2007_pull'
INSERT INTO dbo.SYM_TRIGGER (trigger_id, source_table_name,channel_id, last_update_time, create_time, source_catalog_name, source_schema_name, excluded_column_names)
SELECT trigger_id, source_table_name,channel_id, last_update_time, create_time, source_catalog_name, source_schema_name,excluded_column_names
FROM (
VALUES
('pull_ATCDESCR', 'ATCDESCR', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_COM_CHAIN', 'COM_CHAIN', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_Gamme', 'Gamme', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_GENERICBASEDATA_FROM_ARTIKEL_SUPERSET', 'GENERICBASEDATA_FROM_ARTIKEL_SUPERSET', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_GENERICBASEDATA_FROM_ARTIKEL_SUPERSET_eme7w006', 'GENERICBASEDATA_FROM_ARTIKEL_SUPERSET_eme7w006', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_IMSData', 'IMSData', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_IMSData_Add', 'IMSData_Add', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_IMSData_Archiv', 'IMSData_Archiv', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_IMSData_ExcelImport', 'IMSData_ExcelImport', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_ITDESCR', 'ITDESCR', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_ITLIMIT', 'ITLIMIT', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_LIMITATION', 'LIMITATION', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_LIMITATION_HISTORY', 'LIMITATION_HISTORY', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_LimitationChangeType', 'LimitationChangeType', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','Time_Stamp')
,('pull_LOASPEZ', 'LOASPEZ', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_Logging', 'Logging', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_NATION', 'NATION', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_ORG_GEN_UMSATZ', 'ORG_GEN_UMSATZ', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_OrgGenManualGrenzwert', 'OrgGenManualGrenzwert', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_ORIGINAL_FREIKAUF', 'ORIGINAL_FREIKAUF', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_OriginalGenericMapping', 'OriginalGenericMapping', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_PACK', 'PACK', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PACK_IGNORE', 'PACK_IGNORE', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PACK_LIMPTS', 'PACK_LIMPTS', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PACK_MUTATION', 'PACK_MUTATION', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PACK_NEW', 'PACK_NEW', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PACKPARTNER', 'PACKPARTNER', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PACKPARTNER_IGNORE', 'PACKPARTNER_IGNORE', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PACKPARTNER_MUTATION', 'PACKPARTNER_MUTATION', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PACKPRICE', 'PACKPRICE', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PARTNER', 'PARTNER', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PARTNER_IGNORE', 'PARTNER_IGNORE', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PARTNER_MUTATION', 'PARTNER_MUTATION', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PIMPORT', 'PIMPORT', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_PreiseNeu', 'PreiseNeu', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_PREPARATION', 'PREPARATION', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PREPARATION_IGNORE', 'PREPARATION_IGNORE', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PREPARATION_MUTATION', 'PREPARATION_MUTATION', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PREPARATION_NAME', 'PREPARATION_NAME', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_PREPARATION_NAME_BOOK', 'PREPARATION_NAME_BOOK', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_PREPARATION_NAME_save_vorGammendef20120404', 'PREPARATION_NAME_save_vorGammendef20120404', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_PREPARATION_NEW', 'PREPARATION_NEW', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PREPARATIONTASK', 'PREPARATIONTASK', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PrepGenGroup', 'PrepGenGroup', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_PREPIT', 'PREPIT', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PREPPACKLIMIT', 'PREPPACKLIMIT', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PREPPACKLIMIT_GGML', 'PREPPACKLIMIT_GGML', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PREPPACKSTATUS', 'PREPPACKSTATUS', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PREPPACKSTATUS_GGML', 'PREPPACKSTATUS_GGML', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PREPSUB', 'PREPSUB', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PRICECHANGETYPE', 'PRICECHANGETYPE', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PRICETYPE', 'PRICETYPE', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PUBLICATION', 'PUBLICATION', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PUBLICATION_STATUS', 'PUBLICATION_STATUS', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_PublicationRun', 'PublicationRun', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_RECIDKORR', 'RECIDKORR', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_SL_AUFNSTRCH', 'SL_AUFNSTRCH', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_STATUSTYPE_GGML', 'STATUSTYPE_GGML', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_STATUSTYPE_SL', 'STATUSTYPE_SL', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_SUBSTANCE', 'SUBSTANCE', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_TEMP_BIG_APV', 'TEMP_BIG_APV', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_temp_Modal_Formstaerke', 'temp_Modal_Formstaerke', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_TempAPV20131101', 'TempAPV20131101', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_TempImport', 'TempImport', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_TempUmsatz3J', 'TempUmsatz3J', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_TempUmsatzGamme', 'TempUmsatzGamme', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_TMP_ArtikelVerknuepfungen', 'TMP_ArtikelVerknuepfungen', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_TMP_VertriebsanteilGruppe', 'TMP_VertriebsanteilGruppe', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_Ueberpruefungseinheit', 'Ueberpruefungseinheit', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_VARIA', 'VARIA', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo','TIME_STAMP')
,('pull_VERLIST', 'VERLIST', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
,('pull_VertriebsanteilGruppe', 'VertriebsanteilGruppe', 'sl2007_pull', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'sl2007','dbo',NULL)
)x(trigger_id, source_table_name,channel_id, last_update_time, create_time,source_catalog_name, source_schema_name,excluded_column_names)
WHERE NOT EXISTS (SELECT 1 FROM [dbo].[sym_trigger] c WHERE c.[trigger_id]=x.[trigger_id] AND c.[channel_id]=x.[channel_id]);
--trigger router for pull
INSERT INTO [dbo].[sym_trigger_router] ([trigger_id],[router_id],[enabled],[initial_load_order],[create_time],[last_update_by],[last_update_time],[description],[data_refresh_type])
SELECT
[trigger_id]
,'cloud to onprem' AS [router_id]
,1 AS [enabled]
,1 AS [initial_load_order]
,CURRENT_TIMESTAMP AS [create_time]
,'TSC' AS [last_update_by]
,CURRENT_TIMESTAMP AS [last_update_time]
,'Pull of data from cloud' AS [description]
,'auto' AS [data_refresh_type]
FROM [dbo].[sym_trigger] x
WHERE x.[channel_id]='sl2007_pull'
AND NOT EXISTS (SELECT 1 FROM [dbo].[sym_trigger_router] c WHERE c.[trigger_id]=x.[trigger_id] AND c.[router_id]='cloud to onprem')