Skip to content

Instantly share code, notes, and snippets.

@jasonmitchell
Last active December 10, 2018 21:21
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 jasonmitchell/d0106b7ae336d8bf9e7cbb2d8ec73865 to your computer and use it in GitHub Desktop.
Save jasonmitchell/d0106b7ae336d8bf9e7cbb2d8ec73865 to your computer and use it in GitHub Desktop.
param (
$projection,
$EventStoreIP,
$EventStoreHttpPort
)
."$PSScriptRoot\Api\Projections.ps1"
."$PSScriptRoot\Api\Streams.ps1"
$ErrorActionPreference = "Stop"
function Get-ProjectionVersion {
param ($projection)
$checkpoint = Get-LatestProjectionCheckpoint -projection $projection -eventStoreIp $EventStoreIP -eventStoreHttpPort $EventStoreHttpPort
$projectionVersion = $checkpoint.entries.metadata | ConvertFrom-Json | select -ExpandProperty `$v
Write-Host "Projection $projection has current version of $projectionVersion"
return $projectionVersion
}
function Get-ProjectionPosition {
param ($projection)
Write-Host "Getting latest $projection position..."
$rawPosition = Get-Projection -projection $projection -eventStoreIp $EventStoreIP -eventStoreHttpPort $EventStoreHttpPort `
| select -ExpandProperty position
$parts = $rawPosition -split '/';
$c = [convert]::ToInt64(($parts[0] -split ':' )[1])
$p = [convert]::ToInt64(($parts[1] -split ':' )[1])
Write-Host "Projection position: C:$c/P:$p"
$position = @{
C = $c;
P = $p;
}
return $position
}
function Get-LatestProjectionCheckpoint {
param (
$projection,
$eventStoreIp,
$eventStoreHttpPort,
$user = "admin",
$password = "changeit"
)
Write-Host "Getting checkpoint for projection $projection..."
$uri = "http://${eventStoreIp}:${eventStoreHttpPort}/streams/`$projections-$projection-checkpoint/head/backward/1?embed=body"
$result = Invoke-WebRequest -Uri $uri -UseBasicParsing -Headers @{
Accept = "application/vnd.eventstore.atom+json";
Authorization = (Get-BasicAuthCredentials -user $user -password $password)
}
$json = [System.Text.Encoding]::UTF8.GetString($result.Content) | ConvertFrom-JSON
return $json
}
function Write-ProjectionCheckpoint {
param (
$projection,
$eventStoreIp,
$eventStoreHttpPort,
$commitPosition,
$preparePosition,
$version,
$user = "admin",
$password = "changeit"
)
Write-Host "Writing $projection checkpoint at C:$commitPosition/P:$preparePosition with version $version"
$checkpoint = @{
eventId = [guid]::NewGuid();
eventType = "`$ProjectionCheckpoint";
data = @();
metadata = @{}
}
$checkpoint.metadata.Add("`$v", $version)
$checkpoint.metadata.Add("`$c", $commitPosition)
$checkpoint.metadata.Add("`$p", $preparePosition)
$events = @($checkpoint)
$result = Write-Events -stream "`$projections-$projection-checkpoint" -events $events -eventStoreIp $eventStoreIp -eventStoreHttpPort $eventStoreHttpPort -user $user -password $password
return $result
}
Stop-Projection -projection $projection -eventStoreIp $EventStoreIP -eventStoreHttpPort $EventStoreHttpPort | out-null
$version = Get-ProjectionVersion -projection $projection
$position = Get-ProjectionPosition -projection $projection
Write-ProjectionCheckpoint -projection $projection -eventStoreIp $EventStoreIP -eventStoreHttpPort $EventStoreHttpPort -commitPosition $position.C -preparePosition $position.P -version $version | out-null
Start-Projection -projection $projection -eventStoreIp $EventStoreIP -eventStoreHttpPort $EventStoreHttpPort | out-null
Write-Host "Finished fix"
function Get-BasicAuthCredentials {
param (
$user = "admin",
$password = "changeit"
)
$basicAuthPair = "${user}:${password}"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($basicAuthPair))
$basicAuthValue = "Basic $encodedCreds"
return $basicAuthValue
}
."$PSScriptRoot\Auth.ps1"
function Stop-Projection {
param (
$projection,
$eventStoreIp,
$eventStoreHttpPort,
$user = "admin",
$password = "changeit"
)
Write-Host "Stopping $projection projection..."
$uri = "http://${eventStoreIp}:${eventStoreHttpPort}/projection/${projection}/command/disable"
$result = Invoke-WebRequest -Method Post -Uri $uri -UseBasicParsing -Headers @{
Authorization = (Get-BasicAuthCredentials -user $user -password $password)
}
return $result
}
function Start-Projection {
param (
$projection,
$eventStoreIp,
$eventStoreHttpPort,
$user = "admin",
$password = "changeit"
)
Write-Host "Starting $projection projection..."
$uri = "http://${eventStoreIp}:${eventStoreHttpPort}/projection/${projection}/command/enable"
$result = Invoke-WebRequest -Method Post -Uri $uri -UseBasicParsing -Headers @{
Authorization = (Get-BasicAuthCredentials -user $user -password $password)
}
return $result
}
function Get-Projection {
param (
$projection,
$eventStoreIp,
$eventStoreHttpPort
)
$uri = "http://${eventStoreIp}:${eventStoreHttpPort}/projection/$projection"
$result = Invoke-WebRequest -Uri $uri -UseBasicParsing -Headers @{
Accept = "application/json";
}
$json = $result.Content | ConvertFrom-JSON
return $json
}
function Get-ProjectionResult {
param (
$projection,
$eventStoreIp,
$eventStoreHttpPort
)
Write-Host "Getting result of projection $projection..."
$uri = "http://${eventStoreIp}:${eventStoreHttpPort}/projection/$projection/result"
$result = Invoke-WebRequest -Uri $uri -UseBasicParsing -Headers @{
Accept = "application/json";
}
return $result.Content
}
function New-TransientProjection {
param (
$name,
$query,
$eventStoreIp,
$eventStoreHttpPort,
$user = "admin",
$password = "changeit"
)
$uri = "http://${eventStoreIp}:${eventStoreHttpPort}/projections/transient?name=${name}&type=js&enabled=true"
$result = Invoke-WebRequest -Method Post -Body $query -Uri $uri -UseBasicParsing -Headers @{
Authorization = (Get-BasicAuthCredentials -user $user -password $password)
}
return $result
}
."$PSScriptRoot\Auth.ps1"
function Write-Events {
param (
$stream,
$events,
$eventStoreIp,
$eventStoreHttpPort,
$user = "admin",
$password = "changeit"
)
$body = ConvertTo-Json $events
$uri = "http://${eventStoreIp}:${eventStoreHttpPort}/streams/$stream"
$result = Invoke-WebRequest -Method Post -Uri $uri -UseBasicParsing -Body $body -ContentType "application/vnd.eventstore.events+json" -Headers @{
Authorization = (Get-BasicAuthCredentials -user $user -password $password)
}
return $result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment