diff --git a/PasswordKeeper.DatabaseMigrations/Program.cs b/PasswordKeeper.DatabaseMigrations/Program.cs index 6e9d1e6..c660d7a 100644 --- a/PasswordKeeper.DatabaseMigrations/Program.cs +++ b/PasswordKeeper.DatabaseMigrations/Program.cs @@ -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 @@ -35,8 +35,13 @@ public static int Main(string[] args) /// [Option(template: "-c|--connection", Description = "Use the test database")] public string ConnectionString { get; } = string.Empty; - - + + /// + /// A flag to indicate whether to create the database if it doesn't exist. + /// + [Option(template: "-m|--makeDatabase", Description = "Create the database")] + public bool MakeDatabase { get; } = false; + /// /// The entry point for the application. /// @@ -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;"); @@ -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); @@ -153,5 +163,5 @@ private static void UpdateDatabase(IServiceProvider serviceProvider) /// /// The name of the database. /// - public const string DatabaseName = "password_keeper"; + public static string DatabaseName { get; set; } = "password_keeper"; } \ No newline at end of file diff --git a/PasswordKeeper.Server/PasswordKeeper.Server.csproj b/PasswordKeeper.Server/PasswordKeeper.Server.csproj index f279d39..caf732d 100644 --- a/PasswordKeeper.Server/PasswordKeeper.Server.csproj +++ b/PasswordKeeper.Server/PasswordKeeper.Server.csproj @@ -25,6 +25,7 @@ + diff --git a/PasswordKeeper.Server/Program.cs b/PasswordKeeper.Server/Program.cs index 6ac0750..364ba7c 100644 --- a/PasswordKeeper.Server/Program.cs +++ b/PasswordKeeper.Server/Program.cs @@ -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; @@ -60,6 +59,22 @@ public static void Main(string[] args) _connectionString = builder.Configuration.GetValue("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found."); + + var migrateEnabled = builder.Configuration.GetValue("Migrate:Enabled"); + var createDatabase = builder.Configuration.GetValue("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(options => options.UseMySQL(_connectionString)); diff --git a/PasswordKeeper.Server/appsettings.Development.json b/PasswordKeeper.Server/appsettings.Development.json index 268382e..ea0874c 100644 --- a/PasswordKeeper.Server/appsettings.Development.json +++ b/PasswordKeeper.Server/appsettings.Development.json @@ -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 + } } diff --git a/PasswordKeeper.Server/appsettings.json b/PasswordKeeper.Server/appsettings.json index e7a9bf8..fd54c3c 100644 --- a/PasswordKeeper.Server/appsettings.json +++ b/PasswordKeeper.Server/appsettings.json @@ -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 + } +} \ No newline at end of file