#Requires -Version 5.1 <# .SYNOPSIS Create public synonyms and grants for ROA Oracle. .DESCRIPTION Creates public synonyms for CONTAFIN_ORACLE objects and configures: - Public synonyms for tables, views, packages, types (via synonyms-public.sql) - Public grants (SELECT, EXECUTE, REFERENCES) (via grants-public.sql) - SESIUNE context - Network ACL for CONTAFIN_ORACLE .PARAMETER OracleHome Oracle home directory. If not specified, auto-detects. .PARAMETER ServiceName Database service name. Default: XEPDB1 .PARAMETER SystemPassword SYSTEM user password. Default: romfastsoft .PARAMETER SqlScriptsDir Directory containing SQL scripts. Default: ..\sql .EXAMPLE .\04-create-synonyms-grants.ps1 .EXAMPLE .\04-create-synonyms-grants.ps1 -ServiceName "ROA" -SystemPassword "mypassword" .NOTES File Name : 04-create-synonyms-grants.ps1 Prerequisite : Run 03-import-contafin.ps1 first Copyright 2024 : ROMFAST #> [CmdletBinding()] param( [Parameter(Mandatory = $false)] [string]$OracleHome, [Parameter(Mandatory = $false)] [string]$ServiceName = "XEPDB1", [Parameter(Mandatory = $false)] [string]$SystemPassword = "romfastsoft", [Parameter(Mandatory = $false)] [string]$SqlScriptsDir ) $ErrorActionPreference = 'Stop' # Source library functions . "$PSScriptRoot\lib\logging-functions.ps1" . "$PSScriptRoot\lib\oracle-functions.ps1" # Initialize logging $logPath = Join-Path $PSScriptRoot "..\logs\04-create-synonyms-grants_$(Get-Date -Format 'yyyyMMdd_HHmmss').log" Initialize-LogFile -LogPath $logPath -ScriptName "04-create-synonyms-grants.ps1" try { Write-LogSection "Creating Public Synonyms and Grants" # Validate Oracle installation $oraHome = Get-OracleHome -OracleHome $OracleHome Write-LogSuccess "Oracle Home: $oraHome" # Determine SQL scripts directory if (-not $SqlScriptsDir) { $SqlScriptsDir = Join-Path $PSScriptRoot "..\sql" } # Verify SQL files exist $synonymsScript = Join-Path $SqlScriptsDir "synonyms-public.sql" $grantsScript = Join-Path $SqlScriptsDir "grants-public.sql" if (-not (Test-Path -Path $synonymsScript)) { throw "synonyms-public.sql not found at $synonymsScript" } if (-not (Test-Path -Path $grantsScript)) { throw "grants-public.sql not found at $grantsScript" } Write-Log "SQL scripts directory: $SqlScriptsDir" # Test connection Write-Log "Testing database connection..." if (-not (Test-OracleConnection -OracleHome $oraHome -ServiceName $ServiceName ` -Password $SystemPassword)) { throw "Cannot connect to database. Please verify ServiceName and SystemPassword." } Write-LogSuccess "Database connection successful" # Verify CONTAFIN_ORACLE exists and has objects Write-Log "Verifying CONTAFIN_ORACLE schema..." $counts = Get-SchemaObjectCount -OracleHome $oraHome -ServiceName $ServiceName ` -Password $SystemPassword -SchemaName "CONTAFIN_ORACLE" $totalObjects = if ($counts['TOTAL']) { $counts['TOTAL'] } else { 0 } if ($totalObjects -eq 0) { throw "CONTAFIN_ORACLE schema has no objects. Run 03-import-contafin.ps1 first." } Write-LogSuccess "CONTAFIN_ORACLE has $totalObjects objects" # ========================================================================= # STEP 1: Create Public Synonyms (using synonyms-public.sql) # ========================================================================= Write-LogSection "Creating Public Synonyms" Write-Log "Running synonyms-public.sql..." $result = Invoke-SqlPlus -OracleHome $oraHome -ServiceName $ServiceName ` -Username "SYS" -Password $SystemPassword -SqlFile $synonymsScript -AsSysdba if ($result -match "synonym_count|SYNONYM_NAME") { Write-LogSuccess "Public synonyms created successfully" } else { Write-LogWarning "Could not verify synonym creation" Write-LogDebug $result } # ========================================================================= # STEP 2: Create Grants and ACL (using grants-public.sql) # ========================================================================= Write-LogSection "Creating Grants and Network ACL" Write-Log "Running grants-public.sql..." $grantsResult = Invoke-SqlPlus -OracleHome $oraHome -ServiceName $ServiceName ` -Username "SYS" -Password $SystemPassword -SqlFile $grantsScript -AsSysdba if ($grantsResult -match "Grant|ACL|Grants Complete") { Write-LogSuccess "Grants and ACL configured successfully" } else { Write-LogWarning "Could not verify grants configuration" Write-LogDebug $grantsResult } # ========================================================================= # STEP 3: Verify Results # ========================================================================= Write-LogSection "Verifying Configuration" # Count synonyms $countSql = @" SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF SELECT 'SYNONYM_COUNT:' || COUNT(*) FROM dba_synonyms WHERE owner = 'PUBLIC' AND table_owner = 'CONTAFIN_ORACLE'; EXIT; "@ $countResult = Invoke-SqlPlus -OracleHome $oraHome -ServiceName $ServiceName ` -Username "SYSTEM" -Password $SystemPassword -SqlCommand $countSql -Silent $synonymCount = 0 if ($countResult -match "SYNONYM_COUNT:(\d+)") { $synonymCount = [int]$Matches[1] } Write-Log "Public synonyms for CONTAFIN_ORACLE: $synonymCount" # Verify SESIUNE context exists (created by grants-public.sql or synonyms-public.sql) $contextSql = @" SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF SELECT 'CONTEXT_EXISTS:' || COUNT(*) FROM dba_context WHERE namespace = 'SESIUNE'; EXIT; "@ $contextResult = Invoke-SqlPlus -OracleHome $oraHome -ServiceName $ServiceName ` -Username "SYSTEM" -Password $SystemPassword -SqlCommand $contextSql -Silent $contextExists = $false if ($contextResult -match "CONTEXT_EXISTS:(\d+)") { $contextExists = [int]$Matches[1] -gt 0 } if ($contextExists) { Write-LogSuccess "SESIUNE context exists" } else { Write-LogWarning "SESIUNE context not found - creating..." $createContextSql = @" CREATE CONTEXT SESIUNE USING CONTAFIN_ORACLE.SET_VARIABILE; EXIT; "@ Invoke-SqlPlus -OracleHome $oraHome -ServiceName $ServiceName ` -Username "SYS" -Password $SystemPassword -SqlCommand $createContextSql -AsSysdba } # ========================================================================= # Summary # ========================================================================= Write-LogSection "Setup Complete" Write-LogSuccess "Public synonyms and grants configured!" Write-Log "" Write-Log "Summary:" Write-Log " SQL scripts used:" Write-Log " - synonyms-public.sql (all public synonyms)" Write-Log " - grants-public.sql (all grants and ACL)" Write-Log " Public synonyms created: $synonymCount" Write-Log " SESIUNE context: $(if ($contextExists) { 'Verified' } else { 'Created' })" Write-Log " Network ACL: Configured (roaupdate.xml)" Write-Log "" Write-Log "Next steps:" Write-Log " 1. Run 05-import-companies.ps1 to import company schemas" Close-LogFile -Success $true exit 0 } catch { Write-LogError "Setup failed: $_" Write-LogError $_.ScriptStackTrace Close-LogFile -Success $false exit 1 }