diff --git a/Selector.CLI/Command/MigrateCommand.cs b/Selector.CLI/Command/MigrateCommand.cs new file mode 100644 index 0000000..335a5e3 --- /dev/null +++ b/Selector.CLI/Command/MigrateCommand.cs @@ -0,0 +1,55 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using Selector.CLI.Extensions; +using Selector.Model; +using System; +using System.CommandLine; +using System.CommandLine.Invocation; + +namespace Selector.CLI +{ + public class MigrateCommand : Command + { + public MigrateCommand(string name, string description = null) : base(name, description) + { + var connectionString = new Option("--connection", "database to migrate"); + connectionString.AddAlias("-c"); + AddOption(connectionString); + + Handler = CommandHandler.Create((string connectionString) => Execute(connectionString)); + } + + public static int Execute(string connectionString) + { + try + { + var context = new CommandContext().WithLogger().WithDb(connectionString).WithLastfmApi(); + var logger = context.Logger.CreateLogger("Scrobble"); + + using var db = new ApplicationDbContext(context.DatabaseConfig.Options, context.Logger.CreateLogger()); + + logger.LogInformation("Preparing to migrate ({})", db.Database.GetConnectionString()); + + Console.WriteLine("Migrate database? (y/n) "); + var input = Console.ReadLine(); + + if (input.Trim().Equals("y", StringComparison.OrdinalIgnoreCase)) + { + logger.LogInformation("Migrating database"); + db.Database.Migrate(); + } + else + { + logger.LogInformation("Exiting"); + } + } + catch (Exception ex) + { + Console.WriteLine(ex); + return 1; + } + + return 0; + } + } +} diff --git a/Selector.CLI/Command/Scrobble/ScrobbleSave.cs b/Selector.CLI/Command/Scrobble/ScrobbleSave.cs index 1e60475..a507436 100644 --- a/Selector.CLI/Command/Scrobble/ScrobbleSave.cs +++ b/Selector.CLI/Command/Scrobble/ScrobbleSave.cs @@ -52,7 +52,7 @@ namespace Selector.CLI var context = new CommandContext().WithLogger().WithDb().WithLastfmApi(); var logger = context.Logger.CreateLogger("Scrobble"); - var db = new ApplicationDbContext(context.DatabaseConfig.Options, context.Logger.CreateLogger()); + using var db = new ApplicationDbContext(context.DatabaseConfig.Options, context.Logger.CreateLogger()); logger.LogInformation("Running from {0} to {1}", from, to); diff --git a/Selector.CLI/Extensions/CommandContextExtensions.cs b/Selector.CLI/Extensions/CommandContextExtensions.cs index a656c03..a23dbaf 100644 --- a/Selector.CLI/Extensions/CommandContextExtensions.cs +++ b/Selector.CLI/Extensions/CommandContextExtensions.cs @@ -34,7 +34,7 @@ namespace Selector.CLI.Extensions return context; } - public static CommandContext WithDb(this CommandContext context) + public static CommandContext WithDb(this CommandContext context, string connectionString = null) { if (context.Config is null) { @@ -42,7 +42,7 @@ namespace Selector.CLI.Extensions } context.DatabaseConfig = new DbContextOptionsBuilder(); - context.DatabaseConfig.UseNpgsql(context.Config.DatabaseOptions.ConnectionString); + context.DatabaseConfig.UseNpgsql(string.IsNullOrWhiteSpace(connectionString) ? context.Config.DatabaseOptions.ConnectionString : connectionString); return context; } diff --git a/Selector.CLI/Program.cs b/Selector.CLI/Program.cs index f2c1735..fb0cc5c 100644 --- a/Selector.CLI/Program.cs +++ b/Selector.CLI/Program.cs @@ -8,6 +8,7 @@ namespace Selector.CLI { var cmd = new HostRootCommand(); cmd.AddCommand(new ScrobbleCommand("scrobble", "Manipulate scrobbles")); + cmd.AddCommand(new MigrateCommand("migrate", "Migrate database")); cmd.Invoke(args); }