Skip to content

Commit 199c7d7

Browse files
committed
Seed data
1 parent 01459d7 commit 199c7d7

13 files changed

+212
-40
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,4 @@ jobs:
1010
steps:
1111
- uses: actions/checkout@v4
1212
- name: Build the stack
13-
run: docker compose up -d
14-
- name: Test
15-
run: docker compose logs sqlpackage
16-
- name: Stats
17-
run: docker compose stats --no-stream
18-
- name: Stop the stack
19-
run: docker compose down -v
13+
run: docker compose up --quiet-pull --no-color --remove-orphans --abort-on-container-failure --exit-code-from seed

DbProject/dbo/Tables/DimCustomer.sql

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
CREATE TABLE [dbo].[DimCustomer] (
22
[CustomerKey] INT NOT NULL PRIMARY KEY,
3-
[CustomerAltKey] VARCHAR (50) NULL,
4-
[Title] VARCHAR (5) NULL,
53
[FirstName] VARCHAR (50) NOT NULL,
64
[LastName] VARCHAR (50) NULL,
75
[AddressLine1] VARCHAR (200) NULL,
86
[City] VARCHAR (50) NULL,
9-
[StateProvince] VARCHAR (50) NULL,
10-
[CountryRegion] VARCHAR (50) NULL,
117
[PostalCode] VARCHAR (20) NULL
128
);
139

DbProject/dbo/Tables/DimDate.sql

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
CREATE TABLE [dbo].[DimDate] (
22
[DateKey] INT NOT NULL PRIMARY KEY,
3-
[DateAltKey] DATE NOT NULL,
4-
[DayOfWeek] INT NOT NULL,
5-
[WeekDayName] VARCHAR (10) NULL,
6-
[DayOfMonth] INT NOT NULL,
7-
[Month] INT NOT NULL,
8-
[MonthName] VARCHAR (12) NULL,
9-
[Year] INT NOT NULL
3+
[Date] DATE NOT NULL
104
);
115

126

