Skip to content

Commit

Permalink
Seed data
Browse files Browse the repository at this point in the history
  • Loading branch information
atrakic committed Dec 12, 2024
1 parent 01459d7 commit 199c7d7
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 40 deletions.
8 changes: 1 addition & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,4 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Build the stack
run: docker compose up -d
- name: Test
run: docker compose logs sqlpackage
- name: Stats
run: docker compose stats --no-stream
- name: Stop the stack
run: docker compose down -v
run: docker compose up --quiet-pull --no-color --remove-orphans --abort-on-container-failure --exit-code-from seed
4 changes: 0 additions & 4 deletions DbProject/dbo/Tables/DimCustomer.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
CREATE TABLE [dbo].[DimCustomer] (
[CustomerKey] INT NOT NULL PRIMARY KEY,
[CustomerAltKey] VARCHAR (50) NULL,
[Title] VARCHAR (5) NULL,
[FirstName] VARCHAR (50) NOT NULL,
[LastName] VARCHAR (50) NULL,
[AddressLine1] VARCHAR (200) NULL,
[City] VARCHAR (50) NULL,
[StateProvince] VARCHAR (50) NULL,
[CountryRegion] VARCHAR (50) NULL,
[PostalCode] VARCHAR (20) NULL
);

Expand Down
8 changes: 1 addition & 7 deletions DbProject/dbo/Tables/DimDate.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
CREATE TABLE [dbo].[DimDate] (
[DateKey] INT NOT NULL PRIMARY KEY,
[DateAltKey] DATE NOT NULL,
[DayOfWeek] INT NOT NULL,
[WeekDayName] VARCHAR (10) NULL,
[DayOfMonth] INT NOT NULL,
[Month] INT NOT NULL,
[MonthName] VARCHAR (12) NULL,
[Year] INT NOT NULL
[Date] DATE NOT NULL
);


Expand Down
2 changes: 0 additions & 2 deletions DbProject/dbo/Tables/DimProduct.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
CREATE TABLE [dbo].[DimProduct] (
-- Update comment
[ProductKey] INT NOT NULL PRIMARY KEY,
[ProductAltKey] VARCHAR (25) NULL,
[ProductName] VARCHAR (50) NOT NULL,
[Category] VARCHAR (50) NULL,
[ListPrice] DECIMAL (18) NULL
Expand Down
17 changes: 17 additions & 0 deletions DbProject/dbo/Views/vSalesByCityAndCategory.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

CREATE VIEW vSalesByCityAndCategory
AS
SELECT
c.City,
p.Category,
SUM(f.Quantity) AS TotalQuantity,
SUM(f.SalesTotal) AS TotalSales
FROM
dbo.FactSalesOrder f
INNER JOIN dbo.DimCustomer c ON f.CustomerKey = c.CustomerKey
INNER JOIN dbo.DimProduct p ON f.ProductKey = p.ProductKey
GROUP BY
c.City,
p.Category

GO
18 changes: 0 additions & 18 deletions DbProject/dbo/Views/vSalesByRegion.sql

This file was deleted.

47 changes: 47 additions & 0 deletions SQL/dw-seed.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
-- Insert sample data into the Customers table
MERGE INTO DimCustomer AS target
USING (VALUES
(1, 'John', 'Doe', '123 Main St', 'Springfield', '12345'),
(2, 'Jane', 'Smith', '456 Elm St', 'Springfield', '12345'),
(3, 'Bob', 'Jones', '789 Oak St', 'Springfield', '12345')
) AS source (CustomerKey, FirstName, LastName, AddressLine1, City, PostalCode)
ON target.CustomerKey = source.CustomerKey
WHEN NOT MATCHED BY TARGET THEN
INSERT (CustomerKey, FirstName, LastName, AddressLine1, City, PostalCode)
VALUES (source.CustomerKey, source.FirstName, source.LastName, source.AddressLine1, source.City, source.PostalCode);

-- Insert sample data into the DimProducts table
MERGE INTO DimProduct AS target
USING (VALUES
(1, 'Bike', 'BK-001', 1000.00),
(2, 'Car', 'CR-001', 2000.00),
(3, 'Truck', 'TR-001', 3000.00)
) AS source (ProductKey, ProductName, Category, ListPrice)
ON target.ProductKey = source.ProductKey
WHEN NOT MATCHED BY TARGET THEN
INSERT (ProductKey, ProductName, Category, ListPrice)
VALUES (source.ProductKey, source.ProductName, source.Category, source.ListPrice);

-- Insert sample data into the DimDate table
MERGE INTO DimDate AS target
USING (VALUES
(1, '2020-01-01'),
(2, '2020-01-02'),
(3, '2020-01-03')
) AS source (DateKey, Date)
ON target.DateKey = source.DateKey
WHEN NOT MATCHED BY TARGET THEN
INSERT (DateKey, Date)
VALUES (source.DateKey, source.Date);

-- Insert sample data into the FactSales table
MERGE INTO FactSalesOrder AS target
USING (VALUES
(1, 1, 1, 1, 1, 1000.00),
(2, 2, 2, 2, 2, 4000.00),
(3, 3, 3, 3, 3, 9000.00)
) AS source (SalesOrderKey, SalesOrderDateKey, ProductKey, CustomerKey, Quantity, SalesTotal)
ON target.SalesOrderKey = source.SalesOrderKey
WHEN NOT MATCHED BY TARGET THEN
INSERT (SalesOrderKey, SalesOrderDateKey, ProductKey, CustomerKey, Quantity, SalesTotal)
VALUES (source.SalesOrderKey, source.SalesOrderDateKey, source.ProductKey, source.CustomerKey, source.Quantity, source.SalesTotal);
11 changes: 11 additions & 0 deletions azure-sql-deploy-dacpac.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbProject", "DbProject\DbProject.sqlproj", "{0DA6EF13-79FA-455F-85E0-C6A54842EED0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{A1AF7517-E017-4C34-A844-B6A2A78D141A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqlClient.ConsoleApp", "src\SqlClient.ConsoleApp\SqlClient.ConsoleApp.csproj", "{405F855C-1D92-41FF-9E1A-EDDCE4305170}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -18,5 +22,12 @@ Global
{0DA6EF13-79FA-455F-85E0-C6A54842EED0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0DA6EF13-79FA-455F-85E0-C6A54842EED0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0DA6EF13-79FA-455F-85E0-C6A54842EED0}.Release|Any CPU.Build.0 = Release|Any CPU
{405F855C-1D92-41FF-9E1A-EDDCE4305170}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{405F855C-1D92-41FF-9E1A-EDDCE4305170}.Debug|Any CPU.Build.0 = Debug|Any CPU
{405F855C-1D92-41FF-9E1A-EDDCE4305170}.Release|Any CPU.ActiveCfg = Release|Any CPU
{405F855C-1D92-41FF-9E1A-EDDCE4305170}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{405F855C-1D92-41FF-9E1A-EDDCE4305170} = {A1AF7517-E017-4C34-A844-B6A2A78D141A}
EndGlobalSection
EndGlobal
26 changes: 24 additions & 2 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
services:
seed:
build:
context: ./src/SqlClient.ConsoleApp
tty: true
container_name: seed
working_dir: /app
env_file:
- .env
environment:
ConnectionString: "Server=tcp:db,1433;Initial Catalog=demo;UID=sa;Password=${MSSQL_SA_PASSWORD};TrustServerCertificate=true;Connection Timeout=3;"
volumes:
- $PWD/SQL/dw-seed.sql:/seed.sql
entrypoint:
- /bin/sh
- -c
- |
./SqlClient.ConsoleApp /seed.sql
# keep the container running (for debugging)
#tail -f /dev/null
depends_on:
sqlpackage:
condition: service_completed_successfully
required: true

sqlpackage:
build:
context: ./docker/sqlpackage
Expand All @@ -19,8 +43,6 @@ services:
- -c
- |
/scripts/deploy.sh
# keep the container running (for debugging)
tail -f /dev/null
depends_on:
db:
condition: service_healthy
Expand Down
8 changes: 8 additions & 0 deletions src/SqlClient.ConsoleApp/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# directories
**/bin/
**/obj/
**/out/

# files
Dockerfile*
**/*.md
19 changes: 19 additions & 0 deletions src/SqlClient.ConsoleApp/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:9.0 AS build
ARG TARGETARCH
WORKDIR /source

# Copy the project file and restore dependencies
COPY *.csproj ./
RUN dotnet restore -a $TARGETARCH

# Copy source code and publish app
COPY . ./
RUN dotnet publish -c Release -a $TARGETARCH --no-restore -o /app

# Use the official Microsoft .NET runtime image
FROM mcr.microsoft.com/dotnet/runtime:9.0 AS final
WORKDIR /app
COPY --link --from=build /app ./
ARG APP_UID=1000
USER $APP_UID
ENTRYPOINT ["./SqlClient.ConsoleApp"]
67 changes: 67 additions & 0 deletions src/SqlClient.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Diagnostics;
using Microsoft.Data.SqlClient;
using System.Threading.Tasks;
using static System.Console;

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;

namespace SqlClient.ConsoleApp
{
class Program
{
static async Task Main(string[] args)
{

var serviceProvider = new ServiceCollection()
.AddLogging(configure => configure.AddConsole())
.BuildServiceProvider();

var logger = serviceProvider.GetService<ILogger<Program>>();


string connection = Environment.GetEnvironmentVariable("ConnectionString")
?? throw new ArgumentException("Missing 'ConnectionString' env variable");

if (args.Length == 0)
{
logger.LogInformation("Please provide the path to the SQL file:");
string filePath = ReadLine();
if (string.IsNullOrEmpty(filePath))
{
logger.LogError("Missing SQL file argument");
throw new ArgumentException("Missing SQL file argument");
}
args = new string[] { filePath };
}

try
{
string _sql = System.IO.File.ReadAllText(args[0]);
SqlConnection conn = new SqlConnection(connection);
conn.Open();

var logMessage = new
{
Message = "Connected to SQL Server: ",
ServerVersion = conn.ServerVersion,
OSVersion = Environment.OSVersion.VersionString
};
logger.LogInformation(JsonSerializer.Serialize(logMessage));

SqlCommand cmd = new SqlCommand(_sql, conn);
var qr = await cmd.ExecuteScalarAsync();
logger.LogInformation($"Query from file: {args[0]} executed successfully.");
logger.LogInformation($"Result: {qr}");
conn.Close();
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred while executing the SQL file.");
}
}
}
}
17 changes: 17 additions & 0 deletions src/SqlClient.ConsoleApp/SqlClient.ConsoleApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<EnableSdkContainerSupport>true</EnableSdkContainerSupport>
<InvariantGlobalization>false</InvariantGlobalization>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.0" />
</ItemGroup>

</Project>

0 comments on commit 199c7d7

Please sign in to comment.