-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add database migration with yuniql (#224)
- Loading branch information
1 parent
62a0ed0
commit d6f56ce
Showing
22 changed files
with
266 additions
and
46 deletions.
There are no files selected for viewing
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
31 changes: 16 additions & 15 deletions
31
src/Altinn.Profile.Integrations/Extensions/ConfigurationExtensions.cs
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,43 +1,44 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Altinn.Profile.Integrations.Extensions; | ||
|
||
/// <summary> | ||
/// Extension class for <see cref="IConfiguration"/> to add more members. | ||
/// </summary> | ||
[ExcludeFromCodeCoverage] | ||
public static class ConfigurationExtensions | ||
{ | ||
private const string ProfileDbAdminUserNameKey = "PostgreSqlSettings:ProfileDbAdminUserName"; | ||
private const string ProfileDbAdminPasswordKey = "PostgreSqlSettings:ProfileDbAdminPassword"; | ||
private const string ProfileDbConnectionStringKey = "PostgreSqlSettings:ProfileDbConnectionString"; | ||
private const string ConnectionStringKey = "PostgreSqlSettings:ConnectionString"; | ||
private const string ProfileDbPasswordKey = "PostgreSqlSettings:ProfileDbPwd"; | ||
|
||
/// <summary> | ||
/// Retrieves the database connection string from the configuration. | ||
/// </summary> | ||
/// <param name="config">The configuration instance containing the connection settings.</param> | ||
/// <returns>The formatted database connection string if all required settings are present; otherwise, an empty string.</returns> | ||
/// <remarks> | ||
/// This method expects the configuration to contain the following keys: | ||
/// This method expects IConfiguration to contain the following keys: | ||
/// <list type="bullet"> | ||
/// <item><description><c>PostgreSqlSettings--ProfileDbAdminUserName</c></description></item> | ||
/// <item><description><c>PostgreSqlSettings--ProfileDbAdminPassword</c></description></item> | ||
/// <item><description><c>PostgreSqlSettings--ProfileDbConnectionString</c></description></item> | ||
/// <item><description><c>PostgreSqlSettings:ConnectionString</c></description></item> | ||
/// <item><description><c>PostgreSqlSettings:ProfileDbPwd</c></description></item> | ||
/// </list> | ||
/// The connection string is formatted using the administrator user name and password. | ||
/// The connection string is expected to contain a placeholder for the password. The password is added to the connection string | ||
/// through string formatting. The connection string value should be provieded as a value in the helm chart for profile. The password | ||
/// is retrieved from the platform KeyVault. The names can therefore not be changed, but must follow the above naming conventions. | ||
/// </remarks> | ||
public static string GetDatabaseConnectionString(this IConfiguration config) | ||
{ | ||
var adminUserName = config[ProfileDbAdminUserNameKey]; | ||
var adminPassword = config[ProfileDbAdminPasswordKey]; | ||
var connectionString = config[ProfileDbConnectionStringKey]; | ||
var connectionString = config[ConnectionStringKey]; | ||
var userPassword = config[ProfileDbPasswordKey]; | ||
|
||
if (string.IsNullOrWhiteSpace(adminUserName) || | ||
string.IsNullOrWhiteSpace(adminPassword) || | ||
if (string.IsNullOrWhiteSpace(userPassword) || | ||
string.IsNullOrWhiteSpace(connectionString)) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
return string.Format(connectionString, adminUserName, adminPassword); | ||
return string.Format(connectionString, userPassword); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/Altinn.Profile.Integrations/Extensions/WebApplicationExtensions.cs
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,54 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
using Altinn.Profile.Integrations.Persistence; | ||
|
||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
using Yuniql.AspNetCore; | ||
using Yuniql.PostgreSql; | ||
|
||
namespace Altinn.Profile.Integrations.Extensions; | ||
|
||
/// <summary> | ||
/// Extension class for web application | ||
/// </summary> | ||
[ExcludeFromCodeCoverage] | ||
public static class WebApplicationExtensions | ||
{ | ||
/// <summary> | ||
/// Configure and set up db | ||
/// </summary> | ||
/// <param name="app">app</param> | ||
/// <param name="isDevelopment">is environment dev</param> | ||
/// <param name="config">the configuration collection</param> | ||
public static void SetUpPostgreSql(this IApplicationBuilder app, bool isDevelopment, IConfiguration config) | ||
{ | ||
PostgreSqlSettings? settings = config.GetSection("PostgreSQLSettings") | ||
.Get<PostgreSqlSettings>() | ||
?? throw new ArgumentNullException(nameof(config), "Required PostgreSQLSettings is missing from application configuration"); | ||
|
||
if (settings.EnableDBConnection) | ||
{ | ||
ConsoleTraceService traceService = new() { IsDebugEnabled = true }; | ||
|
||
string connectionString = string.Format(settings.AdminConnectionString, settings.ProfileDbAdminPwd); | ||
|
||
string fullWorkspacePath = isDevelopment ? | ||
Path.Combine(Directory.GetParent(Environment.CurrentDirectory)!.FullName, settings.MigrationScriptPath) : | ||
Path.Combine(Environment.CurrentDirectory, settings.MigrationScriptPath); | ||
|
||
app.UseYuniql( | ||
new PostgreSqlDataService(traceService), | ||
new PostgreSqlBulkImportService(traceService), | ||
traceService, | ||
new Configuration | ||
{ | ||
Workspace = fullWorkspacePath, | ||
ConnectionString = connectionString, | ||
IsAutoCreateDatabase = false, | ||
IsDebug = settings.EnableDebug | ||
}); | ||
} | ||
} | ||
} |
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,2 @@ | ||
# The `_draft` directory | ||
Scripts in progress. Scripts that you are currently working and have not moved to specific version directory yet. Executed every time after the latest version. |
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,2 @@ | ||
# The `_erase` directory | ||
Database cleanup scripts. Executed once only when you do `yuniql erase`. |
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,2 @@ | ||
# The `_init` directory | ||
Initialization scripts. Executed once. This is called the first time you do `yuniql run`. |
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,2 @@ | ||
# The `_post` directory | ||
Post migration scripts. Executed every time and always the last batch to run. |
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,2 @@ | ||
# The `_pre` directory | ||
Pre migration scripts. Executed every time before any version. |
2 changes: 2 additions & 0 deletions
2
src/Altinn.Profile.Integrations/Migration/v0.00/01-setup-schema.sql
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,2 @@ | ||
-- Create schema if it doesn't exist | ||
CREATE SCHEMA IF NOT EXISTS contact_and_reservation; |
3 changes: 3 additions & 0 deletions
3
src/Altinn.Profile.Integrations/Migration/v0.01/01-setup-grants.sql
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,3 @@ | ||
-- Grant access to the schema | ||
GRANT ALL ON SCHEMA contact_and_reservation TO platform_profile_admin; | ||
GRANT USAGE ON SCHEMA contact_and_reservation TO platform_profile; |
7 changes: 0 additions & 7 deletions
7
...file.Integrations/Migration/profiledb.sql → ...tions/Migration/v0.01/02-setup-tables.sql
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
77 changes: 77 additions & 0 deletions
77
src/Altinn.Profile.Integrations/Persistence/ConsoleTraceService.cs
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,77 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
using Yuniql.Extensibility; | ||
|
||
namespace Altinn.Profile.Integrations.Persistence; | ||
|
||
/// <summary> | ||
/// Copied from sample project. | ||
/// </summary> | ||
[ExcludeFromCodeCoverage] | ||
public class ConsoleTraceService : ITraceService | ||
{ | ||
/// <summary> | ||
/// Debug enabled | ||
/// </summary> | ||
public bool IsDebugEnabled { get; set; } = false; | ||
|
||
/// <inheritdoc/>> | ||
public bool IsTraceSensitiveData { get; set; } = false; | ||
|
||
/// <inheritdoc/>> | ||
public bool IsTraceToFile { get; set; } = false; | ||
|
||
/// <inheritdoc/>> | ||
public bool IsTraceToDirectory { get; set; } = false; | ||
|
||
/// <inheritdoc/>> | ||
public string? TraceDirectory { get; set; } | ||
|
||
/// <summary> | ||
/// Info | ||
/// </summary> | ||
public void Info(string message, object? payload = null) | ||
{ | ||
var traceMessage = $"INF {DateTime.UtcNow:o} {message}{Environment.NewLine}"; | ||
Console.Write(traceMessage); | ||
} | ||
|
||
/// <summary> | ||
/// Error | ||
/// </summary> | ||
public void Error(string message, object? payload = null) | ||
{ | ||
var traceMessage = $"ERR {DateTime.UtcNow:o} {message}{Environment.NewLine}"; | ||
Console.Write(traceMessage); | ||
} | ||
|
||
/// <summary> | ||
/// Debug | ||
/// </summary> | ||
public void Debug(string message, object? payload = null) | ||
{ | ||
if (IsDebugEnabled) | ||
{ | ||
var traceMessage = $"DBG {DateTime.UtcNow:o} {message}{Environment.NewLine}"; | ||
Console.Write(traceMessage); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Success | ||
/// </summary> | ||
public void Success(string message, object? payload = null) | ||
{ | ||
var traceMessage = $"INF {DateTime.UtcNow:u} {message}{Environment.NewLine}"; | ||
Console.Write(traceMessage); | ||
} | ||
|
||
/// <summary> | ||
/// Warn | ||
/// </summary> | ||
public void Warn(string message, object? payload = null) | ||
{ | ||
var traceMessage = $"WRN {DateTime.UtcNow:o} {message}{Environment.NewLine}"; | ||
Console.Write(traceMessage); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Altinn.Profile.Integrations/Persistence/PostgreSQLSettings.cs
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 @@ | ||
namespace Altinn.Profile.Integrations.Persistence; | ||
|
||
/// <summary> | ||
/// Settings for Postgre database | ||
/// </summary> | ||
public class PostgreSqlSettings | ||
{ | ||
/// <summary> | ||
/// Boolean indicating if database should be connected | ||
/// </summary> | ||
public bool EnableDBConnection { get; set; } = true; | ||
|
||
/// <summary> | ||
/// Path to migration scripts | ||
/// </summary> | ||
public string MigrationScriptPath { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Connection string for the admin user of postgre db | ||
/// </summary> | ||
public string AdminConnectionString { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Password for admin user for the postgre db | ||
/// </summary> | ||
public string ProfileDbAdminPwd { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Connection string for app user the postgre db | ||
/// </summary> | ||
public string ConnectionString { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Password for app user for the postgre db | ||
/// </summary> | ||
public string ProfileDbPwd { get; set; } = string.Empty; | ||
|
||
/// <summary> | ||
/// Gets or sets a value indicating whether to include parameter values in logging/tracing | ||
/// </summary> | ||
public bool LogParameters { get; set; } = false; | ||
|
||
/// <summary> | ||
/// Boolean indicating if connection to db should be in debug mode | ||
/// </summary> | ||
public bool EnableDebug { get; set; } = false; | ||
} |
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
Oops, something went wrong.