Skip to content

Commit e76611b

Browse files
committed
Version 0.3 initial commit
0 parents  commit e76611b

File tree

88 files changed

+9005
-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.

88 files changed

+9005
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# ignore the settings folder and files for VSCode
2+
.vscode/*
3+
4+
# VIM backup files
5+
*~
6+
7+
# Local constant file
8+
tests/constants.local.ps1

README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
| master | development |
2+
|---|---|
3+
| [![Build status](https://ci.appveyor.com/api/projects/status/m0ml0392r631tp60/branch/master?svg=true)](https://ci.appveyor.com/project/nvarscar/powerup/branch/master) | [![Build status](https://ci.appveyor.com/api/projects/status/m0ml0392r631tp60/branch/development?svg=true)](https://ci.appveyor.com/project/nvarscar/powerup/branch/development) |
4+
5+
![dbops](https://nvarscar.github.io/powerup/img/dbops.jpg)
6+
# DBOps
7+
DBOps is a Powershell module that provides Continuous Integration/Continuous Deployment capabilities for SQL database deployments. In addition to easy-to-use deployment functions, it provides tracking functionality, ensuring that each script is deployed only once and in due order. It will also grant you with ability to organize scripts into builds and deploy them in a repeatable manner on top of any previously deployed version.
8+
9+
The deployment functionality of the module is provided by [DbUp](https://github.com/DbUp/DbUp) .Net library, which has proven its flexibility and reliability during deployments.
10+
11+
Currently supported RDBMS:
12+
* SQL Server
13+
* Oracle
14+
15+
## Features
16+
The most notable features of the module:
17+
18+
* No scripting experience required - the module is designed around usability and functionality
19+
* Introduces an option to aggregate source scripts from multiple sources into a single ready-to-deploy file
20+
* CI/CD pipelining functionality: builds, artifact management, deployment
21+
* Can detect new/changed files in your source code folder and generate a new deployment build based on those files
22+
* Introduces optional internal build system: older builds are kept inside the deployment package ensuring smooth and errorless deployments
23+
* Reliably deploys the scripts in a consistent manner - all the scripts are executed in alphabetical order one build at a time
24+
* Can be deployed without the module installed in the system - module itself is integrated into the deployment package
25+
* Transactionality of the deployments/migrations: every build can be deployed as a part of a single transaction, rolling back unsuccessful deployments
26+
* Dynamically change your code based on custom variables - use `#{customVarName}` tokens to define variables inside the scripts or execution parameters
27+
* Packages are fully compatible with Octopus Deploy deployments: all packages are in essence zip archives with Deploy.ps1 file that initiates deployment
28+
29+
30+
## System requirements
31+
32+
* Powershell 5.0 or higher
33+
34+
## Installation
35+
### Using git
36+
```powershell
37+
git clone https://github.com/sqlcollaborative/dbops.git dbops
38+
Import-Module .\dbops
39+
```
40+
Make sure to have the following modules installed as well:
41+
- [PSFramework](https://github.com/PowershellFrameworkCollective/psframework)
42+
- [ZipHelper](https://www.powershellgallery.com/packages/ziphelper) - only if you intend to run module tests
43+
44+
### Using PSGallery (Powershell 5+)
45+
```powershell
46+
Install-Module dbops
47+
```
48+
49+
## Usage scenarios
50+
51+
* Ad-hoc deployments of any scale without manual code execution
52+
* Delivering new version of the database schema in a consistent manner to multiple environments
53+
* Build/Test/Deploy scenarios inside the Continuous Integration/Continuous Delivery pipeline
54+
* Dynamic deployment based on modified files in the source folder
55+
56+
## Examples
57+
### Simple deployment
58+
```powershell
59+
# Quick deployment without tracking deployment history
60+
Invoke-DBODeployment -ScriptPath C:\temp\myscripts -SqlInstance server1 -Database MyDB -SchemaVersionTable $null
61+
```
62+
### Package management
63+
```powershell
64+
# Deployment using packages & builds with keeping track of deployment history in the SchemaVersions table
65+
New-DBOPackage Deploy.zip -ScriptPath C:\temp\myscripts | Install-DBOPackage -SqlInstance server1 -Database MyDB
66+
67+
# Create new deployment package with predefined configuration and deploy it replacing #{dbName} tokens with corresponding values
68+
New-DBOPackage -Path MyPackage.zip -ScriptPath .\Scripts -Configuration @{ Database = '#{dbName}'; ConnectionTimeout = 5 }
69+
Install-DBOPackage MyPackage.zip -Variables @{ dbName = 'myDB' }
70+
71+
# Adding builds to the package
72+
Add-DBOBuild Deploy.zip -ScriptPath .\myscripts -Type Unique -Build 2.0
73+
Get-ChildItem .\myscripts | Add-DBOBuild Deploy.zip -Type New,Modified -Build 3.0\
74+
75+
# Install package using internal script Deploy.ps1 - to use when module is not installed locally
76+
Expand-Archive Deploy.zip '.\MyTempFolder'
77+
.\MyTempFolder\Deploy.ps1 -SqlInstance server1 -Database MyDB
78+
```
79+
### Configurations and defaults
80+
```powershell
81+
# Setting deployment options within the package to be able to deploy it without specifying options
82+
Update-DBOConfig Deploy.zip -Configuration @{ DeploymentMethod = 'SingleTransaction'; SqlInstance = 'localhost'; DatabaseName = 'MyDb2' }
83+
Install-DBOPackage Deploy.zip
84+
85+
# Generating config files and using it later as a deployment template
86+
(Get-DBOConfig -Configuration @{ DeploymentMethod = 'SingleTransaction'; SqlInstance = 'devInstance'; DatabaseName = 'MyDB' }).SaveToFile('.\dev.json')
87+
(Get-DBOConfig -Path '.\dev.json' -Configuration @{ SqlInstance = 'prodInstance' }).SaveToFile('.\prod.json')
88+
Install-DBOPackage Deploy.zip -ConfigurationFile .\dev.json
89+
90+
# Invoke package deployment using custom connection string
91+
Install-DBOPackage -Path Deploy.zip -ConnectionString 'Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;'
92+
93+
# Invoke package deployment to an Oracle database OracleDB
94+
Install-DBOPackage -Path Deploy.zip -Server OracleDB -ConnectionType Oracle
95+
96+
# Get a list of all the default settings
97+
Get-DBODefaultSetting
98+
99+
# Change the default SchemaVersionTable setting to null, disabling the deployment logging by default
100+
Set-DBODefaultSetting -Name SchemaVersionTable -Value $null
101+
```
102+
### CI/CD features
103+
```powershell
104+
# Invoke CI/CD build of the package MyPackage.zip using scripts from the source folder .\Scripts
105+
# Each execution of the command will only pick up new files from the ScriptPath folder
106+
Invoke-DBOPackageCI -Path MyPackage.zip -ScriptPath .\Scripts -Version 1.0
107+
108+
# Store the package in a DBOps package repository in a folder \\data\repo
109+
Publish-DBOPackageArtifact -Path myPackage.zip -Repository \\data\repo
110+
111+
# Retrieve the latest package version from the repository and install it
112+
Get-DBOPackageArtifact -Path myPackage.zip -Repository \\data\repo | Install-DBOPackage -Server MyDBServer -Database MyDB
113+
114+
```
115+
116+
## Planned for future releases
117+
118+
* Code analysis: know what kind of code makes its way into the package. Will find hidden sysadmin grants, USE statements and other undesired statements
119+
* Support for other RDBMS (eventually, everything that DbUp libraries can talk with)
120+
* Integration with unit tests (tSQLt/Pester/...?)
121+
* Module for Ansible (right now can still be used as a powershell task)
122+
* Linux support
123+
* SQLCMD support
124+
* Deployments to multiple databases at once
125+
* Optional rollback scripts

appveyor.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# See http://www.appveyor.com/docs/appveyor-yml for many more options
2+
3+
build: false
4+
version: 0.1.{build}
5+
6+
# Set build info
7+
environment:
8+
environment: development
9+
version: 0.1.$(appveyor_build_number)
10+
11+
matrix:
12+
# - scenario: MSSQL
13+
# main_instance: localhost\SQL2017
14+
# services:
15+
# - mssql2017
16+
- scenario: ALL
17+
# main_instance: localhost\SQL2017
18+
19+
#Configure services
20+
services:
21+
- mssql2017
22+
23+
# Set alternative clone folder
24+
clone_folder: c:\github\dbops
25+
26+
before_test:
27+
# run preparation scripts
28+
- ps: .\Tests\appveyor.prep.ps1
29+
30+
test_script:
31+
# Test with native PS version
32+
- ps: .\Tests\appveyor.pester.ps1
33+
34+
# Collecting results
35+
- ps: .\Tests\appveyor.pester.ps1 -Finalize
36+
37+
after_test:
38+
- ps: .\Tests\appveyor.post.ps1
39+
40+
#on_finish:
41+
# - ps:

bin/dbup-core.dll

70.5 KB
Binary file not shown.

bin/dbup-oracle.dll

13.5 KB
Binary file not shown.

bin/dbup-sqlserver.dll

17 KB
Binary file not shown.

bin/deploy.ps1

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[CmdletBinding()]
2+
Param (
3+
[string]$SqlInstance,
4+
[string]$Database,
5+
[ValidateSet('SingleTransaction', 'TransactionPerScript', 'NoTransaction')]
6+
[string]$DeploymentMethod = 'NoTransaction',
7+
[int]$ConnectionTimeout,
8+
[switch]$Encrypt,
9+
[pscredential]$Credential,
10+
[string]$UserName,
11+
[securestring]$Password,
12+
[string]$LogToTable,
13+
[switch]$Silent,
14+
[hashtable]$Variables
15+
)
16+
17+
#Stop on error
18+
#$ErrorActionPreference = 'Stop'
19+
20+
#Import module
21+
If (Get-Module dbops) {
22+
Remove-Module dbops
23+
}
24+
Import-Module "$PSScriptRoot\Modules\dbops\dbops.psd1" -Force
25+
26+
#Invoke deployment using current parameters
27+
$params = $PSBoundParameters
28+
$params += @{ PackageFile = "$PSScriptRoot\dbops.package.json"}
29+
Invoke-DBODeployment @params
30+

dbops.psd1

7.73 KB
Binary file not shown.

dbops.psm1

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
Add-Type -AssemblyName System.IO.Compression
2+
Add-Type -AssemblyName System.IO.Compression.FileSystem
3+
$moduleCatalog = Get-Content "$PSScriptRoot\internal\json\dbops.json" -Raw | ConvertFrom-Json
4+
foreach ($bin in $moduleCatalog.Libraries) {
5+
Unblock-File -Path "$PSScriptRoot\$bin" -ErrorAction SilentlyContinue
6+
Add-Type -Path "$PSScriptRoot\$bin"
7+
}
8+
9+
foreach ($function in $moduleCatalog.Functions) {
10+
. "$PSScriptRoot\$function"
11+
}
12+
13+
foreach ($function in $moduleCatalog.Internal) {
14+
. "$PSScriptRoot\$function"
15+
}
16+
17+
# defining validations
18+
19+
Register-PSFConfigValidation -Name "transaction" -ScriptBlock {
20+
Param (
21+
$Value
22+
)
23+
24+
$Result = New-Object PSOBject -Property @{
25+
Success = $True
26+
Value = $null
27+
Message = ""
28+
}
29+
try {
30+
if (([string]$Value) -in @('SingleTransaction', 'TransactionPerScript', 'NoTransaction')) {
31+
$Result.Value = [string]$Value
32+
}
33+
else {
34+
$Result.Message = "Allowed values: SingleTransaction, TransactionPerScript, NoTransaction"
35+
$Result.Success = $False
36+
}
37+
}
38+
catch {
39+
$Result.Message = "Failed to convert value to string"
40+
$Result.Success = $False
41+
}
42+
43+
return $Result
44+
}
45+
46+
Register-PSFConfigValidation -Name "securestring" -ScriptBlock {
47+
Param (
48+
$Value
49+
)
50+
51+
$Result = New-Object PSOBject -Property @{
52+
Success = $True
53+
Value = $null
54+
Message = ""
55+
}
56+
if ($Value -is [securestring]) {
57+
$Result.Value = $Value
58+
}
59+
else {
60+
$Result.Message = 'Only [securestring] is accepted'
61+
$Result.Success = $False
62+
}
63+
return $Result
64+
}
65+
66+
Register-PSFConfigValidation -Name "hashtable" -ScriptBlock {
67+
Param (
68+
$Value
69+
)
70+
71+
$Result = New-Object PSOBject -Property @{
72+
Success = $True
73+
Value = $null
74+
Message = ""
75+
}
76+
try {
77+
if (([hashtable]$Value) -is [hashtable]) {
78+
$Result.Value = [hashtable]$Value
79+
}
80+
else {
81+
$Result.Message = "Only hashtables are allowed"
82+
$Result.Success = $False
83+
}
84+
}
85+
catch {
86+
$Result.Message = "Failed to convert value to hashtable. Only hashtables are allowed."
87+
$Result.Success = $False
88+
}
89+
return $Result
90+
}
91+
92+
# defining defaults
93+
94+
Set-PSFConfig -FullName dbops.ApplicationName -Value "dbops" -Initialize -Description "Application name in the connection string"
95+
Set-PSFConfig -FullName dbops.SqlInstance -Value "localhost" -Initialize -Description "Server to connect to"
96+
Set-PSFConfig -FullName dbops.Database -Value $null -Initialize -Description "Name of the database for deployment"
97+
Set-PSFConfig -FullName dbops.DeploymentMethod -Value 'NoTransaction' -Initialize -Validation transaction `
98+
-Description "Transactional behavior during deployment. Allowed values: SingleTransaction, TransactionPerScript, NoTransaction (default)"
99+
Set-PSFConfig -FullName dbops.Username -Value $null -Initialize -Description "Connection username"
100+
Set-PSFConfig -FullName dbops.Password -Value $null -Initialize -Validation securestring `
101+
-Description "Connection password. Only available to the same OS user, as it will be encrypted"
102+
Set-PSFConfig -FullName dbops.SchemaVersionTable -Value 'SchemaVersions' -Initialize -Description "Name of the table where the schema deployment history will be stored"
103+
Set-PSFConfig -FullName dbops.Schema -Value $null -Initialize -Description "Schema name to use by default. Not applicable to some RDBMS."
104+
Set-PSFConfig -FullName dbops.ConnectionTimeout -Value 30 -Initialize -Validation integerpositive -Description "Connection attempt timeout in seconds. 0 to wait indefinitely."
105+
Set-PSFConfig -FullName dbops.ExecutionTimeout -Value 0 -Initialize -Validation integerpositive -Description "Script execution timeout in seconds. 0 to wait indefinitely."
106+
Set-PSFConfig -FullName dbops.Encrypt -Value $false -Initialize -Validation bool -Description "Encrypt connection if supported by the driver."
107+
Set-PSFConfig -FullName dbops.Silent -Value $false -Initialize -Validation bool -Description "Silent execution with no output to the console."
108+
Set-PSFConfig -FullName dbops.Credential -Value $null -Initialize -Description "Database credentials to authenticate with."
109+
Set-PSFConfig -FullName dbops.Variables -Value $null -Initialize -Validation hashtable -Description "A hashtable with key/value pairs representing #{variables} that will be swapped during execution."
110+
111+
# defining aliases
112+
113+
New-Alias -Name Write-Message -Value Write-PSFMessage
114+
New-Alias -Name Stop-Function -Value Stop-PSFFunction

docs/_config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
theme: jekyll-theme-minimal

0 commit comments

Comments
 (0)