DbProject/dbo/Tables/DimProduct.sql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
CREATE TABLE [dbo].[DimProduct] (
2-
-- Update comment
32
[ProductKey] INT NOT NULL PRIMARY KEY,
4-
[ProductAltKey] VARCHAR (25) NULL,
53
[ProductName] VARCHAR (50) NOT NULL,
64
[Category] VARCHAR (50) NULL,
75
[ListPrice] DECIMAL (18) NULL
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
CREATE VIEW vSalesByCityAndCategory
3+
AS
4+
SELECT
5+
c.City,
6+
p.Category,
7+
SUM(f.Quantity) AS TotalQuantity,
8+
SUM(f.SalesTotal) AS TotalSales
9+
FROM
10+
dbo.FactSalesOrder f
11+
INNER JOIN dbo.DimCustomer c ON f.CustomerKey = c.CustomerKey
12+
INNER JOIN dbo.DimProduct p ON f.ProductKey = p.ProductKey
13+
GROUP BY
14+
c.City,
15+
p.Category
16+
17+
GO

DbProject/dbo/Views/vSalesByRegion.sql

Lines changed: 0 additions & 18 deletions
This file was deleted.

SQL/dw-seed.sql

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
-- Insert sample data into the Customers table
2+
MERGE INTO DimCustomer AS target
3+
USING (VALUES
4+
(1, 'John', 'Doe', '123 Main St', 'Springfield', '12345'),
5+
(2, 'Jane', 'Smith', '456 Elm St', 'Springfield', '12345'),
6+
(3, 'Bob', 'Jones', '789 Oak St', 'Springfield', '12345')
7+
) AS source (CustomerKey, FirstName, LastName, AddressLine1, City, PostalCode)
8+
ON target.CustomerKey = source.CustomerKey
9+
WHEN NOT MATCHED BY TARGET THEN
10+
INSERT (CustomerKey, FirstName, LastName, AddressLine1, City, PostalCode)
11+
VALUES (source.CustomerKey, source.FirstName, source.LastName, source.AddressLine1, source.City, source.PostalCode);
12+
13+
-- Insert sample data into the DimProducts table
14+
MERGE INTO DimProduct AS target
15+
USING (VALUES
16+
(1, 'Bike', 'BK-001', 1000.00),
17+
(2, 'Car', 'CR-001', 2000.00),
18+
(3, 'Truck', 'TR-001', 3000.00)
19+
) AS source (ProductKey, ProductName, Category, ListPrice)
20+
ON target.ProductKey = source.ProductKey
21+
WHEN NOT MATCHED BY TARGET THEN
22+
INSERT (ProductKey, ProductName, Category, ListPrice)
23+
VALUES (source.ProductKey, source.ProductName, source.Category, source.ListPrice);
24+
25+
-- Insert sample data into the DimDate table
26+
MERGE INTO DimDate AS target
27+
USING (VALUES
28+
(1, '2020-01-01'),
29+
(2, '2020-01-02'),
30+
(3, '2020-01-03')
31+
) AS source (DateKey, Date)
32+
ON target.DateKey = source.DateKey
33+
WHEN NOT MATCHED BY TARGET THEN
34+
INSERT (DateKey, Date)
35+
VALUES (source.DateKey, source.Date);
36+
37+
-- Insert sample data into the FactSales table
38+
MERGE INTO FactSalesOrder AS target
39+
USING (VALUES
40+
(1, 1, 1, 1, 1, 1000.00),
41+
(2, 2, 2, 2, 2, 4000.00),
42+
(3, 3, 3, 3, 3, 9000.00)
43+
) AS source (SalesOrderKey, SalesOrderDateKey, ProductKey, CustomerKey, Quantity, SalesTotal)
44+
ON target.SalesOrderKey = source.SalesOrderKey
45+
WHEN NOT MATCHED BY TARGET THEN
46+
INSERT (SalesOrderKey, SalesOrderDateKey, ProductKey, CustomerKey, Quantity, SalesTotal)
47+
VALUES (source.SalesOrderKey, source.SalesOrderDateKey, source.ProductKey, source.CustomerKey, source.Quantity, source.SalesTotal);

azure-sql-deploy-dacpac.sln

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ VisualStudioVersion = 17.0.31903.59
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbProject", "DbProject\DbProject.sqlproj", "{0DA6EF13-79FA-455F-85E0-C6A54842EED0}"
77
EndProject
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{A1AF7517-E017-4C34-A844-B6A2A78D141A}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqlClient.ConsoleApp", "src\SqlClient.ConsoleApp\SqlClient.ConsoleApp.csproj", "{405F855C-1D92-41FF-9E1A-EDDCE4305170}"
11+
EndProject
812
Global
913
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1014
Debug|Any CPU = Debug|Any CPU
@@ -18,5 +22,12 @@ Global
1822
{0DA6EF13-79FA-455F-85E0-C6A54842EED0}.Debug|Any CPU.Build.0 = Debug|Any CPU
1923
{0DA6EF13-79FA-455F-85E0-C6A54842EED0}.Release|Any CPU.ActiveCfg = Release|Any CPU
2024
{0DA6EF13-79FA-455F-85E0-C6A54842EED0}.Release|Any CPU.Build.0 = Release|Any CPU
25+
{405F855C-1D92-41FF-9E1A-EDDCE4305170}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26+
{405F855C-1D92-41FF-9E1A-EDDCE4305170}.Debug|Any CPU.Build.0 = Debug|Any CPU
27+
{405F855C-1D92-41FF-9E1A-EDDCE4305170}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{405F855C-1D92-41FF-9E1A-EDDCE4305170}.Release|Any CPU.Build.0 = Release|Any CPU
29+
EndGlobalSection
30+
GlobalSection(NestedProjects) = preSolution
31+
{405F855C-1D92-41FF-9E1A-EDDCE4305170} = {A1AF7517-E017-4C34-A844-B6A2A78D141A}
2132
EndGlobalSection
2233
EndGlobal

compose.yml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
services:
2+
seed:
3+
build:
4+
context: ./src/SqlClient.ConsoleApp
5+
tty: true
6+
container_name: seed
7+
working_dir: /app
8+
env_file:
9+
- .env
10+
environment:
11+
ConnectionString: "Server=tcp:db,1433;Initial Catalog=demo;UID=sa;Password=${MSSQL_SA_PASSWORD};TrustServerCertificate=true;Connection Timeout=3;"
12+
volumes:
13+
- $PWD/SQL/dw-seed.sql:/seed.sql
14+
entrypoint:
15+
- /bin/sh
16+
- -c
17+
- |
18+
./SqlClient.ConsoleApp /seed.sql
19+
# keep the container running (for debugging)
20+
#tail -f /dev/null
21+
depends_on:
22+
sqlpackage:
23+
condition: service_completed_successfully
24+
required: true
25+
226
sqlpackage:
327
build:
428
context: ./docker/sqlpackage
@@ -19,8 +43,6 @@ services:
1943
- -c
2044
- |
2145
/scripts/deploy.sh
22-
# keep the container running (for debugging)
23-
tail -f /dev/null
2446
depends_on:
2547
db:
2648
condition: service_healthy
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# directories
2+
**/bin/
3+
**/obj/
4+
**/out/
5+
6+
# files
7+
Dockerfile*
8+
**/*.md

src/SqlClient.ConsoleApp/Dockerfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:9.0 AS build
2+
ARG TARGETARCH
3+
WORKDIR /source
4+
5+
# Copy the project file and restore dependencies
6+
COPY *.csproj ./
7+
RUN dotnet restore -a $TARGETARCH
8+
9+
# Copy source code and publish app
10+
COPY . ./
11+
RUN dotnet publish -c Release -a $TARGETARCH --no-restore -o /app
12+
13+
# Use the official Microsoft .NET runtime image
14+
FROM mcr.microsoft.com/dotnet/runtime:9.0 AS final
15+
WORKDIR /app
16+
COPY --link --from=build /app ./
17+
ARG APP_UID=1000
18+
USER $APP_UID
19+
ENTRYPOINT ["./SqlClient.ConsoleApp"]

src/SqlClient.ConsoleApp/Program.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text.Json;
4+
using System.Diagnostics;
5+
using Microsoft.Data.SqlClient;
6+
using System.Threading.Tasks;
7+
using static System.Console;
8+
9+
using Microsoft.Extensions.Logging;
10+
using Microsoft.Extensions.DependencyInjection;
11+
12+
namespace SqlClient.ConsoleApp
13+
{
14+
class Program
15+
{
16+
static async Task Main(string[] args)
17+
{
18+
19+
var serviceProvider = new ServiceCollection()
20+
.AddLogging(configure => configure.AddConsole())
21+
.BuildServiceProvider();
22+
23+
var logger = serviceProvider.GetService<ILogger<Program>>();
24+
25+
26+
string connection = Environment.GetEnvironmentVariable("ConnectionString")
27+
?? throw new ArgumentException("Missing 'ConnectionString' env variable");
28+
29+
if (args.Length == 0)
30+
{
31+
logger.LogInformation("Please provide the path to the SQL file:");
32+
string filePath = ReadLine();
33+
if (string.IsNullOrEmpty(filePath))
34+
{
35+
logger.LogError("Missing SQL file argument");
36+
throw new ArgumentException("Missing SQL file argument");
37+
}
38+
args = new string[] { filePath };
39+
}
40+
41+
try
42+
{
43+
string _sql = System.IO.File.ReadAllText(args[0]);
44+
SqlConnection conn = new SqlConnection(connection);
45+
conn.Open();
46+
47+
var logMessage = new
48+
{
49+
Message = "Connected to SQL Server: ",
50+
ServerVersion = conn.ServerVersion,
51+
OSVersion = Environment.OSVersion.VersionString
52+
};
53+
logger.LogInformation(JsonSerializer.Serialize(logMessage));
54+
55+
SqlCommand cmd = new SqlCommand(_sql, conn);
56+
var qr = await cmd.ExecuteScalarAsync();
57+
logger.LogInformation($"Query from file: {args[0]} executed successfully.");
58+
logger.LogInformation($"Result: {qr}");
59+
conn.Close();
60+
}
61+
catch (Exception ex)
62+
{
63+
logger.LogError(ex, "An error occurred while executing the SQL file.");
64+
}
65+
}
66+
}
67+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<EnableSdkContainerSupport>true</EnableSdkContainerSupport>
7+
<InvariantGlobalization>false</InvariantGlobalization>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.2" />
12+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
13+
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
14+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.0" />
15+
</ItemGroup>
16+
17+
</Project>

0 commit comments

Comments
 (0)