|
|
@@ -0,0 +1,98 @@
|
|
|
+using Microsoft.Data.SqlClient;
|
|
|
+using Quadarax.Application.QLiberace.Base;
|
|
|
+using Quadarax.Application.QLiberace.Console.Commands.Base;
|
|
|
+using Quadarax.Foundation.Core.QConsole.Argument;
|
|
|
+using Quadarax.Foundation.Core.QConsole;
|
|
|
+using Quadarax.Foundation.Core.Value;
|
|
|
+using Microsoft.EntityFrameworkCore;
|
|
|
+using Quadarax.Foundation.Core.QConsole.Attributes;
|
|
|
+
|
|
|
+namespace Quadarax.Application.QLiberace.Console.Commands
|
|
|
+{
|
|
|
+ [CommandDefinition]
|
|
|
+ internal class DbUpdateCmd : DbMigrationCommand
|
|
|
+ {
|
|
|
+ #region *** Properties ***
|
|
|
+
|
|
|
+ public override string Name => Constants.Commands.DbUpdate.Name;
|
|
|
+ public override string Description => Constants.Commands.DbUpdate.Description;
|
|
|
+
|
|
|
+ #endregion
|
|
|
+ #region *** Fields ***
|
|
|
+
|
|
|
+ private bool _isListOnly;
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region *** Constructor ***
|
|
|
+
|
|
|
+ public DbUpdateCmd(Engine engine) : base(engine)
|
|
|
+ {
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region *** EXECUTE ***
|
|
|
+ protected override Result OnExecute(QlbrcDbContext context)
|
|
|
+ {
|
|
|
+ WriteInfo("Connecting database...");
|
|
|
+ var resultCode = 0;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (context.Database.CanConnect())
|
|
|
+ {
|
|
|
+ if (_isListOnly)
|
|
|
+ {
|
|
|
+ WriteInfo("Pending migrations list:");
|
|
|
+ var migrations = context.Database.GetPendingMigrations().ToArray();
|
|
|
+ if (migrations.Length > 0)
|
|
|
+ {
|
|
|
+ foreach (var mig in migrations)
|
|
|
+ WriteInfo(mig);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ WriteInfo("No pending migrations present.");
|
|
|
+ }
|
|
|
+
|
|
|
+ WriteWarning("No migration applied.");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ ApplyMigration(context);
|
|
|
+ WriteWarning("Migration applied.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ WriteWarning("Database doesn't exists. Use db_install first.");
|
|
|
+ return new Result(Constants.Commands.DbUpdate.ReturnCodes.DatabaseNotExists);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (SqlException e)
|
|
|
+ {
|
|
|
+ return HandleConnectionException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return new Result(resultCode);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region *** Private operations ***
|
|
|
+ protected override void OnValidateArguments()
|
|
|
+ {
|
|
|
+ base.OnValidateArguments();
|
|
|
+ if (ContainsArgument(Constants.Commands.DbUpdate.Arguments.List.Name))
|
|
|
+ _isListOnly = GetArgumentValueOrDefault<bool>(Constants.Commands.DbUpdate.Arguments.List.Name);
|
|
|
+ }
|
|
|
+ protected override IEnumerable<AbstractArgument> OnSetupArguments()
|
|
|
+ {
|
|
|
+ var arguments = new List<AbstractArgument>(base.OnSetupArguments());
|
|
|
+ arguments.Add(new FlagArgument(Constants.Commands.DbUpdate.Arguments.List.Name,
|
|
|
+ Constants.Commands.DbUpdate.Arguments.List.Description,
|
|
|
+ Constants.Commands.DbUpdate.Arguments.List.Hint, false));
|
|
|
+ return arguments;
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+ }
|
|
|
+}
|