-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-database.ps1
More file actions
111 lines (102 loc) · 4.11 KB
/
Copy pathsetup-database.ps1
File metadata and controls
111 lines (102 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env pwsh
# Sets up LocalDB with EfcptSampleDb for the database-first SQL generation sample
$ErrorActionPreference = "Stop"
Write-Host "Setting up LocalDB with EfcptSampleDb..." -ForegroundColor Cyan
# Configuration
$instanceName = "mssqllocaldb"
$databaseName = "EfcptSampleDb"
$scriptDir = $PSScriptRoot
# Step 1: Check LocalDB installation
Write-Host "`n[1/5] Checking LocalDB installation..." -ForegroundColor Yellow
try {
$null = sqllocaldb info 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Error "LocalDB is not installed. Please install SQL Server LocalDB"
exit 1
}
Write-Host " [OK] LocalDB is installed" -ForegroundColor Green
}
catch {
Write-Error "Failed to check LocalDB installation: $_"
exit 1
}
# Step 2: Create or start LocalDB instance
Write-Host "`n[2/5] Setting up LocalDB instance '$instanceName'..." -ForegroundColor Yellow
$instances = sqllocaldb info
if ($instances -contains $instanceName) {
Write-Host " [OK] Instance '$instanceName' exists" -ForegroundColor Green
$state = sqllocaldb info $instanceName | Select-String "State:"
if ($state -match "Stopped") {
sqllocaldb start $instanceName | Out-Null
if ($LASTEXITCODE -eq 0) {
Write-Host " [OK] Instance started" -ForegroundColor Green
}
}
else {
Write-Host " [OK] Instance is running" -ForegroundColor Green
}
}
else {
sqllocaldb create $instanceName | Out-Null
sqllocaldb start $instanceName | Out-Null
Write-Host " [OK] Instance created and started" -ForegroundColor Green
}
# Step 3: Create database
Write-Host "`n[3/5] Creating database '$databaseName'..." -ForegroundColor Yellow
$createDbQuery = "IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = N'$databaseName') CREATE DATABASE [$databaseName]"
sqlcmd -S "(localdb)\$instanceName" -Q $createDbQuery -b | Out-Null
if ($LASTEXITCODE -eq 0) {
Write-Host " [OK] Database ready" -ForegroundColor Green
}
else {
Write-Error "Failed to create database"
exit 1
}
# Step 4: Create schema
Write-Host "`n[4/5] Creating sample schema..." -ForegroundColor Yellow
$schemaFile = Join-Path $scriptDir "schema.sql"
sqlcmd -S "(localdb)\$instanceName" -d $databaseName -i $schemaFile -b | Out-Null
if ($LASTEXITCODE -eq 0) {
Write-Host " [OK] Tables created" -ForegroundColor Green
}
else {
Write-Error "Failed to create schema"
exit 1
}
# Step 5: Insert sample data
Write-Host "`n[5/5] Inserting sample data..." -ForegroundColor Yellow
$dataFile = Join-Path $scriptDir "data.sql"
sqlcmd -S "(localdb)\$instanceName" -d $databaseName -i $dataFile -b | Out-Null
if ($LASTEXITCODE -eq 0) {
Write-Host " [OK] Sample data inserted" -ForegroundColor Green
}
else {
Write-Error "Failed to insert sample data"
exit 1
}
# Summary
Write-Host "`n=====================================================================" -ForegroundColor Cyan
Write-Host "[OK] Database setup complete!" -ForegroundColor Green
Write-Host "=====================================================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Database Details:" -ForegroundColor White
Write-Host " Server: (localdb)\$instanceName" -ForegroundColor Gray
Write-Host " Database: $databaseName" -ForegroundColor Gray
Write-Host ""
Write-Host "Connection String:" -ForegroundColor White
Write-Host " Server=(localdb)\$instanceName;Database=$databaseName;Trusted_Connection=True" -ForegroundColor Gray
Write-Host ""
Write-Host "Tables Created:" -ForegroundColor White
Write-Host " - Categories (4 rows)" -ForegroundColor Gray
Write-Host " - Products (8 rows)" -ForegroundColor Gray
Write-Host " - Customers (4 rows)" -ForegroundColor Gray
Write-Host " - Orders (4 rows)" -ForegroundColor Gray
Write-Host " - OrderItems (9 rows)" -ForegroundColor Gray
Write-Host ""
Write-Host "Next Steps:" -ForegroundColor White
Write-Host " 1. Build the DatabaseProject:" -ForegroundColor Gray
Write-Host " dotnet build DatabaseProject" -ForegroundColor Cyan
Write-Host ""
Write-Host " 2. Build the DataAccessProject:" -ForegroundColor Gray
Write-Host " dotnet build DataAccessProject" -ForegroundColor Cyan
Write-Host ""