adding migrate command
This commit is contained in:
parent
8a14f2a1db
commit
526b06775e
55
Selector.CLI/Command/MigrateCommand.cs
Normal file
55
Selector.CLI/Command/MigrateCommand.cs
Normal file
@ -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<string>("--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<ApplicationDbContext>());
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -52,7 +52,7 @@ namespace Selector.CLI
|
|||||||
var context = new CommandContext().WithLogger().WithDb().WithLastfmApi();
|
var context = new CommandContext().WithLogger().WithDb().WithLastfmApi();
|
||||||
var logger = context.Logger.CreateLogger("Scrobble");
|
var logger = context.Logger.CreateLogger("Scrobble");
|
||||||
|
|
||||||
var db = new ApplicationDbContext(context.DatabaseConfig.Options, context.Logger.CreateLogger<ApplicationDbContext>());
|
using var db = new ApplicationDbContext(context.DatabaseConfig.Options, context.Logger.CreateLogger<ApplicationDbContext>());
|
||||||
|
|
||||||
logger.LogInformation("Running from {0} to {1}", from, to);
|
logger.LogInformation("Running from {0} to {1}", from, to);
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ namespace Selector.CLI.Extensions
|
|||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CommandContext WithDb(this CommandContext context)
|
public static CommandContext WithDb(this CommandContext context, string connectionString = null)
|
||||||
{
|
{
|
||||||
if (context.Config is null)
|
if (context.Config is null)
|
||||||
{
|
{
|
||||||
@ -42,7 +42,7 @@ namespace Selector.CLI.Extensions
|
|||||||
}
|
}
|
||||||
|
|
||||||
context.DatabaseConfig = new DbContextOptionsBuilder<ApplicationDbContext>();
|
context.DatabaseConfig = new DbContextOptionsBuilder<ApplicationDbContext>();
|
||||||
context.DatabaseConfig.UseNpgsql(context.Config.DatabaseOptions.ConnectionString);
|
context.DatabaseConfig.UseNpgsql(string.IsNullOrWhiteSpace(connectionString) ? context.Config.DatabaseOptions.ConnectionString : connectionString);
|
||||||
|
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ namespace Selector.CLI
|
|||||||
{
|
{
|
||||||
var cmd = new HostRootCommand();
|
var cmd = new HostRootCommand();
|
||||||
cmd.AddCommand(new ScrobbleCommand("scrobble", "Manipulate scrobbles"));
|
cmd.AddCommand(new ScrobbleCommand("scrobble", "Manipulate scrobbles"));
|
||||||
|
cmd.AddCommand(new MigrateCommand("migrate", "Migrate database"));
|
||||||
|
|
||||||
cmd.Invoke(args);
|
cmd.Invoke(args);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user