Преглед на файлове

fix resultCode principles in DbInstallCmd

Dalibor Votruba преди 3 години
родител
ревизия
cebc3f553a
променени са 4 файла, в които са добавени 88 реда и са изтрити 61 реда
  1. 45 45
      Modules/qdr.app.qlbrc.base/QlbrcDbContext.cs
  2. 36 13
      qdr.app.qlbrc.console/Commands/DbInstallCmd.cs
  3. 5 1
      qdr.app.qlbrc.console/Program.cs
  4. 2 2
      qdr.app.qlbrc.console/readme.txt

+ 45 - 45
Modules/qdr.app.qlbrc.base/QlbrcDbContext.cs

@@ -1,46 +1,46 @@
-using Microsoft.EntityFrameworkCore;
-using Quadarax.Application.QLiberace.Base.Entities;
-using Quadarax.Application.QLiberace.Common.Attributes;
+using Microsoft.EntityFrameworkCore;
+using Quadarax.Application.QLiberace.Base.Entities;
+using Quadarax.Application.QLiberace.Common.Attributes;
 using Quadarax.Application.QLiberace.Common.Configuration;
-using Quadarax.Foundation.Core.Reflection;
-
-namespace Quadarax.Application.QLiberace.Base
-{
-    [DbContextModuleAssignment(Constants.Modules.Base.Code)]
-    public class QlbrcDbContext : DbContext
-    {
-
-        #region *** Mapped entities ***
-        public DbSet<User> Users { get; set; }
-        public DbSet<Tenant> Tenants { get; set; }
-
-        public DbSet<Setting> Settings { get; set; }
-
-        #endregion
-        
-        #region *** Constructors ***
-        public QlbrcDbContext() : base()
-        {
-        }
-        public QlbrcDbContext(DbContextOptions options) : base(options)
-        {
-        }
-        #endregion
-
-
-        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
-        {
-            optionsBuilder.UseSqlServer(GetConnectionString());
-        }
-
-        private string GetConnectionString()
-        {
-            var attr = AttributeQuery<DbContextModuleAssignmentAttribute>.Get(typeof(QlbrcDbContext));
-            if (attr == null)
-                throw new InvalidOperationException($"No connection string defined for DbContext {this.GetType().Name} or DbContextModuleAssignment is missing.");
-
-            return OlbrcConfiguration.Get(attr.ModuleCode).ConnectionString;
-        }
-
-    }
-}
+using Quadarax.Foundation.Core.Reflection;
+
+namespace Quadarax.Application.QLiberace.Base
+{
+    [DbContextModuleAssignment(Constants.Modules.Base.Code)]
+    public class QlbrcDbContext : DbContext
+    {
+
+        #region *** Mapped entities ***
+        public DbSet<User> Users { get; set; }
+        public DbSet<Tenant> Tenants { get; set; }
+
+        public DbSet<Setting> Settings { get; set; }
+
+        #endregion
+        
+        #region *** Constructors ***
+        public QlbrcDbContext() : base()
+        {
+        }
+        public QlbrcDbContext(DbContextOptions options) : base(options)
+        {
+        }
+        #endregion
+
+
+        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
+        {
+            optionsBuilder.UseSqlServer(GetConnectionString());
+        }
+
+        private string GetConnectionString()
+        {
+            var attr = AttributeQuery<DbContextModuleAssignmentAttribute>.Get(typeof(QlbrcDbContext));
+            if (attr == null)
+                throw new InvalidOperationException($"No connection string defined for DbContext {this.GetType().Name} or DbContextModuleAssignment is missing.");
+
+            return OlbrcConfiguration.Get(attr.ModuleCode).ConnectionString;
+        }
+
+    }
+}

+ 36 - 13
qdr.app.qlbrc.console/Commands/DbInstallCmd.cs

@@ -1,4 +1,6 @@
-using Quadarax.Application.QLiberace.Base;
+using System.Linq.Expressions;
+using Microsoft.Data.SqlClient;
+using Quadarax.Application.QLiberace.Base;
 using Quadarax.Application.QLiberace.Console.Commands.Base;
 using Quadarax.Foundation.Core.QConsole.Attributes;
 using Quadarax.Foundation.Core.QConsole;
@@ -35,26 +37,47 @@ namespace Quadarax.Application.QLiberace.Console.Commands
         protected override Result OnExecute(QlbrcDbContext context)
         {
             WriteInfo("Ensuring database...");
-            if (context.Database.EnsureCreated())
-                WriteInfo("Database already exists.");
-            else
-                WriteInfo("Database created.");
-
-            if (_isLatestUpdateEnabled)
+            var resultCode = 0;
+            // TODO: handle this exception as resultCode = 1 on context.Database.EnsureCreated()
+            // Microsoft.Data.SqlClient.SqlException: 'A network-related or instance-specific error occurred while establishing a connection to SQL Server
+            try
             {
-                WriteInfo("Checking pending migrations.");
-                if (CheckPendingMigrations(context))
+                if (!context.Database.EnsureCreated())
                 {
-                    WriteInfo("New pending migrations detected.");
-                    ApplyMigration(context);
+                    WriteInfo("Database already exists.");
+                    resultCode = 1;
                 }
                 else
                 {
-                    WriteInfo("Nothing found.");
+                    WriteInfo("Database created.");
                 }
+
+                if (_isLatestUpdateEnabled)
+                {
+                    WriteInfo("Checking pending migrations.");
+                    if (CheckPendingMigrations(context))
+                    {
+                        WriteInfo("New pending migrations detected.");
+                        ApplyMigration(context);
+                    }
+                    else
+                    {
+                        WriteInfo("Nothing found.");
+                    }
+                }
+            }
+            catch (SqlException e)
+            {
+                if (e.Message.Contains("A network-related or instance-specific error occurred while establishing a connection to SQL Server"))
+                {
+                    WriteError(e);
+                    return new Result(10);
+                }
+
+                throw e;
             }
 
-            return new Result();
+            return new Result(resultCode);
         }
 
         

+ 5 - 1
qdr.app.qlbrc.console/Program.cs

@@ -1,6 +1,7 @@
 // See https://aka.ms/new-console-template for more information
 
 using System.Reflection;
+using Quadarax.Foundation.Core.Console;
 using Quadarax.Foundation.Core.QConsole;
 using Quadarax.Foundation.Core.QConsole.Configuration;
 using Quadarax.Foundation.Core.QConsole.Context;
@@ -29,4 +30,7 @@ config.IsConsoleDebug = false;
 var console = new Engine(config, new DefualtEngineContext(), args);
 console.Start();
 
-Environment.Exit(console.ReturnCode);;
+using (var writer = new ConsoleWriter(console.ReturnCode == 0 ? ConsoleColor.DarkGreen : console.ReturnCode < 9 ? ConsoleColor.DarkYellow : ConsoleColor.DarkRed))
+    writer.WriteLine($"Exiting with return code {console.ReturnCode}");
+
+Environment.Exit(console.ReturnCode);

+ 2 - 2
qdr.app.qlbrc.console/readme.txt

@@ -38,6 +38,6 @@ db_install
 
 	exit codes:
 		0		Ok
-		1		Cannot connect to server
-		2		Database already axists
+		1		Database already axists
+		10		Cannot connect to server		
 		100		Other unhandled exception