-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-database.ps1
More file actions
135 lines (114 loc) · 4.08 KB
/
setup-database.ps1
File metadata and controls
135 lines (114 loc) · 4.08 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Setup script for SQLite sample database
# This script creates a sample SQLite database with tables for demonstration
$ErrorActionPreference = "Stop"
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$databaseDir = Join-Path $scriptDir "Database"
$dbPath = Join-Path $databaseDir "sample.db"
# Create Database directory if it doesn't exist
if (-not (Test-Path $databaseDir)) {
New-Item -ItemType Directory -Path $databaseDir | Out-Null
Write-Host "Created Database directory"
}
# Remove existing database if present
if (Test-Path $dbPath) {
Remove-Item $dbPath -Force
Write-Host "Removed existing database"
}
# Create the database using dotnet and inline C# (requires .NET SDK)
$csharpCode = @"
using Microsoft.Data.Sqlite;
var connectionString = @"Data Source=$($dbPath.Replace('\', '\\'))";
using var connection = new SqliteConnection(connectionString);
connection.Open();
using var command = connection.CreateCommand();
command.CommandText = @"
-- Categories table
CREATE TABLE categories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
description TEXT,
created_at TEXT DEFAULT CURRENT_TIMESTAMP
);
-- Products table
CREATE TABLE products (
id INTEGER PRIMARY KEY AUTOINCREMENT,
category_id INTEGER NOT NULL,
name TEXT NOT NULL,
description TEXT,
price REAL NOT NULL,
stock_quantity INTEGER DEFAULT 0,
is_active INTEGER DEFAULT 1,
created_at TEXT DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (category_id) REFERENCES categories(id)
);
-- Orders table
CREATE TABLE orders (
id INTEGER PRIMARY KEY AUTOINCREMENT,
customer_name TEXT NOT NULL,
customer_email TEXT NOT NULL,
order_date TEXT DEFAULT CURRENT_TIMESTAMP,
status TEXT DEFAULT 'pending',
total_amount REAL DEFAULT 0
);
-- Order items table
CREATE TABLE order_items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
order_id INTEGER NOT NULL,
product_id INTEGER NOT NULL,
quantity INTEGER NOT NULL,
unit_price REAL NOT NULL,
FOREIGN KEY (order_id) REFERENCES orders(id),
FOREIGN KEY (product_id) REFERENCES products(id)
);
-- Create indexes
CREATE INDEX idx_products_category ON products(category_id);
CREATE INDEX idx_products_active ON products(is_active);
CREATE INDEX idx_order_items_order ON order_items(order_id);
CREATE INDEX idx_order_items_product ON order_items(product_id);
CREATE INDEX idx_orders_customer_email ON orders(customer_email);
CREATE INDEX idx_orders_status ON orders(status);
-- Insert sample data
INSERT INTO categories (name, description) VALUES
('Electronics', 'Electronic devices and accessories'),
('Books', 'Physical and digital books'),
('Clothing', 'Apparel and fashion items');
INSERT INTO products (category_id, name, description, price, stock_quantity) VALUES
(1, 'Laptop', 'High-performance laptop', 999.99, 50),
(1, 'Wireless Mouse', 'Ergonomic wireless mouse', 29.99, 200),
(2, 'Programming Guide', 'Complete guide to programming', 49.99, 100),
(3, 'T-Shirt', 'Cotton t-shirt', 19.99, 500);
";
command.ExecuteNonQuery();
Console.WriteLine("Database created successfully!");
"@
# Create a temporary project to run the database creation
$tempDir = Join-Path $env:TEMP "sqlite-setup-$(Get-Random)"
New-Item -ItemType Directory -Path $tempDir | Out-Null
try {
# Create a minimal console project
Push-Location $tempDir
$csprojContent = @"
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.1" />
</ItemGroup>
</Project>
"@
Set-Content -Path "Setup.csproj" -Value $csprojContent
Set-Content -Path "Program.cs" -Value $csharpCode
Write-Host "Creating SQLite database..."
dotnet run --verbosity quiet
if ($LASTEXITCODE -ne 0) {
throw "Failed to create database"
}
Write-Host "Database created at: $dbPath" -ForegroundColor Green
}
finally {
Pop-Location
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
}