Skip to content

Commit

Permalink
Merge pull request #2 from JustinFrizzell/purge-customer-data
Browse files Browse the repository at this point in the history
Purge customer data
  • Loading branch information
JustinFrizzell authored Oct 12, 2023
2 parents ed167cb + 497908a commit b65e2eb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Challenges/#6 - usp_PurgeCustomerData.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Requirement: usp_PurgeCustomerData

-- We need a stored procedure that: Deletes personal data of the customer from relevant tables.

-- Technical Requirements:
-- Create a stored procedure named usp_PurgeCustomerData which:
-- Takes a parameter named @CustomerID.
-- Deletes or anonymizes data for the given customer across the database.

-- Solution: WideWorldImporters\Challenges\StoredProcedures\usp_PurgeCustomerData.sql

EXEC Challenges.usp_PurgeCustomerData @CustomerID = 1;
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ A SQL project based on the [WideWorldImporters](https://learn.microsoft.com/en-u
- [Challenges.TopSellingProductsByCity](https://github.com/JustinFrizzell/wide-world-importers-sql-project/blob/main/WideWorldImporters/Challenges/Views/TopSellingProductsByCity.sql) - A view of the top 5 selling products split for each city
- [Challenges.StockItemSalesDistribution](https://github.com/JustinFrizzell/wide-world-importers-sql-project/blob/main/WideWorldImporters/Challenges/Views/StockItemSalesDistribution.sql) - A view showing the cumulative distribution of revenue by product
- [Challenges.ufn_GetTotalOrderWeight](https://github.com/JustinFrizzell/wide-world-importers-sql-project/blob/main/WideWorldImporters/Challenges/Functions/ufn_GetTotalOrderWeight.sql) - A scalar function returning the total weight of a given OrderID
- [Challenges.ufn_DaysSinceLastInvoice](https://github.com/JustinFrizzell/wide-world-importers-sql-project/blob/main/WideWorldImporters/Challenges/Functions/ufn_DaysSinceLastInvoice.sql) - A tabular function returning the date of the previous invoice, current invoice and the number of days since the last invoice for an InvoiceID
- [Challenges.usp_PurgeCustomerData](https://github.com/JustinFrizzell/wide-world-importers-sql-project/blob/main/WideWorldImporters/Challenges/StoredProcedures/usp_PurgeCustomerData.sql) - A stored procedure to remove customer data from the database after a GDPR request

The project has been built into a `.dacpac` file for easy deployment using either SQL Server Data Tools (SSDT) or Azure Data Studio (see Installation).

CI/CD is achieved using Azure Pipelines to validate the build process. SQLFluff is used to ensure consistency across code.

Project structure:

![Screenshot showing structure](https://raw.githubusercontent.com/JustinFrizzell/wide-world-importers-sql-project/main/project_structure.png)

# Installation

## WorldWideImporters
Expand All @@ -28,3 +34,5 @@ SQLFluff is a SQL linter and formatter, to install follow [this guide](https://d
This repository builds into a Data-Tier Application Package (`.dacpac`), which is a single file containing the full database schema. This can be used to deploy the database to various environments using SQL Server Data Tools in Visual Studio or Azure Data Studio.

To deploy, download the latest `.dacpac` from [Releases](https://github.com/JustinFrizzell/wide-world-importers-sql-project/releases) and follow [this guide](https://www.sqlservercentral.com/articles/sql-server-dacpac-in-azure-data-studio). This will add the Challenges schema to the WorldWideImporters database installed above.

![Screenshot showing deployment](https://raw.githubusercontent.com/JustinFrizzell/wide-world-importers-sql-project/main/deployment.png)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE FUNCTION dbo.ufn_DaysSinceLastInvoice (@InvoiceID INT)
CREATE FUNCTION Challenges.ufn_DaysSinceLastInvoice (@InvoiceID INT)
RETURNS TABLE
AS
RETURN
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CREATE PROCEDURE Challenges.usp_PurgeCustomerData
@CustomerID INT
AS
BEGIN
BEGIN TRANSACTION;
BEGIN TRY

-- Anonymise data in the Orders table
UPDATE Sales.Orders
SET
Comments = NULL
, DeliveryInstructions = NULL
WHERE CustomerID = @CustomerID;

-- Delete personal data from the Customers table
DELETE FROM Sales.Customers
WHERE CustomerID = @CustomerID;

COMMIT TRANSACTION;

END TRY
BEGIN CATCH
-- Rollback if there's an error
ROLLBACK TRANSACTION;
THROW;
END CATCH
END
Binary file modified project_structure.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b65e2eb

Please sign in to comment.