Skip to content

Instantly share code, notes, and snippets.

View Alex-Yates's full-sized avatar

Alex Yates (Personal) Alex-Yates

View GitHub Profile
-- USE Chinook_PROD;
USE Chinook_Dev
SELECT COUNT(*)
FROM dbo.customer
JOIN dbo.Invoice
ON Customer.CustomerId = Invoice.CustomerId;
SELECT TOP 10
FirstName,
@Alex-Yates
Alex-Yates / Build-TestDatabase.ps1
Last active June 24, 2026 20:16
Creates a T-SQL script to build a test database, based on a Redgate Test Data Manager classification file. The test database will contain all the tables and columns referenced in the classification file. All columns will be of type NVARCHAR. The test database will not contain any data, keys, indexes, constraints etc. Just simple tables and columns.
<#
.SYNOPSIS
Generates a T-SQL script that recreates a database's schema (empty tables)
from a TDM classification.json file, for local pipeline testing.
.DESCRIPTION
The classification file lists every schema, table and column, plus (for string
columns) a maxLength. It does NOT contain real SQL data types, so every column
is created as NVARCHAR:
* maxLength 1..4000 -> NVARCHAR(maxLength)
# UPDATE ME!!!!
$rgCloneEndpoint = "https://update-me:8132/"
# DON'T CHANGE ANYTHING ELSE
###############################################################
<#
For more information, see:
https://documentation.red-gate.com/redgate-clone/using-the-cli/cli-installation
#>
$testMode = $true # By default, don't actually delete anything. Just print out the commands you would execute without executing them. Set this to false if you want to actually delete stuff
$images = (rgclone get di --output json) | ConvertFrom-Json
$failedImageIds = ($images | Where-Object {$_.status -like "Failed"}).Id
$containers = (rgclone get dc --output json) | ConvertFrom-Json
$failedContainerIds = ($containers | Where-Object {$_.status -like "Failed"}).Id
if ($failedImageIds.length -ne 0){
$failedImageIds | ForEach-Object {
sourceType: Backup
name: pagila-pg
engine: postgresql
version: 14
backups:
- file: pagila-pg/dump-pagila_dev.sql
- file: pagila-pg/dump-pagila_shadow.sql
initialDatabaseName: pagila
postScript: |
CREATE ROLE redgate WITH LOGIN SUPERUSER PASSWORD 'not_the_real_password';
:: DO NOT BLINDLY RUN THIS FILE!!!!
:: It won't work.
:: Also, it's important you understand what each bit does.
:: Instead, step through it, one line at a time, reading the comments (::) as you go.
::
:: STEP 0 - Download the rgclone command line
:: It's available here. (NOTE: You need to provide your own rgclone URL and port!):
:: https://rgclone-url.com:port/cloning-api/download/cli/windows-amd64
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- If you want to use a different name for the table, update this variable
DECLARE @TABLENAME AS NVARCHAR(100)=N'flyway_schema_history';
@Alex-Yates
Alex-Yates / Set-SqlDefaultPath.ps1
Created September 10, 2021 16:53
Set-SqlDefaultPath.ps1
function Set-SqlDefaultPath {
param (
[Parameter(Mandatory=$true)][string]$SqlInstance,
$NewDefaultDataPath = "",
$NewDefaultLogPath = "",
$NewDefaultBackupPath = ""
)
try {
import-module dbatools
}
Session type:
Lightning Talk
Title:
Dante's 9 Levels of Database Deployment Hell
Abstract:
Got a monolithic database with a zillion wicked dependencies?
Do database deployments send fear down your spine?
Have a dark sense of adventure?
-- Attribution to Ivan Donev
-- Details:
-- http://www.mssqlinsider.com/2013/09/check-databases-part-replication/
-- Requires SA
IF IS_SRVROLEMEMBER ('sysadmin') = 1
print 'Current user''s login is a member of the sysadmin role'
ELSE IF IS_SRVROLEMEMBER ('sysadmin') = 0
THROW 50001, 'ERROR: Current user''s login is NOT a member of the sysadmin role.', 1
ELSE IF IS_SRVROLEMEMBER ('sysadmin') IS NULL