Skip to content

Instantly share code, notes, and snippets.

View danielmackay's full-sized avatar

Daniel Mackay [SSW] danielmackay

View GitHub Profile
@danielmackay
danielmackay / connection-string
Last active July 29, 2024 11:24
Create an empty database on a fresh SQL Server on MacOS
Server=localhost,1600;Initial Catalog=$DB_NAME;Persist Security Info=False;User ID=sa;Password=yourStrong(!)Password;MultipleActiveResultSets=True;TrustServerCertificate=True;Connection Timeout=30;
services:
db:
environment:
ACCEPT_EULA: "Y"
SA_PASSWORD: "yourStrong(!)Password"
# mssql server image isn't available for arm64 architecture, so we use azure-sql instead
image: mcr.microsoft.com/azure-sql-edge:latest
# If you really want to use MS SQL Server, uncomment the following line
#image: mcr.microsoft.com/mssql/server
ports:
@danielmackay
danielmackay / ConfigurationExtensions.cs
Created May 22, 2024 11:33
ASP.NET Core - Connection String Configuration
public static class ConfigurationExtensions
{
public static string GetConnectionStringOrThrow(this IConfiguration configuration, string name)
{
var connectionString = configuration.GetConnectionString(name);
if (string.IsNullOrWhiteSpace(connectionString))
{
throw new InvalidOperationException($"Connection string '{name}' not found.");
}
@danielmackay
danielmackay / appsettings.json
Last active May 21, 2024 01:18
Docker Compose - SQL Server
{
// NOTE: If any of your connection strings real passwords, you should delete them and put them in the user secrets file instead.
"ConnectionStrings": {
"DefaultConnection": "Server=localhost,1500;Initial Catalog=CleanArchitecture;Persist Security Info=False;User ID=sa;Password=yourStrong(!)Password;MultipleActiveResultSets=True;TrustServerCertificate=True;Connection Timeout=30;"
}
}
@danielmackay
danielmackay / k6.js
Created May 20, 2024 10:15
K6 - Basic load test
// k6 run ./script.js
import http from 'k6/http';
import { sleep, check } from 'k6';
import { Rate } from 'k6/metrics';
export const errorRate = new Rate('errors');
export let options = {
insecureSkipTLSVerify: true,
@danielmackay
danielmackay / up.ps1
Created May 17, 2024 07:01
Up PowerShell Script - Docker Compose, DB Creation, Migration, and Seeding
# Originally Created by William Liebenberg
Param(
[switch]$skipDeploy = $false,
[string]$apiDbConnectionString = "Server=.,2100;Database=MoMo;User Id=sa;Password=Password!##3;MultipleActiveResultSets=true;TrustServerCertificate=True;"
)
$upScriptPath = $Script:MyInvocation.MyCommand.Path | Split-Path
$srcPath = Join-Path -Path $upScriptPath -ChildPath "src"
@danielmackay
danielmackay / MigrationExtensions.cs
Created December 28, 2023 04:09
Apply Migrations Extension Method
public static class MigrationExtensions
{
public static void ApplyMigrations(this IApplicationBuilder app)
{
using IServiceScope scope = app.ApplicationServices.CreateScope();
using ApplicationDbContext dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
dbContext.Database.Migrate();
}
}
@danielmackay
danielmackay / StringLengthGuard.cs
Created October 11, 2023 06:01
Guard Clause Custom Extension
public static class StringLengthGuard
{
public static string StringLength(this IGuardClause guardClause,
string input,
int maxLength,
[CallerArgumentExpression("input")] string? parameterName = null)
{
if (input?.Length > maxLength)
throw new ArgumentException($"Cannot exceed string length of {maxLength}", parameterName);
@danielmackay
danielmackay / WebAppBitBucketRestrictions.ps1
Created September 7, 2022 10:55
Powershell script that whitelists all bit bucket ip addresses so that pipelines can deploy to a locked down SCM website
param
(
[Parameter(Position = 0, Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$WebApp,
[Parameter(Position = 1, Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$ResourceGroup
@danielmackay
danielmackay / ContentReferenceExt.cs
Created June 24, 2019 01:16
Focal Point Implementation
public static class ContentReferenceExt
{
public static T ToContentType<T>(this ContentReference contentReference) where T : ContentData
{
if (contentReference.IsEmpty())
return null;
var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
return contentLoader.TryGet(contentReference, out T content) ? content : null;
}