Skip to content

Commit

Permalink
Merge pull request #7 from max-ieremenko/release/2.2
Browse files Browse the repository at this point in the history
Release/2.2
  • Loading branch information
max-ieremenko committed Mar 11, 2021
2 parents 8da786a + 96687f5 commit 6eec05b
Show file tree
Hide file tree
Showing 90 changed files with 2,277 additions and 184 deletions.
15 changes: 13 additions & 2 deletions Build/build-scripts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function Test-PowerShellCore($image) {
--env connectionString=$connectionString `
--env test=/test `
$image `
pwsh -Command ./test/Test.ps1
pwsh -Command ./test/TestPowerShell.ps1
}
}

Expand Down Expand Up @@ -82,7 +82,7 @@ function Test-GlobalTool($image) {
}
}

function Test-NetCore($targetFramework, $image) {
function Test-NetCoreLinux($targetFramework, $image) {
$bin = Join-Path $binDir "SqlDatabase\$targetFramework\publish"
$app = $bin + ":/app"
$test = $moduleIntegrationTests + ":/test"
Expand All @@ -99,6 +99,17 @@ function Test-NetCore($targetFramework, $image) {
}
}

function Test-NetCore($targetFramework, $image) {
$bin = Join-Path $binDir "SqlDatabase\$targetFramework\publish"
$script = Join-Path $moduleIntegrationTests "Test.ps1"

$builder = New-Object -TypeName System.Data.SqlClient.SqlConnectionStringBuilder -ArgumentList $connectionString
$builder["Data Source"] = "."
$cs = $builder.ToString()

& $script $bin $cs
}

function Test-Unit($targetFramework) {
$sourceDir = Join-Path $binDir "Tests"
$sourceDir = Join-Path $sourceDir $targetFramework
Expand Down
38 changes: 30 additions & 8 deletions Build/build-tasks.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Task UnitTest -Depends InitializeTests `
, UnitTestcore22 `
, UnitTestcore31 `
, UnitTest50

Task Test -Depends InitializeTests `
, TestPublishModule `
, TestPowerShellDesktop `
Expand All @@ -23,9 +24,13 @@ Task Test -Depends InitializeTests `
, TestPowerShellCore703 `
, TestPowerShellCore710 `
, TestPowerShellCore720 `
, TestPowerShellCore712 `
, TestGlobalTool22 `
, TestGlobalTool31 `
, TestGlobalTool50 `
, TestNetCoreLinux22 `
, TestNetCoreLinux31 `
, TestNetLinux50 `
, TestNetCore22 `
, TestNetCore31 `
, TestNet50
Expand Down Expand Up @@ -77,6 +82,7 @@ Task Build {
New-Item -Path $net45Dest -ItemType Directory
Copy-Item -Path (Join-Path $net45Source "SqlDatabase.exe") -Destination $net45Dest
Copy-Item -Path (Join-Path $net45Source "SqlDatabase.pdb") -Destination $net45Dest
Copy-Item -Path (Join-Path $net45Source "System.Management.Automation.dll") -Destination $net45Dest
}

Task PackGlobalTool {
Expand Down Expand Up @@ -234,6 +240,10 @@ Task TestPowerShellCore710 {
Test-PowerShellCore "mcr.microsoft.com/powershell:7.1.0-ubuntu-18.04"
}

Task TestPowerShellCore712 {
Test-PowerShellCore "mcr.microsoft.com/powershell:7.1.2-ubuntu-20.04"
}

Task TestPowerShellCore720 {
Test-PowerShellCore "mcr.microsoft.com/powershell:7.2.0-preview.2-ubuntu-20.04"
}
Expand All @@ -251,31 +261,43 @@ Task TestPowerShellDesktop {
$builder["Data Source"] = "."
$env:connectionString = $builder.ToString()

$testScript = Join-Path $moduleIntegrationTests "Test.ps1"
$testScript = Join-Path $moduleIntegrationTests "TestPowerShell.ps1"

Test-PowerShellDesktop ". $testScript"
}

Task TestGlobalTool22 {
Test-GlobalTool "microsoft/dotnet:2.2-sdk"
Test-GlobalTool "sqldatabase/dotnet_pwsh:2.2-sdk"
}

Task TestGlobalTool31 {
Test-GlobalTool "mcr.microsoft.com/dotnet/core/sdk:3.1"
Test-GlobalTool "sqldatabase/dotnet_pwsh:3.1-sdk"
}

Task TestGlobalTool50 {
Test-GlobalTool "mcr.microsoft.com/dotnet/sdk:5.0"
Test-GlobalTool "sqldatabase/dotnet_pwsh:5.0-sdk"
}

Task TestNetCoreLinux22 {
Test-NetCoreLinux "netcoreapp2.2" "sqldatabase/dotnet_pwsh:2.2-runtime"
}

Task TestNetCoreLinux31 {
Test-NetCoreLinux "netcoreapp3.1" "sqldatabase/dotnet_pwsh:3.1-runtime"
}

Task TestNetLinux50 {
Test-NetCoreLinux "net5.0" "sqldatabase/dotnet_pwsh:5.0-runtime"
}

Task TestNetCore22 {
Test-NetCore "netcoreapp2.2" "microsoft/dotnet:2.2-runtime"
Test-NetCore "netcoreapp2.2" "sqldatabase/dotnet_pwsh:2.2-runtime"
}

Task TestNetCore31 {
Test-NetCore "netcoreapp3.1" "mcr.microsoft.com/dotnet/core/runtime:3.1"
Test-NetCore "netcoreapp3.1" "sqldatabase/dotnet_pwsh:3.1-runtime"
}

Task TestNet50 {
Test-NetCore "net5.0" "mcr.microsoft.com/dotnet/runtime:5.0"
}
Test-NetCore "net5.0" "sqldatabase/dotnet_pwsh:5.0-runtime"
}
60 changes: 60 additions & 0 deletions Build/create-images-tasks.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Task default -Depends BuildDotnetSdk22 `
, BuildDotnetRuntime22 `
, BuildDotnetSdk31 `
, BuildDotnetRuntime31 `
, BuildDotnetSdk50 `
, BuildDotnetRuntime50

Task BuildDotnetSdk22 {
Exec {
docker build `
-f dotnet-sdk-2.2.dockerfile `
-t sqldatabase/dotnet_pwsh:2.2-sdk `
.
}
}

