-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Genre a separate entity in its own right
- Loading branch information
1 parent
abe14d3
commit 3204cf3
Showing
38 changed files
with
853 additions
and
78 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM mcr.microsoft.com/dotnet/core/aspnet:latest | ||
COPY musiccatalogue.api-1.14.0.0 /opt/musiccatalogue.api-1.14.0.0 | ||
WORKDIR /opt/musiccatalogue.api-1.14.0.0/bin | ||
COPY musiccatalogue.api-1.15.0.0 /opt/musiccatalogue.api-1.15.0.0 | ||
WORKDIR /opt/musiccatalogue.api-1.15.0.0/bin | ||
ENTRYPOINT [ "./MusicCatalogue.Api" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
-- "Before" Query | ||
SELECT Title, Genre | ||
FROM ALBUMS | ||
ORDER BY Title ASC; | ||
|
||
-- "After" Query | ||
SELECT al.Title, g.Name AS "Genre" | ||
FROM ALBUMS al | ||
INNER JOIN GENRES g ON g.Id = al.GenreId | ||
ORDER BY al.Title ASC; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
-- Create a GENRES table manually | ||
CREATE TABLE GENRES ( | ||
Id int NOT NULL PRIMARY KEY, | ||
Name Text NOT NULL ); | ||
|
||
-- Add the Genre ID column to the ALBUMS table | ||
ALTER TABLE ALBUMS ADD GenreId int NULL; | ||
|
||
-- Check the WITH construct does what's required in this instance | ||
WITH CURRENT_GENRES ( Id, Name ) AS | ||
( | ||
SELECT DISTINCT RANK() OVER ( ORDER BY Genre ), Genre | ||
FROM ALBUMS | ||
GROUP BY Genre | ||
) | ||
SELECT * | ||
FROM CURRENT_GENRES; | ||
|
||
-- Test the migration script to move genres to the new table | ||
WITH CURRENT_GENRES ( Id, Name ) AS | ||
( | ||
SELECT DISTINCT RANK() OVER ( ORDER BY Genre ), Genre | ||
FROM ALBUMS | ||
) | ||
INSERT INTO GENRES ( Id, Name ) | ||
SELECT Id, Name | ||
FROM CURRENT_GENRES; | ||
|
||
-- And to update the Genre ID against the album | ||
UPDATE ALBUMS | ||
SET GenreId = ( SELECT Id FROM GENRES WHERE Name = ALBUMS.Genre ); | ||
|
||
-- Cross-check - should produce 0 rows | ||
SELECT al.Genre, g.Name | ||
FROM ALBUMS al | ||
INNER JOIN GENRES g ON g.Id = al.GenreId | ||
WHERE al.Genre <> g.Name; | ||
|
||
-- Clean up | ||
ALTER TABLE ALBUMS DROP GenreId; | ||
DROP TABLE GENRES; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System.Reflection; | ||
|
||
namespace MusicCatalogue.Data | ||
{ | ||
public static class MigrationUtilities | ||
{ | ||
/// <summary> | ||
/// Get the namespace for the migration SQL scripts | ||
/// </summary> | ||
/// <returns></returns> | ||
public static string GetSqlScriptNamespace() | ||
{ | ||
// The assumption is that there will be a SQL folder at the same level as this class | ||
// where the SQL script resources are held | ||
var dataAssemblyNamespace = MethodBase.GetCurrentMethod()?.DeclaringType?.Namespace ?? ""; | ||
var sqlScriptNamespace = $"{dataAssemblyNamespace}.Sql"; | ||
return sqlScriptNamespace; | ||
} | ||
|
||
/// <summary> | ||
/// Read and return the contents of an embedded resource containing a SQL migration script | ||
/// </summary> | ||
/// <param name="name"></param> | ||
/// <returns></returns> | ||
public static string ReadMigrationSqlScript(string name) | ||
{ | ||
string content = ""; | ||
|
||
// Get the resource name | ||
var sqlScriptNamespace = GetSqlScriptNamespace(); | ||
var sqlResourceName = $"{sqlScriptNamespace}.{name}"; | ||
|
||
// Get the name of the resource and a resource stream for reading it | ||
var assembly = Assembly.GetExecutingAssembly(); | ||
var resourceStream = assembly.GetManifestResourceStream(sqlResourceName); | ||
|
||
// Open a stream reader to read the file content | ||
using (var reader = new StreamReader(resourceStream!)) | ||
{ | ||
// Read the file content | ||
content = reader.ReadToEnd(); | ||
} | ||
|
||
return content; | ||
} | ||
} | ||
} |
Oops, something went wrong.