Skip to content

Commit 96a84e3

Browse files
committed
Initial commit
0 parents  commit 96a84e3

File tree

150 files changed

+8076
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

150 files changed

+8076
-0
lines changed

.gitignore

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Web.Publish.xml
2+
build/
3+
release/
4+
*.suo
5+
*.user
6+
bin
7+
Bin
8+
obj
9+
_ReSharper*
10+
*.csproj.user
11+
*.resharper.user
12+
*.suo
13+
*.cache
14+
TestResult.xml
15+
*.trx
16+
*.log
17+
*.orig
18+
_isconfig.xml
19+
Express/
20+
*.msi
21+
setup.exe
22+
source/packages/*

README.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
AliaSQL
2+
================
3+
4+
AliaSQL is a command line tool for database deployments
5+
6+
It depends on having a database scripts folder with these 3 folders:
7+
- Create
8+
- Update
9+
- Seed
10+
11+
Example usage:
12+
13+
```dos
14+
AliaSQL.exe [Action] [Database Server] [Scripts path]
15+
```
16+
17+
Create database and run all scripts in Create folder.
18+
Logs to usd_AppliedDatabaseScript
19+
```dos
20+
AliaSQL.exe Create .\sqlexpress ./scripts
21+
```
22+
23+
Run all scripts in Create and Update folders that have not yet been ran - expects database to already exist.
24+
Logs to usd_AppliedDatabaseScript
25+
```dos
26+
AliaSQL.exe Update .\sqlexpress ./scripts
27+
```
28+
29+
Drop and recreate database then run all scripts in Create and Update folders.
30+
Logs to usd_AppliedDatabaseScript
31+
```dos
32+
AliaSQL.exe Rebuild .\sqlexpress ./scripts
33+
```
34+
35+
Run all scripts in Seed folder that has yet been ran - expects database to already exist.
36+
Logs to usd_AppliedDatabaseSeedScript
37+
```dos
38+
AliaSQL.exe Seed .\sqlexpress ./scripts
39+
```
40+
41+
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.
42+
Logs to usd_AppliedDatabaseScript
43+
```dos
44+
AliaSQL.exe Baseline .\sqlexpress ./scripts
45+
```
46+
47+
48+
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.
49+
50+
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.
51+
52+
I posted some details on why I did this on our office blog at http://sharpcoders.org/post/Introducing-AliaSQL
53+
54+
55+
Install it via Nuget at https://www.nuget.org/packages/AliaSQL/2.0.0.1202
56+
57+
Or download it via Github releases at https://github.com/ericdc1/AliaSQL/releases
58+

Thumbs.db

123 KB
Binary file not shown.

build.bat

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
powershell.exe -NoProfile -ExecutionPolicy unrestricted -Command "& { Import-Module '.\lib\psakev4\psake.psm1'; Invoke-psake .\default.ps1; }"
2+
pause

build_and_package.bat

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
powershell.exe -NoProfile -ExecutionPolicy unrestricted -Command "& { Import-Module '.\lib\psakev4\psake.psm1'; Invoke-psake ci; }"
2+
3+
pause

database-rebuild.bat

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
powershell.exe -NoProfile -ExecutionPolicy unrestricted -Command "& { Import-Module '.\lib\psakev4\psake.psm1'; Invoke-psake RebuildDatabase; }"

database-seed.bat

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
powershell.exe -NoProfile -ExecutionPolicy unrestricted -Command "& { Import-Module '.\lib\psakev4\psake.psm1'; Invoke-psake SeedDatabase; }"

database-update.bat

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
powershell.exe -NoProfile -ExecutionPolicy unrestricted -Command "& { Import-Module '.\lib\psakev4\psake.psm1'; Invoke-psake UpdateDatabase; }"

default.ps1

+213
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
properties {
2+
$projectName = "AliaSQL"
3+
$version = "1.0.0"
4+
5+
$version = $version + "." + (get-date -format "MMdd")
6+
$projectConfig = "Release"
7+
$base_dir = resolve-path .
8+
$source_dir = "$base_dir\source"
9+
$unitTestAssembly = "$projectName.UnitTests.dll"
10+
$nunitPath = "$source_dir\packages\NUnit.Runners.2.6.3\tools\"
11+
$AliaSQLPath = "$base_dir\lib\AliaSQL\"
12+
$build_dir = "$base_dir\build"
13+
$test_dir = "$build_dir\test"
14+
$testCopyIgnorePath = "_ReSharper"
15+
$package_dir = "$build_dir\package"
16+
$package_file = "$build_dir\latestVersion\" + $projectName +"_Package.zip"
17+
$databaseServer = ".\sqlexpress"
18+
$databaseScripts = "$source_dir\Database"
19+
$databaseName = "Demo"
20+
}
21+
22+
task default -depends Init, UpdateAssemblyInfo, Compile, Test
23+
task ci -depends Init, UpdateAssemblyInfo, Compile, Test, Package, NugetPack
24+
25+
task Init {
26+
delete_file $package_file
27+
delete_directory $build_dir
28+
create_directory $test_dir
29+
create_directory $build_dir
30+
}
31+
32+
task Compile -depends Init {
33+
exec { & $source_dir\.nuget\nuget.exe restore $source_dir\$projectName.sln }
34+
msbuild /t:clean /v:q /nologo /p:Configuration=$projectConfig $source_dir\$projectName.sln /p:VisualStudioVersion=12.0
35+
36+
delete_file $error_dir
37+
msbuild /t:build /v:q /nologo /p:Configuration=$projectConfig $source_dir\$projectName.sln /p:VisualStudioVersion=12.0
38+
}
39+
40+
task Test {
41+
if (Test-Path ("$nunitPath\nunit-console-x86.exe")){
42+
copy_all_assemblies_for_test $test_dir
43+
if (Test-Path ("$test_dir\$unitTestAssembly")){
44+
write-host "Testing $unitTestAssembly"
45+
exec {
46+
& $nunitPath\nunit-console-x86.exe $test_dir\$unitTestAssembly /xml $build_dir\UnitTestResult.xml
47+
}
48+
}
49+
else
50+
{
51+
write-host "Cannot run unit tests as $nunitPath\$unitTestAssembly is MISSING"
52+
}
53+
}
54+
else{
55+
write-host "Cannot run tests as $nunitPath\nunit.console.clr4.exe is MISSING"
56+
}
57+
}
58+
59+
task UpdateAssemblyInfo {
60+
Update-AssemblyInfoFiles $version
61+
}
62+
63+
task RebuildDatabase {
64+
exec { & $AliaSQLPath\AliaSQL.exe Rebuild $databaseServer "$databaseName" "$databaseScripts\Scripts"}
65+
}
66+
67+
task UpdateDatabase {
68+
try{
69+
exec { & $AliaSQLPath\AliaSQL.exe Update $databaseServer "$databaseName" "$databaseScripts\Scripts"}
70+
}
71+
catch{
72+
write-host "Database does not exist - running rebuild"
73+
exec { & $AliaSQLPath\AliaSQL.exe Rebuild $databaseServer "$databaseName" "$databaseScripts\Scripts"}
74+
}
75+
}
76+
77+
task SeedDatabase {
78+
exec { & $AliaSQLPath\AliaSQL.exe Seed $databaseServer " $databaseName" "$databaseScripts\Scripts"}
79+
}
80+
81+
task Package -depends Compile {
82+
write-host "Clean package directory"
83+
delete_directory $package_dir
84+
85+
write-host "Copy newly compiled version of Database Deployer"
86+
copy_files "$base_dir\source\AliaSQL.Console\Bin\Release" "$package_dir\AliaSQL"
87+
88+
write-host "Copy in database scripts"
89+
copy_files "$databaseScripts\scripts" "$package_dir\database\"
90+
write-host "Copy AliaSQL tool so scripts can be ran"
91+
copy_files "$AliaSQLPath" "$package_dir\database\"
92+
write-host "Create batch files to run db updates"
93+
create-dbdeployscript "Update" "$package_dir\database\_Update-Database.bat"
94+
create-dbdeployscript "Rebuild" "$package_dir\database\_Rebuild-Database.bat"
95+
create-dbdeployscript "Baseline" "$package_dir\database\_Baseline-Database.bat"
96+
create-dbdeployscript "Seed" "$package_dir\database\_Seed-Database.bat"
97+
write-host "Zip it up"
98+
zip_directory $package_dir $package_file
99+
}
100+
101+
task NugetPack -depends Package {
102+
exec {
103+
& $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
104+
}
105+
exec {
106+
& $source_dir\.nuget\nuget.exe pack -Version $version -outputdirectory $build_dir $base_dir\nuget\AliaSQL.nuspec
107+
}
108+
exec {
109+
& $source_dir\.nuget\nuget.exe pack -Version $version -outputdirectory $build_dir $base_dir\nuget\AliaSQL.Kickstarter.nuspec
110+
}
111+
}
112+
113+
function global:zip_directory($directory,$file) {
114+
write-host "Zipping folder: " $test_assembly
115+
delete_file $file
116+
cd $directory
117+
& "$base_dir\lib\7zip\7za.exe" a -mx=9 -r $file
118+
cd $base_dir
119+
}
120+
121+
function global:copy_files($source,$destination,$exclude=@()){
122+
create_directory $destination
123+
Get-ChildItem $source -Recurse -Exclude $exclude -ErrorAction SilentlyContinue | Copy-Item -ErrorAction SilentlyContinue -Destination {Join-Path $destination $_.FullName.Substring($source.length)}
124+
}
125+
126+
function global:Copy_and_flatten ($source,$filter,$dest) {
127+
ls $source -filter $filter -r | Where-Object{!$_.FullName.Contains("$testCopyIgnorePath") -and !$_.FullName.Contains("packages") -and !$_.FullName.Contains("build") }| cp -dest $dest -force
128+
}
129+
130+
function global:copy_all_assemblies_for_test($destination){
131+
create_directory $destination
132+
Copy_and_flatten $source_dir *.exe $destination
133+
Copy_and_flatten $source_dir *.dll $destination
134+
Copy_and_flatten $source_dir *.config $destination
135+
Copy_and_flatten $source_dir *.xml $destination
136+
Copy_and_flatten $source_dir *.pdb $destination
137+
}
138+
139+
function global:delete_file($file) {
140+
if($file) { remove-item $file -force -ErrorAction SilentlyContinue | out-null }
141+
}
142+
143+
function global:delete_directory($directory_name)
144+
{
145+
rd $directory_name -recurse -force -ErrorAction SilentlyContinue | out-null
146+
}
147+
148+
function global:delete_files_in_dir($dir)
149+
{
150+
get-childitem $dir -recurse | foreach ($_) {remove-item $_.fullname}
151+
}
152+
153+
function global:create_directory($directory_name)
154+
{
155+
mkdir $directory_name -ErrorAction SilentlyContinue | out-null
156+
}
157+
158+
function create-dbdeployscript($verb, $filename)
159+
{
160+
"@echo off
161+
set /p serverName=""DB Server: "" %=%
162+
AliaSQL.exe $verb %serverName% $databaseName .
163+
pause" | out-file $filename -encoding "ASCII"
164+
}
165+
166+
167+
function Update-AssemblyInfoFiles ([string] $version, [System.Array] $excludes = $null) {
168+
169+
#-------------------------------------------------------------------------------
170+
# Update version numbers of AssemblyInfo.cs
171+
# adapted from: http://www.luisrocha.net/2009/11/setting-assembly-version-with-windows.html
172+
#-------------------------------------------------------------------------------
173+
174+
if ($version -notmatch "[0-9]+(\.([0-9]+|\*)){1,3}") {
175+
Write-Error "Version number incorrect format: $version"
176+
}
177+
178+
$versionPattern = 'AssemblyVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)'
179+
$versionAssembly = 'AssemblyVersion("' + $version + '")';
180+
$versionFilePattern = 'AssemblyFileVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)'
181+
$versionAssemblyFile = 'AssemblyFileVersion("' + $version + '")';
182+
183+
Get-ChildItem -r -filter AssemblyInfo.* | % {
184+
$filename = $_.fullname
185+
186+
$update_assembly_and_file = $true
187+
188+
# set an exclude flag where only AssemblyFileVersion is set
189+
if ($excludes -ne $null)
190+
{ $excludes | % { if ($filename -match $_) { $update_assembly_and_file = $false } } }
191+
192+
# see http://stackoverflow.com/questions/3057673/powershell-locking-file
193+
# I am getting really funky locking issues.
194+
# The code block below should be:
195+
# (get-content $filename) | % {$_ -replace $versionPattern, $version } | set-content $filename
196+
197+
$tmp = ($file + ".tmp")
198+
if (test-path ($tmp)) { remove-item $tmp }
199+
200+
if ($update_assembly_and_file) {
201+
(get-content $filename) | % {$_ -replace $versionFilePattern, $versionAssemblyFile } | % {$_ -replace $versionPattern, $versionAssembly } > $tmp
202+
write-host Updating file AssemblyInfo and AssemblyFileInfo: $filename --> $versionAssembly / $versionAssemblyFile
203+
} else {
204+
(get-content $filename) | % {$_ -replace $versionFilePattern, $versionAssemblyFile } > $tmp
205+
write-host Updating file AssemblyInfo only: $filename --> $versionAssemblyFile
206+
}
207+
208+
if (test-path ($filename)) { remove-item $filename }
209+
move-item $tmp $filename -force
210+
211+
}
212+
}
213+

images/AliaSQL-200x200.png

3.63 KB
Loading

images/AliaSQL.PNG

14.1 KB
Loading

images/AliaSQL.ico

102 KB
Binary file not shown.

lib/7zip/7-zip.chm

86.1 KB
Binary file not shown.

lib/7zip/7za.exe

524 KB
Binary file not shown.

0 commit comments

Comments
 (0)