Task BuildDotnetRuntime22 {
Exec {
docker build `
-f dotnet-runtime-2.2.dockerfile `
-t sqldatabase/dotnet_pwsh:2.2-runtime `
.
}
}

Task BuildDotnetSdk31 {
Exec {
docker build `
-f dotnet-sdk-3.1.dockerfile `
-t sqldatabase/dotnet_pwsh:3.1-sdk `
.
}
}

Task BuildDotnetRuntime31 {
Exec {
docker build `
-f dotnet-runtime-3.1.dockerfile `
-t sqldatabase/dotnet_pwsh:3.1-runtime `
.
}
}

Task BuildDotnetSdk50 {
Exec {
docker build `
-f dotnet-sdk-5.0.dockerfile `
-t sqldatabase/dotnet_pwsh:5.0-sdk `
.
}
}

Task BuildDotnetRuntime50 {
Exec {
docker build `
-f dotnet-runtime-5.0.dockerfile `
-t sqldatabase/dotnet_pwsh:5.0-runtime `
.
}
}
5 changes: 5 additions & 0 deletions Build/create-images.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#Install-Module -Name psake
#Requires -Modules @{ModuleName='psake'; RequiredVersion='4.9.0'}

$psakeMain = Join-Path $PSScriptRoot "create-images-tasks.ps1"
Invoke-psake $psakeMain
6 changes: 6 additions & 0 deletions Build/dotnet-runtime-2.2.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM microsoft/dotnet:2.2-runtime

RUN curl -L https://github.com/PowerShell/PowerShell/releases/download/v6.2.7/powershell_6.2.7-1.debian.9_amd64.deb --output powershell_6.2.7-1.debian.9_amd64.deb && \
dpkg -i powershell_6.2.7-1.debian.9_amd64.deb && \
apt-get install -f && \
rm -f powershell_6.2.7-1.debian.9_amd64.deb
8 changes: 8 additions & 0 deletions Build/dotnet-runtime-3.1.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM mcr.microsoft.com/dotnet/core/runtime:3.1

RUN apt-get update && \
apt-get install -y liblttng-ust0 && \
curl -L https://github.com/PowerShell/PowerShell/releases/download/v7.0.5/powershell_7.0.5-1.debian.10_amd64.deb --output powershell_7.0.5-1.debian.10_amd64.deb && \
dpkg -i powershell_7.0.5-1.debian.10_amd64.deb && \
apt-get install -f && \
rm -f powershell_7.0.5-1.debian.10_amd64.deb
8 changes: 8 additions & 0 deletions Build/dotnet-runtime-5.0.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM mcr.microsoft.com/dotnet/runtime:5.0

RUN apt-get update && \
apt-get install -y liblttng-ust0 curl && \
curl -L https://github.com/PowerShell/PowerShell/releases/download/v7.1.2/powershell_7.1.2-1.debian.10_amd64.deb --output powershell_7.1.2-1.debian.10_amd64.deb && \
dpkg -i powershell_7.1.2-1.debian.10_amd64.deb && \
apt-get install -f && \
rm -f powershell_7.1.2-1.debian.10_amd64.deb
6 changes: 6 additions & 0 deletions Build/dotnet-sdk-2.2.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM microsoft/dotnet:2.2-sdk

RUN curl -L https://github.com/PowerShell/PowerShell/releases/download/v6.2.7/powershell_6.2.7-1.debian.9_amd64.deb --output powershell_6.2.7-1.debian.9_amd64.deb && \
dpkg -i powershell_6.2.7-1.debian.9_amd64.deb && \
apt-get install -f && \
rm -f powershell_6.2.7-1.debian.9_amd64.deb
8 changes: 8 additions & 0 deletions Build/dotnet-sdk-3.1.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM mcr.microsoft.com/dotnet/core/sdk:3.1

RUN apt-get update && \
apt-get install -y liblttng-ust0 && \
curl -L https://github.com/PowerShell/PowerShell/releases/download/v7.0.5/powershell_7.0.5-1.debian.10_amd64.deb --output powershell_7.0.5-1.debian.10_amd64.deb && \
dpkg -i powershell_7.0.5-1.debian.10_amd64.deb && \
apt-get install -f && \
rm -f powershell_7.0.5-1.debian.10_amd64.deb
8 changes: 8 additions & 0 deletions Build/dotnet-sdk-5.0.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM mcr.microsoft.com/dotnet/sdk:5.0

RUN apt-get update && \
apt-get install -y liblttng-ust0 && \
curl -L https://github.com/PowerShell/PowerShell/releases/download/v7.1.2/powershell_7.1.2-1.debian.10_amd64.deb --output powershell_7.1.2-1.debian.10_amd64.deb && \
dpkg -i powershell_7.1.2-1.debian.10_amd64.deb && \
apt-get install -f && \
rm -f powershell_7.1.2-1.debian.10_amd64.deb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SolutionScripts" Version="1.2.0" />
<PackageReference Include="SqlDatabase" Version="2.1.2" />
<PackageReference Include="SqlDatabase" Version="2.2.0" />
</ItemGroup>

</Project>
24 changes: 24 additions & 0 deletions Examples/PowerShellScript/example.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[CmdletBinding(SupportsShouldProcess=$true)] # indicates that the script implementation supports -WhatIf scenario
param (
$Command, # instance of SqlCommand, $null in case -WhatIf
$Variables # access to variables
)

if (-not $Variables.TableName) {
throw "Variable TableName is not defined."
}

if ($WhatIfPreference) {
# handle -WhatIf scenario
return
}

Write-Information "start execution"

$Command.CommandText = ("print 'current database name is {0}'" -f $Variables.DatabaseName)
$Command.ExecuteNonQuery()

$Command.CommandText = ("drop table {0}" -f $Variables.TableName)
$Command.ExecuteNonQuery()

Write-Information "finish execution"
101 changes: 101 additions & 0 deletions Examples/PowerShellScript/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
.ps1 script
==========================================

SqlDatabase supports powershell scripts for commands [execute](https://github.com/max-ieremenko/SqlDatabase/tree/master/Examples/ExecuteScriptsFolder), [create](https://github.com/max-ieremenko/SqlDatabase/tree/master/Examples/CreateDatabaseFolder) and [upgrade](https://github.com/max-ieremenko/SqlDatabase/tree/master/Examples/MigrationStepsFolder).

Example:

```bash
$ SqlDatabase execute ^
"-database=Data Source=server;Initial Catalog=database;Integrated Security=True" ^
-from=c:\script.ps1

PS> Execute-SqlDatabase `
-database "Data Source=server;Initial Catalog=database;Integrated Security=True" `
-from c:\script.ps1 `
-InformationAction Continue
```

script.ps1:

```powershell
[CmdletBinding(SupportsShouldProcess=$true)] # indicates that the script implementation supports -WhatIf scenario
param (
$Command, # instance of SqlCommand, $null in case -WhatIf
$Variables # access to variables
)
if (-not $Variables.TableName) {
throw "Variable TableName is not defined."
}
if ($WhatIfPreference) {
# handle -WhatIf scenario
return
}
Write-Information "start execution"
$Command.CommandText = ("print 'current database name is {0}'" -f $Variables.DatabaseName)
$Command.ExecuteNonQuery()
$Command.CommandText = ("drop table {0}" -f $Variables.TableName)
$Command.ExecuteNonQuery()
Write-Information "finish execution"
```

use

* cmdlet parameter binding
* parameter `$Command` to affect database
* parameter `$Variables` to access variables
* `Write-*` to write something into output/log
* `SupportsShouldProcess=$true` and `$WhatIfPreference` if script supports `-WhatIf` scenario

## Which version of PowerShell is used to run .ps1

### SqlDatabase powershell module

[![PowerShell Gallery](https://img.shields.io/powershellgallery/v/SqlDatabase.svg?style=flat-square)](https://www.powershellgallery.com/packages/SqlDatabase)

The version with which you run the module.

### .net framework 4.5.2

[![NuGet](https://img.shields.io/nuget/v/SqlDatabase.svg?style=flat-square&label=nuget%20net%204.5.2)](https://www.nuget.org/packages/SqlDatabase/)

Installed Powershell Desktop version.

### .net SDK tool for .net 5.0 or .net core 2.2/3.1

[![NuGet](https://img.shields.io/nuget/v/SqlDatabase.GlobalTool.svg?style=flat-square&label=nuget%20dotnet%20tool)](https://www.nuget.org/packages/SqlDatabase.GlobalTool/)

Pre-installed Powershell Core is required, will be used by SqlDatabase as external component. Due to Powershell Core design,

* SqlDatabase .net 5.0 can host Powershell Core versions below 7.2
* .net core 3.1 below 7.1
* .net core 2.2 below 7.0

PowerShell location can be passed via command line:

```bash
$ SqlDatabase execute ^
-usePowerShell=C:\Program Files\PowerShell\7
$ dotnet SqlDatabase.dll create ^
-usePowerShell=/opt/microsoft/powershell/7
```

PowerShell location by default:

* if SqlDatabase is running by PowerShell (parent process is PowerShell) and version is compatible, use this version
* check well-known installation folders: `C:\Program Files\PowerShell` on windows and `/opt/microsoft/powershell` on linux, use latest compatible version

## Scripts isolation

For each .ps1 script, executing by SqlDatabase

* create new PowerShell session with default cmdlets, providers, built-in functions, aliases etc.
* run script as `script block`
* destroy the session
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ Scripts
-------

- *.sql* a text file with Sql Server scripts
- *.ps1* a text file with PowerShell script, details are [here](Examples/PowerShellScript)
- *.dll* or *.exe* an .NET assembly with a script implementation, details are [here](Examples/CSharpMirationStep)

[Back to ToC](#table-of-contents)
Expand Down
Binary file not shown.
Loading

0 comments on commit 6eec05b

Please sign in to comment.