Aggiunto DB bfdskreport02p
This commit is contained in:
parent
5d6841891b
commit
3692d71086
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,5 @@
|
||||
logs/
|
||||
transito/
|
||||
localhost/
|
||||
stored/
|
||||
# extract/
|
@ -19,6 +19,9 @@ Questo progetto contiene gli script e le procedure necessarie per la creazione e
|
||||
## Script Disponibili
|
||||
|
||||
- `creadb.ps1` - Crea il database e le tabelle
|
||||
- `-UseDoneFolder` - Sposta i file SQL eseguiti con successo nella cartella 'done'
|
||||
- `-LeaveSuccessLogs` - Mantiene i file di log anche in caso di successo
|
||||
- `-DeleteSuccessSql` - Elimina i file SQL dopo l'esecuzione con successo
|
||||
- `esporta.ps1` - Esporta una tabella in formato CSV
|
||||
- `importa.ps1` - Importa una tabella da un file CSV
|
||||
- `copia.ps1` - Copia una tabella da un ambiente all'altro (combina esporta e importa)
|
||||
@ -37,4 +40,4 @@ Questo progetto contiene gli script e le procedure necessarie per la creazione e
|
||||
|
||||
## Ultimo Aggiornamento
|
||||
|
||||
Data: 6 Giugno 2025
|
||||
Data: 19 Giugno 2025
|
||||
|
@ -3,17 +3,25 @@ $config = @{
|
||||
# Credenziali database
|
||||
# DbUser = "sa"
|
||||
# DbPassword = "_p1sap1a"
|
||||
# DbUser = "SEIReport"
|
||||
# DbPassword = "SEIReport"
|
||||
DbUser = "F701264"
|
||||
DbPassword = "contrsei"
|
||||
|
||||
# DbHostOutput
|
||||
DbHostOutput = "DATABASE_PDC_LOCALE"
|
||||
# DbHostOutput = "testbes.armundia.com"
|
||||
DbHostInput = "bfdskrepsei01c.sysfideuram.sysbancafideuram.it"
|
||||
|
||||
# DbHostInput
|
||||
# DbHostInput = "bfdskrepsei01c.sysfideuram.sysbancafideuram.it"
|
||||
# DbHostInput = "testbes.armundia.com"
|
||||
# DbHostInput = "DATABASE_PDC_LOCALE"
|
||||
# DbHostInput = "bfdskreport01p.fideuram.bancafideuram.it"
|
||||
DbHostInput = "bfdskreport01p.fideuram.bancafideuram.it"
|
||||
# DbHostInput = "bfdskreport02p.fideuram.bancafideuram.it"
|
||||
# DbName = "master"
|
||||
ServerName = "CONSUNI"
|
||||
DbName = "C6StampeCentralizzate"
|
||||
# DbName = "SEIReport"
|
||||
|
||||
# Directory di log
|
||||
WorkDir = Split-Path -Path $PSScriptRoot -Parent
|
||||
|
@ -1,6 +1,7 @@
|
||||
param(
|
||||
[switch]$UseDoneFolder = $false,
|
||||
[switch]$DeleteSuccessLogs = $true
|
||||
[switch]$UseDoneFolder,
|
||||
[switch]$LeaveSuccessLogs,
|
||||
[switch]$DeleteSuccessSql
|
||||
)
|
||||
|
||||
# Importa configurazione
|
||||
@ -12,13 +13,14 @@ $logFile = Join-Path $env:logDir "${scriptName}.log" # for debug
|
||||
|
||||
# List of SQL files or folders to execute in order
|
||||
$sqlFiles = @(
|
||||
# "C6SC01_creadb.sql", # file singolo
|
||||
# "base\C6SC01_creadb.sql", # file singolo
|
||||
# "C6SC08_creauser.sql",
|
||||
# "StampeCentralizzateBackupRidotto.sql",
|
||||
# "enable_sa_full.sql",
|
||||
# "add_linked_server.sql",
|
||||
# "enable_F701264_full.sql",
|
||||
# "stored", # cartella
|
||||
# "base\StampeCentralizzateBackupRidotto.sql",
|
||||
# "base\enable_sa_full.sql",
|
||||
# "base\add_linked_server.sql",
|
||||
# "base\enable_F701264_full.sql",
|
||||
# "C6MartPeriodico_GESTIONE_SELECT_S80_FASE2_CU_20190206_preMioFoglio.sql",
|
||||
"stored", # cartella
|
||||
# "tables_from_select", # cartella
|
||||
""
|
||||
)
|
||||
@ -80,24 +82,64 @@ foreach ($fileSql in $filesToExecute) {
|
||||
$retryCount = 0
|
||||
$success = $false
|
||||
|
||||
$success = $false
|
||||
while (-not $success -and $retryCount -lt $maxRetries) {
|
||||
# try {
|
||||
# Execute the SQL file using sqlcmd
|
||||
sqlcmd -S $env:dbHostOutput -U $env:dbUser -P $env:dbPassword -d $env:dbName -i $fileSql >$fileLog 2>&1
|
||||
$esitoComando = $LASTEXITCODE
|
||||
|
||||
try {
|
||||
# Execute SQL and capture all output
|
||||
$output = & sqlcmd -S $env:dbHostOutput -U $env:dbUser -P $env:dbPassword -d $env:dbName -i $fileSql -b 2>&1
|
||||
$esitoComando = $LASTEXITCODE
|
||||
|
||||
# Save output to log file and process it
|
||||
$output | Out-File -FilePath $fileLog
|
||||
$logContent = Get-Content -Path $fileLog -Raw
|
||||
|
||||
# Check for SQL errors
|
||||
if ($logContent -match "(Msg \d+,.*?)(?=Msg \d+,|$)|(Il nome della colonna.*?)(?=Msg \d+,|$)|(Errore di sintassi.*?)(?=Msg \d+,|$)") {
|
||||
"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') ERRORE nell'esecuzione di $fileName" | Out-File -FilePath $logFile -Append
|
||||
"------------------" | Out-File -FilePath $logFile -Append
|
||||
$Matches[0].Trim() | Out-File -FilePath $logFile -Append
|
||||
"----------------------------------------" | Out-File -FilePath $logFile -Append
|
||||
break
|
||||
}
|
||||
|
||||
if ($esitoComando -ne 0) {
|
||||
"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') ERRORE nell'esecuzione di $fileName (Exit Code: $esitoComando)" | Out-File -FilePath $logFile -Append
|
||||
"------------------" | Out-File -FilePath $logFile -Append
|
||||
$logContent | Out-File -FilePath $logFile -Append
|
||||
"----------------------------------------" | Out-File -FilePath $logFile -Append
|
||||
break
|
||||
}
|
||||
|
||||
$success = $true
|
||||
}
|
||||
catch {
|
||||
"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') ERRORE nell'esecuzione di $fileName" | Out-File -FilePath $logFile -Append
|
||||
"------------------" | Out-File -FilePath $logFile -Append
|
||||
$_.Exception.Message | Out-File -FilePath $logFile -Append
|
||||
"----------------------------------------" | Out-File -FilePath $logFile -Append
|
||||
break
|
||||
}
|
||||
|
||||
# Leggi il contenuto del file di log
|
||||
$logContent = Get-Content -Path $fileLog -Raw
|
||||
|
||||
# Controlla vari tipi di messaggi
|
||||
# Controlla vari tipi di messaggi e errori
|
||||
$isObjectExistsError = $logContent -match "There is already an object named"
|
||||
$hasWarnings = $logContent -match "(?m)^Warning:"
|
||||
$hasErrors = $logContent -match "(?m)^Msg \d+, Level \d+, State \d+" -or
|
||||
$logContent -match "Incorrect syntax" -or
|
||||
$logContent -match "Invalid object name" -or
|
||||
$logContent -match "Could not find stored procedure" -or
|
||||
$logContent -match "Login failed"
|
||||
$warningLines = if ($hasWarnings) {
|
||||
$logContent -split "`n" | Where-Object { $_ -match "^Warning:" }
|
||||
} else { @() }
|
||||
$errorLines = if ($hasErrors) {
|
||||
($logContent -split "`n" | Where-Object { $_ -match "^Msg \d+,|^Incorrect syntax|^Invalid object|^Could not find|^Login failed" }) -join "`n"
|
||||
} else { "" }
|
||||
"logSqlApp: $logSqlApp esitoComando: $esitoComando" | Out-File -FilePath $logFile -Append
|
||||
|
||||
IF ($esitoComando -eq 0 -or $isObjectExistsError) {
|
||||
IF (($esitoComando -eq 0 -or $isObjectExistsError) -and -not $hasErrors) {
|
||||
# Determina lo stato dell'esecuzione
|
||||
$status = if ($isObjectExistsError) {
|
||||
"SKIP"
|
||||
@ -119,13 +161,16 @@ foreach ($fileSql in $filesToExecute) {
|
||||
}
|
||||
} else {
|
||||
Write-Host "Eseguito con successo $fileName" -ForegroundColor Green
|
||||
# Cancella il log file solo se richiesto e non ci sono warning ed errori
|
||||
if ($DeleteSuccessLogs) {
|
||||
# Cancella il log file se non è richiesto di mantenerlo e non ci sono warning ed errori
|
||||
if (-not $LeaveSuccessLogs) {
|
||||
Remove-Item -Path $fileLog -Force
|
||||
}
|
||||
|
||||
# Sposta il file SQL nella directory done se richiesto
|
||||
if ($UseDoneFolder) {
|
||||
# Gestione del file SQL dopo l'esecuzione con successo
|
||||
if ($DeleteSuccessSql) {
|
||||
Remove-Item -Path $fileSql -Force
|
||||
Write-Host " File SQL eliminato" -ForegroundColor Gray
|
||||
} elseif ($UseDoneFolder) {
|
||||
$destPath = Join-Path $doneDir $fileName
|
||||
Move-Item -Path $fileSql -Destination $destPath -Force
|
||||
Write-Host " File spostato in done/" -ForegroundColor Gray
|
||||
@ -135,7 +180,11 @@ foreach ($fileSql in $filesToExecute) {
|
||||
} else {
|
||||
Write-Host "ERRORE nell'esecuzione di $fileName" -ForegroundColor Red
|
||||
Write-Host "Log dell'errore:" -ForegroundColor Red
|
||||
Write-Host $logContent -ForegroundColor Red
|
||||
if ($errorLines) {
|
||||
Write-Host $errorLines -ForegroundColor Red
|
||||
} else {
|
||||
Write-Host $logContent -ForegroundColor Red
|
||||
}
|
||||
Write-Host "----------------------------------------" -ForegroundColor Red
|
||||
|
||||
$(Get-Date -Format "yyyy-MM-dd_HH:mm:ss")+" ERROR per $fileSql" | Out-File -FilePath $logFile -Append
|
||||
|
@ -3,13 +3,17 @@
|
||||
. $PSScriptRoot\conf.ps1
|
||||
|
||||
# Imposta il percorso di output nella cartella sql
|
||||
$OutputPath = Join-Path $env:workdir "sql\stored"
|
||||
$OutputPath = Join-Path $env:workdir "transito"
|
||||
|
||||
# Importa il modulo SQL Server se non è già caricato
|
||||
if (-not (Get-Module -Name SQLPS)) {
|
||||
Import-Module SQLPS -DisableNameChecking
|
||||
# Verifica e installa il modulo SqlServer se necessario
|
||||
if (-not (Get-Module -ListAvailable -Name SqlServer)) {
|
||||
Write-Host "Installazione del modulo SqlServer..."
|
||||
Install-Module -Name SqlServer -Force -AllowClobber -Scope CurrentUser
|
||||
}
|
||||
|
||||
# Importa il modulo SqlServer
|
||||
Import-Module SqlServer -DisableNameChecking
|
||||
|
||||
try {
|
||||
# Query per ottenere l'elenco degli oggetti
|
||||
$query = @"
|
||||
@ -30,7 +34,8 @@ try {
|
||||
"@
|
||||
|
||||
# Esegui la query e salva i risultati
|
||||
$results = Invoke-Sqlcmd -ServerInstance $config.DbHostInput -Database $config.DbName -Username $config.DbUser -Password $config.DbPassword -Query $query
|
||||
$connectionString = "Server=$($config.DbHostInput);Database=$($config.DbName);User Id=$($config.DbUser);Password=$($config.DbPassword);TrustServerCertificate=True"
|
||||
$results = Invoke-Sqlcmd -ConnectionString $connectionString -Query $query
|
||||
|
||||
# Prepara l'output
|
||||
$output = "Elenco oggetti del database $($config.DbName) su $($config.DbHostInput) il $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')`r`n"
|
||||
|
@ -6,7 +6,7 @@ $logFile = Join-Path $env:logDir "${scriptName}.log"
|
||||
"start" | Out-File -FilePath $logFile
|
||||
|
||||
# Imposta il percorso di output nella cartella sql
|
||||
$OutputPath = Join-Path $env:workdir "sql\tables"
|
||||
$OutputPath = Join-Path $env:workdir "transito\tables"
|
||||
|
||||
# Verifica che la directory di output esista, altrimenti la crea
|
||||
if (-not (Test-Path -Path $OutputPath)) {
|
||||
@ -22,47 +22,55 @@ try {
|
||||
# Query per estrarre la definizione delle tabelle
|
||||
$query = @"
|
||||
WITH TableColumns AS (
|
||||
SELECT
|
||||
SELECT DISTINCT
|
||||
t.object_id,
|
||||
STRING_AGG(
|
||||
CONCAT(
|
||||
CHAR(9), '[', c.name, '] ',
|
||||
CASE
|
||||
WHEN t2.name IN ('char', 'varchar', 'nchar', 'nvarchar')
|
||||
THEN t2.name + '(' +
|
||||
CASE
|
||||
WHEN c.max_length = -1 THEN 'MAX'
|
||||
WHEN t2.name LIKE 'n%' THEN CAST(c.max_length/2 AS VARCHAR)
|
||||
ELSE CAST(c.max_length AS VARCHAR)
|
||||
END + ')'
|
||||
WHEN t2.name IN ('decimal', 'numeric')
|
||||
THEN t2.name + '(' + CAST(c.[precision] AS VARCHAR) + ',' + CAST(c.[scale] AS VARCHAR) + ')'
|
||||
ELSE t2.name
|
||||
END,
|
||||
CASE WHEN c.is_nullable = 0 THEN ' NOT NULL' ELSE ' NULL' END,
|
||||
CASE WHEN ic.is_identity = 1 THEN ' IDENTITY(' + CAST(IDENT_SEED(OBJECT_SCHEMA_NAME(t.object_id) + '.' + OBJECT_NAME(t.object_id)) AS VARCHAR) + ',' + CAST(IDENT_INCR(OBJECT_SCHEMA_NAME(t.object_id) + '.' + OBJECT_NAME(t.object_id)) AS VARCHAR) + ')' ELSE '' END,
|
||||
CASE WHEN dc.definition IS NOT NULL THEN ' DEFAULT ' + dc.definition ELSE '' END
|
||||
),
|
||||
',' + CHAR(13) + CHAR(10)
|
||||
) WITHIN GROUP (ORDER BY c.column_id) as ColumnDefinitions
|
||||
(
|
||||
SELECT STUFF((
|
||||
SELECT ',' + CHAR(13) + CHAR(10) +
|
||||
CHAR(9) + '[' + c2.name + '] ' +
|
||||
CASE
|
||||
WHEN t2.name IN ('char', 'varchar', 'nchar', 'nvarchar')
|
||||
THEN t2.name + '(' +
|
||||
CASE
|
||||
WHEN c2.max_length = -1 THEN 'MAX'
|
||||
WHEN t2.name LIKE 'n%' THEN CAST(c2.max_length/2 AS VARCHAR)
|
||||
ELSE CAST(c2.max_length AS VARCHAR)
|
||||
END + ')'
|
||||
WHEN t2.name IN ('decimal', 'numeric')
|
||||
THEN t2.name + '(' + CAST(c2.[precision] AS VARCHAR) + ',' + CAST(c2.[scale] AS VARCHAR) + ')'
|
||||
ELSE t2.name
|
||||
END +
|
||||
CASE WHEN c2.is_nullable = 0 THEN ' NOT NULL' ELSE ' NULL' END +
|
||||
CASE WHEN ic.is_identity = 1 THEN ' IDENTITY(' + CAST(IDENT_SEED(OBJECT_SCHEMA_NAME(t.object_id) + '.' + OBJECT_NAME(t.object_id)) AS VARCHAR) + ',' + CAST(IDENT_INCR(OBJECT_SCHEMA_NAME(t.object_id) + '.' + OBJECT_NAME(t.object_id)) AS VARCHAR) + ')' ELSE '' END +
|
||||
CASE WHEN dc.definition IS NOT NULL THEN ' DEFAULT ' + dc.definition ELSE '' END
|
||||
FROM sys.columns c2
|
||||
INNER JOIN sys.types t2 ON c2.user_type_id = t2.user_type_id
|
||||
LEFT JOIN sys.identity_columns ic ON ic.object_id = c2.object_id AND ic.column_id = c2.column_id
|
||||
LEFT JOIN sys.default_constraints dc ON dc.parent_object_id = c2.object_id AND dc.parent_column_id = c2.column_id
|
||||
WHERE c2.object_id = t.object_id
|
||||
ORDER BY c2.column_id
|
||||
FOR XML PATH('')
|
||||
), 1, 2, '')
|
||||
) as ColumnDefinitions
|
||||
FROM sys.tables t
|
||||
INNER JOIN sys.columns c ON c.object_id = t.object_id
|
||||
INNER JOIN sys.types t2 ON c.user_type_id = t2.user_type_id
|
||||
LEFT JOIN sys.identity_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
|
||||
LEFT JOIN sys.default_constraints dc ON dc.parent_object_id = c.object_id AND dc.parent_column_id = c.column_id
|
||||
GROUP BY t.object_id
|
||||
),
|
||||
TablePrimaryKeys AS (
|
||||
SELECT
|
||||
SELECT DISTINCT
|
||||
i.object_id,
|
||||
CHAR(13) + CHAR(10) + 'ALTER TABLE [' + OBJECT_SCHEMA_NAME(i.object_id) + '].[' + OBJECT_NAME(i.object_id) + '] ADD CONSTRAINT [' + i.name + '] PRIMARY KEY ' +
|
||||
CASE WHEN i.type = 1 THEN 'CLUSTERED' ELSE 'NONCLUSTERED' END +
|
||||
' (' + STRING_AGG(QUOTENAME(c.name), ',') WITHIN GROUP (ORDER BY ic.key_ordinal) + ')' as PkDefinition
|
||||
(
|
||||
SELECT CHAR(13) + CHAR(10) + 'ALTER TABLE [' + OBJECT_SCHEMA_NAME(i.object_id) + '].[' + OBJECT_NAME(i.object_id) + '] ADD CONSTRAINT [' + i.name + '] PRIMARY KEY ' +
|
||||
CASE WHEN i.type = 1 THEN 'CLUSTERED' ELSE 'NONCLUSTERED' END +
|
||||
' (' + STUFF((
|
||||
SELECT ',' + QUOTENAME(c2.name)
|
||||
FROM sys.index_columns ic2
|
||||
INNER JOIN sys.columns c2 ON c2.object_id = ic2.object_id AND c2.column_id = ic2.column_id
|
||||
WHERE ic2.object_id = i.object_id AND ic2.index_id = i.index_id
|
||||
ORDER BY ic2.key_ordinal
|
||||
FOR XML PATH('')
|
||||
), 1, 1, '') + ')'
|
||||
) as PkDefinition
|
||||
FROM sys.indexes i
|
||||
INNER JOIN sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id
|
||||
INNER JOIN sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id
|
||||
WHERE i.is_primary_key = 1
|
||||
GROUP BY i.object_id, i.name, i.type
|
||||
)
|
||||
SELECT
|
||||
s.name as SchemaName,
|
||||
@ -85,7 +93,31 @@ try {
|
||||
Write-Host "Eseguo query su $env:dbHostInput..."
|
||||
"Eseguo query su $env:dbHostInput..." | Out-File -FilePath $logFile -Append
|
||||
|
||||
$tables = Invoke-Sqlcmd -ServerInstance $env:dbHostInput -Database $env:dbName -Query $query -Username $env:dbUser -Password $env:dbPassword -MaxCharLength 1000000 -TrustServerCertificate
|
||||
$sqlParams = @{
|
||||
ServerInstance = $env:dbHostInput
|
||||
Database = $env:dbName
|
||||
Query = $query
|
||||
Username = $env:dbUser
|
||||
Password = $env:dbPassword
|
||||
MaxCharLength = 1000000
|
||||
TrustServerCertificate = $true
|
||||
ConnectionTimeout = 30
|
||||
EncryptConnection = $true
|
||||
ApplicationIntent = 'ReadOnly'
|
||||
}
|
||||
|
||||
try {
|
||||
Write-Host "Tentativo di connessione a $($sqlParams.ServerInstance)..."
|
||||
$tables = Invoke-Sqlcmd @sqlParams
|
||||
}
|
||||
catch {
|
||||
Write-Error "Errore di connessione al database: $($_.Exception.Message)"
|
||||
Write-Host "Dettagli connessione:"
|
||||
Write-Host "Server: $($sqlParams.ServerInstance)"
|
||||
Write-Host "Database: $($sqlParams.Database)"
|
||||
Write-Host "Utente: $($sqlParams.Username)"
|
||||
throw
|
||||
}
|
||||
|
||||
Write-Host "Trovate $($tables.Count) tabelle"
|
||||
"Trovate $($tables.Count) tabelle" | Out-File -FilePath $logFile -Append
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user