Skip to content

Instantly share code, notes, and snippets.

@LitKnd
Last active October 28, 2019 22:35
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 LitKnd/378384ddec4117aecc10178ae62444a6 to your computer and use it in GitHub Desktop.
Save LitKnd/378384ddec4117aecc10178ae62444a6 to your computer and use it in GitHub Desktop.
<#
More examples of PowerShell commands for Continuous Integration:
https://documentation.red-gate.com/sca3/automating-database-changes/continuous-integration
#>
param
(
[string]$project = 'C:\git\StackOverflow\StackOverflow',
[string]$packageVersion = '0.0.001',
[string]$packageID = 'SO',
[string]$artifactPath = 'C:\DBAutomation\BuildArtifacts\SO',
[string]$buildSQLServer = '(localdb)\MSSQLLocalDB', # You can also use: .\DEV
[string]$targetSQLServer = '.\DEV',
[string]$targetDatabase = 'SOExampleDeploy'
)
# Invoke the build
# Note, using -Verbose for demo
$validatedProject = $project | Invoke-DatabaseBuild -TemporaryDatabaseServer "Data Source =$buildSQLServer" -Verbose
# Let's automate some documentation with SQL Doc, for fun
$documentation = $validatedProject | New-DatabaseDocumentation
# Create a database build artifact (and include the doc)
$buildArtifact = $validatedProject | New-DatabaseBuildArtifact -PackageId $packageID -PackageVersion $packageVersion -Documentation $documentation
# Export the build artifact
$buildArtifact | Export-DatabaseBuildArtifact -Path "$artifactPath"
# We need to connect to the database we plan to deploy to to compare our build with its current state
$targetConnection = New-DatabaseConnection -ServerInstance $targetSQLServer -Database $targetDatabase
# Create the release artifact
$releaseArtifact = New-DatabaseReleaseArtifact -Source $buildArtifact -Target $targetConnection -SQLCompareOptions $sqlCompareOptions
# Export it to the file system for posterity
$releaseArtifact | Export-DatabaseReleaseArtifact -Path "$artifactPath\$targetDatabase\$packageID.$packageVersion\"
# Deploy
Import-DatabaseReleaseArtifact -Path "$artifactPath\$targetDatabase\$packageID.$packageVersion\" | Use-DatabaseReleaseArtifact -DeployTo $targetConnection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment