Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slurp db #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions DbDeploy-Kata.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,21 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Deployment", "Deployment",
Legacy-Deployment\20220516-AddOrderTotal.sql = Legacy-Deployment\20220516-AddOrderTotal.sql
EndProjectSection
EndProject
Project("{00D1A9C2-B5F0-4AF3-8072-F6C62B433612}") = "Vs-SqlProject", "Vs-SqlProject\Vs-SqlProject.sqlproj", "{36B155B0-3ADB-4F2A-A32A-B7042AEA1ADD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{36B155B0-3ADB-4F2A-A32A-B7042AEA1ADD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{36B155B0-3ADB-4F2A-A32A-B7042AEA1ADD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{36B155B0-3ADB-4F2A-A32A-B7042AEA1ADD}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{36B155B0-3ADB-4F2A-A32A-B7042AEA1ADD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{36B155B0-3ADB-4F2A-A32A-B7042AEA1ADD}.Release|Any CPU.Build.0 = Release|Any CPU
{36B155B0-3ADB-4F2A-A32A-B7042AEA1ADD}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
Expand Down
35 changes: 26 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,33 @@ On a un site de eCommerce qui vend des ordinateurs. On veut ajouter un systéme

### Etat initial

* Récupérer le répo existant.
* Se positionner sur le tag "00-db-init"
* Raconter :
* Il y a un ensemble de scripts SQL versionnés par des timestamp qui servent à déployer la base
* On veut changer ça et avoir un vrai système moderne
* Exemple :
* on fait l'init de base avec "0-init-db.sql"
* on applique "20220516-AddOrderTotal.sql"
- Récupérer le répo existant.
- Se positionner sur le tag "00-db-init"
- Raconter :

* Lancer les 2 scripts d'init pour avoir la base de données existante
- Il y a un ensemble de scripts SQL versionnés par des timestamp qui servent à déployer la base
- On veut changer ça et avoir un vrai système moderne
- Exemple :
- on fait l'init de base avec "0-init-db.sql"
- on applique "20220516-AddOrderTotal.sql"

- Lancer les 2 scripts d'init pour avoir la base de données existante

On constate que le versioning git vaut é peu prés un versionning sur systéme de fichier.
La procédure de déploiement demande de se rappeler de ce qui a été déployé ou pas, et s'assurer que tout est fait dans le bon ordre.

- Créer un projet de base de données
- Lancer une comparaison de shémas avec la BDD existante
![](./Screenshots/launch_schemas_compare.png)
![](./Screenshots/select_database_for_comparison.png)
![](./Screenshots/select_project_for_comparison.png)
![](./Screenshots/source_and_destination_for_schemas_comparison.png)
- Mettre à jour le projet de base de données
- Lancer une comparaison de données:
![](./Screenshots/launch_data_comparison.png)
- Repérer les tables référentiel.
- Créer des scripts d'insertion de données de référentiel
- Ajouter un script de post deployment
- Ajouter les scripts de données de référentiel
- Publier le projet sur une nouvelle BDD.
- comparer la base originale et la nouvelle base.
Binary file added Screenshots/launch_data_comparison.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/launch_schemas_compare.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/select_database_for_comparison.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Screenshots/select_project_for_comparison.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions Vs-SqlProject/Script.PostDeployment.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
Post-Deployment Script Template
--------------------------------------------------------------------------------------
This file contains SQL statements that will be appended to the build script.
Use SQLCMD syntax to include a file in the post-deployment script.
Example: :r .\myfile.sql
Use SQLCMD syntax to reference a variable in the post-deployment script.
Example: :setvar TableName MyTable
SELECT * FROM [$(TableName)]
--------------------------------------------------------------------------------------
*/
:r .\data\dbo.categories.sql
:r .\data\dbo.paymentMethods.sql
:r .\data\dbo.products.sql
81 changes: 81 additions & 0 deletions Vs-SqlProject/Vs-SqlProject.sqlproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<Name>Vs_SqlProject</Name>
<SchemaVersion>2.0</SchemaVersion>
<ProjectVersion>4.1</ProjectVersion>
<ProjectGuid>{36b155b0-3adb-4f2a-a32a-b7042aea1add}</ProjectGuid>
<DSP>Microsoft.Data.Tools.Schema.Sql.Sql130DatabaseSchemaProvider</DSP>
<OutputType>Database</OutputType>
<RootPath>
</RootPath>
<RootNamespace>Vs_SqlProject</RootNamespace>
<AssemblyName>Vs_SqlProject</AssemblyName>
<ModelCollation>1033, CI</ModelCollation>
<DefaultFileStructure>BySchemaAndSchemaType</DefaultFileStructure>
<DeployToDatabase>True</DeployToDatabase>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<TargetLanguage>CS</TargetLanguage>
<AppDesignerFolder>Properties</AppDesignerFolder>
<SqlServerVerification>False</SqlServerVerification>
<IncludeCompositeObjects>True</IncludeCompositeObjects>
<TargetDatabaseSet>True</TargetDatabaseSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<OutputPath>bin\Release\</OutputPath>
<BuildScriptName>$(MSBuildProjectName).sql</BuildScriptName>
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<DefineDebug>false</DefineDebug>
<DefineTrace>true</DefineTrace>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<OutputPath>bin\Debug\</OutputPath>
<BuildScriptName>$(MSBuildProjectName).sql</BuildScriptName>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<DefineDebug>true</DefineDebug>
<DefineTrace>true</DefineTrace>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">11.0</VisualStudioVersion>
<!-- Default to the v11.0 targets path if the targets file for the current VS version is not found -->
<SSDTExists Condition="Exists('$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets')">True</SSDTExists>
<VisualStudioVersion Condition="'$(SSDTExists)' == ''">11.0</VisualStudioVersion>
</PropertyGroup>
<Import Condition="'$(SQLDBExtensionsRefPath)' != ''" Project="$(SQLDBExtensionsRefPath)\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
<Import Condition="'$(SQLDBExtensionsRefPath)' == ''" Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
<ItemGroup>
<Folder Include="Properties" />
<Folder Include="dbo\" />
<Folder Include="dbo\Tables\" />
<Folder Include="dbo\Views\" />
<Folder Include="data" />
</ItemGroup>
<ItemGroup>
<Build Include="dbo\Tables\Products.sql" />
<Build Include="dbo\Tables\PaymentMethods.sql" />
<Build Include="dbo\Tables\Orders.sql" />
<Build Include="dbo\Tables\OrderLines.sql" />
<Build Include="dbo\Tables\Customers.sql" />
<Build Include="dbo\Tables\Categories.sql" />
<Build Include="dbo\Views\CustomerOrders.sql" />
<None Include="data\dbo.paymentMethods.sql" />
<None Include="data\dbo.products.sql" />
</ItemGroup>
<ItemGroup>
<None Include="data\dbo.categories.sql" />
</ItemGroup>
<ItemGroup>
<PostDeploy Include="Script.PostDeployment.sql" />
</ItemGroup>
</Project>
20 changes: 20 additions & 0 deletions Vs-SqlProject/data/dbo.categories.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
SET IDENTITY_INSERT dbo.Categories ON
MERGE dbo.Categories as Target
USING(
VALUES (1, N'Computers'),
(2, N'Phones'),
(3, N'TV'),
(4, N'Minitels'),
(5, N'Cameras'),
(6, N'Audio'),
(7, N'Accessories'),
(8, N'DVD and Blu-rays')
) as source ([CategoryId], [Name])
on Target.[CategoryId] = source.[CategoryId]
WHEN NOT MATCHED THEN
INSERT ([CategoryId], [Name]) VALUES (source.[CategoryId], source.[Name])
WHEN MATCHED THEN
UPDATE SET [name] = source.[Name]
WHEN NOT MATCHED BY SOURCE THEN
DELETE;
SET IDENTITY_INSERT dbo.Categories OFF
9 changes: 9 additions & 0 deletions Vs-SqlProject/data/dbo.paymentMethods.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MERGE dbo.PaymentMethods as Target
USING(
VALUES (N'Gold')
) as source ([PaymentMethod])
on Target.[PaymentMethod] = source.[PaymentMethod]
WHEN NOT MATCHED THEN
INSERT ([PaymentMethod]) VALUES (source.[PaymentMethod])
WHEN NOT MATCHED BY SOURCE THEN
DELETE;
19 changes: 19 additions & 0 deletions Vs-SqlProject/data/dbo.products.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
SET IDENTITY_INSERT dbo.Products ON
MERGE dbo.Products as Target
USING(
VALUES
(2, N'Minitel 5 Matra', 4, N'Pour avoir ses notes de bac', 200.0000, 2198),
(3, N'Minitel 1b Telic Alcatel', 4, N'Pour consulter les horaires de train', 180.0000, 412),
(5, N'Coffret le Seigneur des Anneaux La trilogie', 8, N'https://fr.wikipedia.org/wiki/Le_Seigneur_des_anneaux', 89.9900, 561),
(6, N'Télé avec lecteur Blu-ray intégré', 3, NULL, 390.0000, 3)

) as source ([ProductId], [Name], [CategoryId], [Description], [UnitPrice], [Stock])
on Target.[ProductId] = source.[ProductId]
WHEN NOT MATCHED THEN
INSERT ([ProductId], [Name], [CategoryId], [Description], [UnitPrice], [Stock]) VALUES (source.[ProductId], source.[Name], source.[CategoryId], source.[Description], source.[UnitPrice], source.[Stock])
WHEN MATCHED THEN
update set [Name] = source.[Name], [CategoryId] = source.[CategoryId], [Description] = source.[Description], [UnitPrice] = source.[UnitPrice], [Stock] = source.[Stock]
WHEN NOT MATCHED BY SOURCE THEN
DELETE;

SET IDENTITY_INSERT dbo.Products OFF
6 changes: 6 additions & 0 deletions Vs-SqlProject/dbo/Tables/Categories.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE [dbo].[Categories] (
[CategoryId] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (100) NOT NULL,
CONSTRAINT [PK_Categories] PRIMARY KEY CLUSTERED ([CategoryId] ASC)
);

11 changes: 11 additions & 0 deletions Vs-SqlProject/dbo/Tables/Customers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE [dbo].[Customers] (
[CustomerId] INT IDENTITY (1, 1) NOT NULL,
[Username] NVARCHAR (500) NOT NULL,
CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED ([CustomerId] ASC)
);


GO
CREATE UNIQUE NONCLUSTERED INDEX [UIX_Customers_Username]
ON [dbo].[Customers]([Username] ASC);

9 changes: 9 additions & 0 deletions Vs-SqlProject/dbo/Tables/OrderLines.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE [dbo].[OrderLines] (
[OrderId] INT NOT NULL,
[ProductId] INT NOT NULL,
[Quantity] INT CONSTRAINT [DF_OrderLines_Quantity] DEFAULT ((1)) NOT NULL,
CONSTRAINT [PK_OrderLines] PRIMARY KEY CLUSTERED ([OrderId] ASC, [ProductId] ASC),
CONSTRAINT [FK_OrderLines_Orders] FOREIGN KEY ([OrderId]) REFERENCES [dbo].[Orders] ([OrderId]),
CONSTRAINT [FK_OrderLines_Products] FOREIGN KEY ([ProductId]) REFERENCES [dbo].[Products] ([ProductId])
);

10 changes: 10 additions & 0 deletions Vs-SqlProject/dbo/Tables/Orders.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE [dbo].[Orders] (
[OrderId] INT IDENTITY (1, 1) NOT NULL,
[CustomerId] INT NOT NULL,
[DeliveryAddress] NVARCHAR (500) NOT NULL,
[PaymentMethod] NVARCHAR (50) NOT NULL,
CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED ([OrderId] ASC),
CONSTRAINT [FK_Orders_Customers] FOREIGN KEY ([CustomerId]) REFERENCES [dbo].[Customers] ([CustomerId]),
CONSTRAINT [FK_Orders_PaymentMethods] FOREIGN KEY ([PaymentMethod]) REFERENCES [dbo].[PaymentMethods] ([PaymentMethod])
);

5 changes: 5 additions & 0 deletions Vs-SqlProject/dbo/Tables/PaymentMethods.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE [dbo].[PaymentMethods] (
[PaymentMethod] NVARCHAR (50) NOT NULL,
CONSTRAINT [PK_PaymentMethods] PRIMARY KEY CLUSTERED ([PaymentMethod] ASC)
);

11 changes: 11 additions & 0 deletions Vs-SqlProject/dbo/Tables/Products.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE [dbo].[Products] (
[ProductId] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (500) NOT NULL,
[CategoryId] INT NOT NULL,
[Description] NVARCHAR (MAX) NULL,
[UnitPrice] MONEY NOT NULL,
[Stock] INT NOT NULL,
CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED ([ProductId] ASC),
CONSTRAINT [FK_Products_Categories] FOREIGN KEY ([CategoryId]) REFERENCES [dbo].[Categories] ([CategoryId])
);

11 changes: 11 additions & 0 deletions Vs-SqlProject/dbo/Views/CustomerOrders.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE VIEW [dbo].[CustomerOrders] AS
(
SELECT c.CustomerId, c.Username, o.OrderId, o.DeliveryAddress, o.PaymentMethod
, SUM(p.UnitPrice * ol.Quantity) OVER ( PARTITION BY o.OrderId ) OrderTotal
, p.ProductId, p.Name ProductName, cat.Name CategoryName, p.UnitPrice, ol.Quantity, p.UnitPrice * ol.Quantity TotalPrice
FROM Customers c
INNER JOIN Orders o ON o.CustomerId = c.CustomerId
INNER JOIN OrderLines ol ON ol.OrderId = o.OrderId
INNER JOIN Products p ON p.ProductId = ol.ProductId
INNER JOIN Categories cat ON cat.CategoryId = p.CategoryId
)