Skip to content
Merged
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
36 changes: 23 additions & 13 deletions PasswordKeeper.DatabaseMigrations/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Text.RegularExpressions;
using FluentMigrator.Runner;
using Microsoft.Extensions.DependencyInjection;
using McMaster.Extensions.CommandLineUtils;
using Microsoft.Extensions.DependencyInjection;
using PasswordKeeper.Classes;

// ReSharper disable MemberCanBePrivate.Global
Expand Down Expand Up @@ -35,8 +35,13 @@ public static int Main(string[] args)
/// </summary>
[Option(template: "-c|--connection", Description = "Use the test database")]
public string ConnectionString { get; } = string.Empty;



/// <summary>
/// A flag to indicate whether to create the database if it doesn't exist.
/// </summary>
[Option(template: "-m|--makeDatabase", Description = "Create the database")]
public bool MakeDatabase { get; } = false;

/// <summary>
/// The entry point for the application.
/// </summary>
Expand All @@ -61,8 +66,6 @@ public void OnExecute()
IsTestDb = false;
connectionString = ConnectionString;

Console.WriteLine(connectionString);

// Replace the database name to sys, that should always exist in a MariaDB server
var connectionStringMaster = Regex.Replace(connectionString, "Database=.*?(;|$)", "database=sys;");

Expand All @@ -79,16 +82,23 @@ public void OnExecute()
connectionString = Regex.Replace(connectionString, "Database=.*?(;|$)", $"Database={DatabaseName}$1");
}

// Create the database, this is done before the migrations are run
using var connection = new MySql.Data.MySqlClient.MySqlConnection(connectionStringMaster);
connection.Open();
// Extract the database name from the connection string
DatabaseName = Regex.Match(connectionString, "Database=.*?(;|$)").Value.Replace("Database=", "")
.Replace(";", "");

using var command = connection.CreateCommand();
if (MakeDatabase)
{
// Create the database, this is done before the migrations are run
using var connection = new MySql.Data.MySqlClient.MySqlConnection(connectionStringMaster);
connection.Open();

using var command = connection.CreateCommand();

command.CommandText = $"CREATE DATABASE IF NOT EXISTS {DatabaseName}";
command.ExecuteNonQuery();
command.CommandText = $"CREATE DATABASE IF NOT EXISTS {DatabaseName}";
command.ExecuteNonQuery();

connection.Close();
connection.Close();
}
}

using var serviceProvider = IsTestDb ? CreateTestServices(connectionString) : CreateServices(connectionString);
Expand Down Expand Up @@ -153,5 +163,5 @@ private static void UpdateDatabase(IServiceProvider serviceProvider)
/// <summary>
/// The name of the database.
/// </summary>
public const string DatabaseName = "password_keeper";
public static string DatabaseName { get; set; } = "password_keeper";
}
1 change: 1 addition & 0 deletions PasswordKeeper.Server/PasswordKeeper.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<ProjectReference Include="..\PasswordKeeper.Classes\PasswordKeeper.Classes.csproj" />
<ProjectReference Include="..\PasswordKeeper.DAO\PasswordKeeper.DAO.csproj" />
<ProjectReference Include="..\PasswordKeeper.DataAccess\PasswordKeeper.DataAccess.csproj" />
<ProjectReference Include="..\PasswordKeeper.DatabaseMigrations\PasswordKeeper.DatabaseMigrations.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
17 changes: 16 additions & 1 deletion PasswordKeeper.Server/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text.Json;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -60,6 +59,22 @@ public static void Main(string[] args)
_connectionString =
builder.Configuration.GetValue<string>("DefaultConnection")
?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");

var migrateEnabled = builder.Configuration.GetValue<bool>("Migrate:Enabled");
var createDatabase = builder.Configuration.GetValue<bool>("Migrate:CreateDatabase");

// Run database migrations, if enabled
if (migrateEnabled)
{
if (createDatabase)
{
PasswordKeeper.DatabaseMigrations.Program.Main(["-c", _connectionString, "-m",]);
}
else
{
PasswordKeeper.DatabaseMigrations.Program.Main(["-c", _connectionString,]);
}
}

// Add the database context
builder.Services.AddDbContextFactory<Entities>(options => options.UseMySQL(_connectionString));
Expand Down
6 changes: 5 additions & 1 deletion PasswordKeeper.Server/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
"Microsoft.AspNetCore": "Warning"
}
},
"DefaultConnection": "Server=localhost;Port=3306;Database=password_keeper;Uid=root;Pwd=strong_password_here;"
"DefaultConnection": "Server=localhost;Port=3306;Database=password_keeper;Uid=root;Pwd=strong_password_here;",
"Migrate": {
"Enabled": true,
"CreateDatabase": true
}
}
8 changes: 6 additions & 2 deletions PasswordKeeper.Server/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
}
},
"AllowedHosts": "*",
"DefaultConnection": "Server=localhost;Port=3306;Database=password_keeper;Uid=root;Pwd=strong_password_here;"
}
"DefaultConnection": "Server=localhost;Port=3306;Database=password_keeper;Uid=root;Pwd=strong_password_here;",
"Migrate": {
"Enabled": false,
"CreateDatabase": false
}
}