Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ericdc1 committed Dec 4, 2013
0 parents commit 96a84e3
Show file tree
Hide file tree
Showing 150 changed files with 8,076 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .gitignore
@@ -0,0 +1,22 @@
Web.Publish.xml
build/
release/
*.suo
*.user
bin
Bin
obj
_ReSharper*
*.csproj.user
*.resharper.user
*.suo
*.cache
TestResult.xml
*.trx
*.log
*.orig
_isconfig.xml
Express/
*.msi
setup.exe
source/packages/*
58 changes: 58 additions & 0 deletions README.md
@@ -0,0 +1,58 @@
AliaSQL
================

AliaSQL is a command line tool for database deployments

It depends on having a database scripts folder with these 3 folders:
- Create
- Update
- Seed

Example usage:

```dos
AliaSQL.exe [Action] [Database Server] [Scripts path]
```

Create database and run all scripts in Create folder.
Logs to usd_AppliedDatabaseScript
```dos
AliaSQL.exe Create .\sqlexpress ./scripts
```

Run all scripts in Create and Update folders that have not yet been ran - expects database to already exist.
Logs to usd_AppliedDatabaseScript
```dos
AliaSQL.exe Update .\sqlexpress ./scripts
```

Drop and recreate database then run all scripts in Create and Update folders.
Logs to usd_AppliedDatabaseScript
```dos
AliaSQL.exe Rebuild .\sqlexpress ./scripts
```

Run all scripts in Seed folder that has yet been ran - expects database to already exist.
Logs to usd_AppliedDatabaseSeedScript
```dos
AliaSQL.exe Seed .\sqlexpress ./scripts
```

Logs but does not execute all scripts in Create and Update folders that have not yet been ran - expects database to already exist. This is to add the usd_AppliedDatabaseScript table and a record of all scripts to a legacy database.
Logs to usd_AppliedDatabaseScript
```dos
AliaSQL.exe Baseline .\sqlexpress ./scripts
```


I like to create a console application in my solution that contains the Create/Update/Seed folders and a simple program to execute AliaSQL.exe from Visual Studio. Here is an example of this https://github.com/ericdc1/AliaSQL/blob/master/source/Database/Program.cs I suggest downloading the code to give it a try.

Note that I have Psake set up to build the code, run the (limited) unit tests, create the nuget package, and zip it all up. This is designed to work with Visual Studio 2013 and SQL Server 2012. It should work against SQL Server 2008 and will compile against older Visual Studio versions with a change in the /p:VisualStudioVersion= setting in default.ps1.

I posted some details on why I did this on our office blog at http://sharpcoders.org/post/Introducing-AliaSQL


Install it via Nuget at https://www.nuget.org/packages/AliaSQL/2.0.0.1202

Or download it via Github releases at https://github.com/ericdc1/AliaSQL/releases

Binary file added Thumbs.db
Binary file not shown.
2 changes: 2 additions & 0 deletions build.bat
@@ -0,0 +1,2 @@
powershell.exe -NoProfile -ExecutionPolicy unrestricted -Command "& { Import-Module '.\lib\psakev4\psake.psm1'; Invoke-psake .\default.ps1; }"
pause
3 changes: 3 additions & 0 deletions build_and_package.bat
@@ -0,0 +1,3 @@
powershell.exe -NoProfile -ExecutionPolicy unrestricted -Command "& { Import-Module '.\lib\psakev4\psake.psm1'; Invoke-psake ci; }"

pause
1 change: 1 addition & 0 deletions database-rebuild.bat
@@ -0,0 +1 @@
powershell.exe -NoProfile -ExecutionPolicy unrestricted -Command "& { Import-Module '.\lib\psakev4\psake.psm1'; Invoke-psake RebuildDatabase; }"
1 change: 1 addition & 0 deletions database-seed.bat
@@ -0,0 +1 @@
powershell.exe -NoProfile -ExecutionPolicy unrestricted -Command "& { Import-Module '.\lib\psakev4\psake.psm1'; Invoke-psake SeedDatabase; }"
1 change: 1 addition & 0 deletions database-update.bat
@@ -0,0 +1 @@
powershell.exe -NoProfile -ExecutionPolicy unrestricted -Command "& { Import-Module '.\lib\psakev4\psake.psm1'; Invoke-psake UpdateDatabase; }"
213 changes: 213 additions & 0 deletions default.ps1
@@ -0,0 +1,213 @@
properties {
$projectName = "AliaSQL"
$version = "1.0.0"

$version = $version + "." + (get-date -format "MMdd")
$projectConfig = "Release"
$base_dir = resolve-path .
$source_dir = "$base_dir\source"
$unitTestAssembly = "$projectName.UnitTests.dll"
$nunitPath = "$source_dir\packages\NUnit.Runners.2.6.3\tools\"
$AliaSQLPath = "$base_dir\lib\AliaSQL\"
$build_dir = "$base_dir\build"
$test_dir = "$build_dir\test"
$testCopyIgnorePath = "_ReSharper"
$package_dir = "$build_dir\package"
$package_file = "$build_dir\latestVersion\" + $projectName +"_Package.zip"
$databaseServer = ".\sqlexpress"
$databaseScripts = "$source_dir\Database"
$databaseName = "Demo"
}

task default -depends Init, UpdateAssemblyInfo, Compile, Test
task ci -depends Init, UpdateAssemblyInfo, Compile, Test, Package, NugetPack

task Init {
delete_file $package_file
delete_directory $build_dir
create_directory $test_dir
create_directory $build_dir
}

task Compile -depends Init {
exec { & $source_dir\.nuget\nuget.exe restore $source_dir\$projectName.sln }
msbuild /t:clean /v:q /nologo /p:Configuration=$projectConfig $source_dir\$projectName.sln /p:VisualStudioVersion=12.0

delete_file $error_dir
msbuild /t:build /v:q /nologo /p:Configuration=$projectConfig $source_dir\$projectName.sln /p:VisualStudioVersion=12.0
}

task Test {
if (Test-Path ("$nunitPath\nunit-console-x86.exe")){
copy_all_assemblies_for_test $test_dir
if (Test-Path ("$test_dir\$unitTestAssembly")){
write-host "Testing $unitTestAssembly"
exec {
& $nunitPath\nunit-console-x86.exe $test_dir\$unitTestAssembly /xml $build_dir\UnitTestResult.xml
}
}
else
{
write-host "Cannot run unit tests as $nunitPath\$unitTestAssembly is MISSING"
}
}
else{
write-host "Cannot run tests as $nunitPath\nunit.console.clr4.exe is MISSING"
}
}

task UpdateAssemblyInfo {
Update-AssemblyInfoFiles $version
}

task RebuildDatabase {
exec { & $AliaSQLPath\AliaSQL.exe Rebuild $databaseServer "$databaseName" "$databaseScripts\Scripts"}
}

task UpdateDatabase {
try{
exec { & $AliaSQLPath\AliaSQL.exe Update $databaseServer "$databaseName" "$databaseScripts\Scripts"}
}
catch{
write-host "Database does not exist - running rebuild"
exec { & $AliaSQLPath\AliaSQL.exe Rebuild $databaseServer "$databaseName" "$databaseScripts\Scripts"}
}
}

task SeedDatabase {
exec { & $AliaSQLPath\AliaSQL.exe Seed $databaseServer " $databaseName" "$databaseScripts\Scripts"}
}

task Package -depends Compile {
write-host "Clean package directory"
delete_directory $package_dir

write-host "Copy newly compiled version of Database Deployer"
copy_files "$base_dir\source\AliaSQL.Console\Bin\Release" "$package_dir\AliaSQL"

write-host "Copy in database scripts"
copy_files "$databaseScripts\scripts" "$package_dir\database\"
write-host "Copy AliaSQL tool so scripts can be ran"
copy_files "$AliaSQLPath" "$package_dir\database\"
write-host "Create batch files to run db updates"
create-dbdeployscript "Update" "$package_dir\database\_Update-Database.bat"
create-dbdeployscript "Rebuild" "$package_dir\database\_Rebuild-Database.bat"
create-dbdeployscript "Baseline" "$package_dir\database\_Baseline-Database.bat"
create-dbdeployscript "Seed" "$package_dir\database\_Seed-Database.bat"
write-host "Zip it up"
zip_directory $package_dir $package_file
}

task NugetPack -depends Package {
exec {
& $base_dir\lib\ilmerge.exe /target:exe /lib:C:\Windows\Microsoft.NET\Framework\v4.0.30319 /targetplatform:v4 /out:$package_dir\AliaSQL\AliaSQL.exe $package_dir\AliaSQL\AliaSQL.console.exe $package_dir\AliaSQL\AliaSQL.core.dll
}
exec {
& $source_dir\.nuget\nuget.exe pack -Version $version -outputdirectory $build_dir $base_dir\nuget\AliaSQL.nuspec
}
exec {
& $source_dir\.nuget\nuget.exe pack -Version $version -outputdirectory $build_dir $base_dir\nuget\AliaSQL.Kickstarter.nuspec
}
}

function global:zip_directory($directory,$file) {
write-host "Zipping folder: " $test_assembly
delete_file $file
cd $directory
& "$base_dir\lib\7zip\7za.exe" a -mx=9 -r $file
cd $base_dir
}

function global:copy_files($source,$destination,$exclude=@()){
create_directory $destination
Get-ChildItem $source -Recurse -Exclude $exclude -ErrorAction SilentlyContinue | Copy-Item -ErrorAction SilentlyContinue -Destination {Join-Path $destination $_.FullName.Substring($source.length)}
}

function global:Copy_and_flatten ($source,$filter,$dest) {
ls $source -filter $filter -r | Where-Object{!$_.FullName.Contains("$testCopyIgnorePath") -and !$_.FullName.Contains("packages") -and !$_.FullName.Contains("build") }| cp -dest $dest -force
}

function global:copy_all_assemblies_for_test($destination){
create_directory $destination
Copy_and_flatten $source_dir *.exe $destination
Copy_and_flatten $source_dir *.dll $destination
Copy_and_flatten $source_dir *.config $destination
Copy_and_flatten $source_dir *.xml $destination
Copy_and_flatten $source_dir *.pdb $destination
}

function global:delete_file($file) {
if($file) { remove-item $file -force -ErrorAction SilentlyContinue | out-null }
}

function global:delete_directory($directory_name)
{
rd $directory_name -recurse -force -ErrorAction SilentlyContinue | out-null
}

function global:delete_files_in_dir($dir)
{
get-childitem $dir -recurse | foreach ($_) {remove-item $_.fullname}
}

function global:create_directory($directory_name)
{
mkdir $directory_name -ErrorAction SilentlyContinue | out-null
}

function create-dbdeployscript($verb, $filename)
{
"@echo off
set /p serverName=""DB Server: "" %=%
AliaSQL.exe $verb %serverName% $databaseName .
pause" | out-file $filename -encoding "ASCII"
}


function Update-AssemblyInfoFiles ([string] $version, [System.Array] $excludes = $null) {

#-------------------------------------------------------------------------------
# Update version numbers of AssemblyInfo.cs
# adapted from: http://www.luisrocha.net/2009/11/setting-assembly-version-with-windows.html
#-------------------------------------------------------------------------------

if ($version -notmatch "[0-9]+(\.([0-9]+|\*)){1,3}") {
Write-Error "Version number incorrect format: $version"
}

$versionPattern = 'AssemblyVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)'
$versionAssembly = 'AssemblyVersion("' + $version + '")';
$versionFilePattern = 'AssemblyFileVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)'
$versionAssemblyFile = 'AssemblyFileVersion("' + $version + '")';

Get-ChildItem -r -filter AssemblyInfo.* | % {
$filename = $_.fullname

$update_assembly_and_file = $true

# set an exclude flag where only AssemblyFileVersion is set
if ($excludes -ne $null)
{ $excludes | % { if ($filename -match $_) { $update_assembly_and_file = $false } } }

# see http://stackoverflow.com/questions/3057673/powershell-locking-file
# I am getting really funky locking issues.
# The code block below should be:
# (get-content $filename) | % {$_ -replace $versionPattern, $version } | set-content $filename

$tmp = ($file + ".tmp")
if (test-path ($tmp)) { remove-item $tmp }

if ($update_assembly_and_file) {
(get-content $filename) | % {$_ -replace $versionFilePattern, $versionAssemblyFile } | % {$_ -replace $versionPattern, $versionAssembly } > $tmp
write-host Updating file AssemblyInfo and AssemblyFileInfo: $filename --> $versionAssembly / $versionAssemblyFile
} else {
(get-content $filename) | % {$_ -replace $versionFilePattern, $versionAssemblyFile } > $tmp
write-host Updating file AssemblyInfo only: $filename --> $versionAssemblyFile
}

if (test-path ($filename)) { remove-item $filename }
move-item $tmp $filename -force

}
}

Binary file added images/AliaSQL-200x200.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/AliaSQL.PNG
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/AliaSQL.ico
Binary file not shown.
Binary file added lib/7zip/7-zip.chm
Binary file not shown.
Binary file added lib/7zip/7za.exe
Binary file not shown.

0 comments on commit 96a84e3

Please sign in to comment.