Skip to content

Instantly share code, notes, and snippets.

@ferventcoder
Created May 30, 2013 16:02
Show Gist options
  • Save ferventcoder/5679052 to your computer and use it in GitHub Desktop.
Save ferventcoder/5679052 to your computer and use it in GitHub Desktop.
Concatenate Database scripts into one
param(
[alias("startWith")]
[string]$startFile=''
)
$scriptDir = $(Split-Path -parent $MyInvocation.MyCommand.Definition)
$dateNow = Get-Date -format "yyyyMMdd_HHmmss"
$fileNameToSaveTo = 'SingleMigration.' + $dateNow + '.sql'
$fileToSaveTo = Join-Path $scriptDir "src\Database\somedb-concat\$($fileNameToSaveTo)"
$databaseTopLevelFolder = Join-Path $scriptDir 'src\Database\somedb'
$databaseUpFolder = Join-Path $databaseTopLevelFolder 'up'
$databaseFunctionsFolder = Join-Path $databaseTopLevelFolder 'functions'
$databaseViewsFolder = Join-Path $databaseTopLevelFolder 'views'
$databaseSprocsFolder = Join-Path $databaseTopLevelFolder 'sprocs'
$databaseIndexesFolder = Join-Path $databaseTopLevelFolder 'indexes'
$databasePermissionsFolder = Join-Path $databaseTopLevelFolder 'permissions'
function GetFilesInFolder {
param(
[string] $folderName
)
if (Test-Path "$folderName") {
Get-ChildItem "$folderName" -Filter *.sql -Exclude *.ENV.* -Recurse | Sort-Object $_.Name | %{
#write-host "found $($_.FullName)"
return $_
}
}
}
#http://franckrichard.blogspot.com/2010/08/powershell-get-encoding-file-type.html
function Get-FileEncoding
{
param (
[Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)]
[string]$Path
)
[byte[]]$byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path
#Write-Host Bytes: $byte[0] $byte[1] $byte[2] $byte[3]
# EF BB BF (UTF8)
if ( $byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf )
{ Write-Output 'UTF8' }
# FE FF (UTF-16 Big-Endian)
elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff)
{ Write-Output 'BigEndianUnicode' }
#{ Write-Output 'Unicode UTF-16 Big-Endian' }
# FF FE (UTF-16 Little-Endian)
elseif ($byte[0] -eq 0xff -and $byte[1] -eq 0xfe)
{ Write-Output 'Unicode' }
#{ Write-Output 'Unicode UTF-16 Little-Endian' }
# 00 00 FE FF (UTF32 Big-Endian)
elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff)
{ Write-Output 'BigEndianUnicode' }
#{ Write-Output 'UTF32 Big-Endian' }
# FE FF 00 00 (UTF32 Little-Endian)
elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff -and $byte[2] -eq 0 -and $byte[3] -eq 0)
{ Write-Output 'Unicode' }
#{ Write-Output 'UTF32 Little-Endian' }
# 2B 2F 76 (38 | 38 | 2B | 2F)
elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76 -and ($byte[3] -eq 0x38 -or $byte[3] -eq 0x39 -or $byte[3] -eq 0x2b -or $byte[3] -eq 0x2f) )
{ Write-Output 'UTF7'}
# F7 64 4C (UTF-1)
elseif ( $byte[0] -eq 0xf7 -and $byte[1] -eq 0x64 -and $byte[2] -eq 0x4c )
{ Write-Output 'UTF-1' }
# DD 73 66 73 (UTF-EBCDIC)
elseif ($byte[0] -eq 0xdd -and $byte[1] -eq 0x73 -and $byte[2] -eq 0x66 -and $byte[3] -eq 0x73)
{ Write-Output 'UTF-EBCDIC' }
# 0E FE FF (SCSU)
elseif ( $byte[0] -eq 0x0e -and $byte[1] -eq 0xfe -and $byte[2] -eq 0xff )
{ Write-Output 'SCSU' }
# FB EE 28 (BOCU-1)
elseif ( $byte[0] -eq 0xfb -and $byte[1] -eq 0xee -and $byte[2] -eq 0x28 )
{ Write-Output 'BOCU-1' }
# 84 31 95 33 (GB-18030)
elseif ($byte[0] -eq 0x84 -and $byte[1] -eq 0x31 -and $byte[2] -eq 0x95 -and $byte[3] -eq 0x33)
{ Write-Output 'GB-18030' }
else
{
#write-host "we have ascii for $Path"
Write-Output 'ASCII'
}
}
function GetFileText {
param (
[string] $fileName
)
$encoding = Get-FileEncoding $fileName
$content = Get-Content $fileName -encoding $encoding
$returncontent = ''
foreach ($line in $content) {
$returncontent += "`r`n$line"
}
#write-host $content
return $returncontent
}
function AppendFile {
param(
[string] $outputFile
,[string] $textToAppend
)
Add-Content "$outputFile" $textToAppend -encoding Unicode #BigEndianUnicode #ASCII #Unicode #ANSI
}
function GetSqlFilesInFolderAndAppendToFile {
param (
[string] $folder
, [string] $filePath
, [bool] $isOneTimeFolder = $false
)
$go = 'GO'
$fileText = ''
$shouldAppendFile = $true
GetFilesInFolder $folder | %{
$fileText = "/* ===== $($_.Name) ===== */ `r`n"
$fileText += GetFileText $_.FullName
$fileText +="`r`n$go`r`n`r`n "
$fileText = $fileText -replace 'GO[\s\n\r]+GO', 'GO' #GO[\s\n\r]+GO
#write-host $fileText
$shouldAppendFile = $true
if ($isOneTimeFolder -and $startFile -ne '') {
#write-host 'is up folder item'
#todo, check date of file and only load this file and ones newer
if ($_.Name -lt $startfile) {
Write-Host "`'$($_.Name)`' will not be loaded - it comes before `'$startFile`'"
$shouldAppendFile = $false
}
}
if ($shouldAppendFile) {
AppendFile $filePath $fileText
}
}
}
if (test-path $fileToSaveTo) {
Remove-Item $fileToSaveTo -force
}
GetSqlFilesInFolderAndAppendToFile $databaseUpFolder $fileToSaveTo $true
GetSqlFilesInFolderAndAppendToFile $databaseFunctionsFolder $fileToSaveTo
GetSqlFilesInFolderAndAppendToFile $databaseViewsFolder $fileToSaveTo
GetSqlFilesInFolderAndAppendToFile $databaseSprocsFolder $fileToSaveTo
GetSqlFilesInFolderAndAppendToFile $databaseIndexesFolder $fileToSaveTo
GetSqlFilesInFolderAndAppendToFile $databasePermissionsFolder $fileToSaveTo
Write-Host "Finished concatenating all sql files to `'$fileToSaveTo`'. Please ensure files concatenated correctly. In cases where you have funny characters, you may need to fix those sections manually."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment