Skip to content

Instantly share code, notes, and snippets.

@boblogdk
Last active December 21, 2015 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boblogdk/6258085 to your computer and use it in GitHub Desktop.
Save boblogdk/6258085 to your computer and use it in GitHub Desktop.
Run directly from Visual Studio Console. Call it whatever you want We use to be able to versionize our SQL as well as our c# code, therefore we needed a simple and fast way to script the database without leaving visual studio. BE AWARE IT DOES NOT DROP YOUR DATABASE, BUT DO INDEED DROP TABLES for consistensy.
################################################################################################################################
# #
# Generates script of database tables only #
# #
# USAGE: ".\UmbracoCurrentDB.ps1" directly from Visual Studio Console :) #
# #
# ALERT: DOES NOT WORK OUTSIDE VISUAL STUDIO - As it depends on environment variables given by VisualStudio #
# #
################################################################################################################################
####### BEGIN : CONFIGURABLE OPTIONS #######
$server = "."
$database = "Nearmiss_Umbraco_605"
$schema = "dbo"
$output_path = ($(SolutionDir) +"\DBScripts\")
$fileName = "UmbracoCurrentDB.sql"
####### END : CONFIGURABLE OPTIONS #######
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null
$srv = New-Object ("Microsoft.SqlServer.Management.SMO.Server") ($server)
$db = New-Object ("Microsoft.SqlServer.Management.SMO.Database")
$tbl = New-Object ("Microsoft.SqlServer.Management.SMO.Table")
$scripter = New-Object ("Microsoft.SqlServer.Management.SMO.Scripter") ($server)
# Get the database and table objects
$db = $srv.Databases[$database]
$tbl = $db.tables | Where-object { $_.schema -eq $schema -and -not $_.IsSystemObject }
# Set scripter options to ensure schema and data is scripted currectly :)
$scripter.Options.ScriptSchema = $true
$scripter.Options.ScriptData = $true
$scripter.Options.IncludeIfNotExists = $true
$scripter.Options.IncludeDatabaseContext = $true
$scripter.Options.NoCommandTerminator = $false
$scripter.Options.ToFileOnly = $true
$scripter.Options.AppendToFile = $true
$scripter.Options.Indexes = $true
$scripter.Options.DriAll = $true
$scripter.Options.SchemaQualify = $true
$scripter.Options.SchemaQuaiflyForeignKeyReferences = $true
function CopyObjectsToFiles($objects, $outDir) {
if (-not (Test-Path $outDir)) {
[System.IO.Directory]::CreateDirectory($outDir)
}
if (Test-Path $outDir$fileName){
Remove-Item $outDir$fileName
}
$scripter.Options.FileName = $outDir + $fileName
foreach ($o in $objects) {
if ($o -ne $null) {
$schemaPrefix = ""
if ($o.Schema -ne $null -and $o.Schema -ne "") {
$schemaPrefix = $o.Schema + "."
}
Write-Host "Scripting Drop for" $o
#SCRIPT DROPS
$scripter.Options.ScriptDrops = $true
$scripter.EnumScript($o)
}
$scripter.Options.IncludeDatabaseContext = $false
}
foreach ($o in $objects) {
if ($o -ne $null) {
$schemaPrefix = ""
if ($o.Schema -ne $null -and $o.Schema -ne "") {
$schemaPrefix = $o.Schema + "."
}
Write-Host "Scripting Schema And Data for" $o
# SCRIPT CREATES
$scripter.Options.ScriptDrops = $false
$scripter.EnumScript($o)
}
}
}
# Output the scripts
CopyObjectsToFiles $tbl $output_path
Write-Host "All Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment