Fix con correzione tab migration

This commit is contained in:
Samuele Locatelli
2024-06-28 07:24:34 +02:00
parent f12b98a3c3
commit e854d97b74
8 changed files with 103 additions and 66 deletions
+6 -3
View File
@@ -32,9 +32,12 @@ namespace EgtBEAMWALL.DataLayer
/// </summary>
public DbSet<UserPriv> UserList { get; set; }
public DbSet<MigrationHistoryModel> MigrList { get; set; }
#if false
/// <summary>
/// Oggetto appoggio x conteggi SQL
/// </summary>
public virtual DbSet<TableCount> DbSetCounts { get; set; }
#endif
#endregion Public Properties
@@ -1,7 +1,9 @@
using NLog;
using System;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Threading;
using System.Threading.Tasks;
namespace EgtBEAMWALL.DataLayer.Controllers
{
@@ -41,7 +43,7 @@ namespace EgtBEAMWALL.DataLayer.Controllers
{
bool answ = false;
string domain = isNetwork ? "%" : "localhost";
answ = doCheckCreate(username, pwd, answ, domain, false);
answ = doCheckCreate(username, pwd, domain, false);
if (answ)
{
doGrantPriv(username, domain, "ALL ON *.*");
@@ -80,14 +82,25 @@ namespace EgtBEAMWALL.DataLayer.Controllers
{
// migrazione minima x eseguire refresh viste
string minMigration = "202404261445205_UpdateLogMachineTable";
// cerco se ho la migration minima per proseguire...
var numRec = dbCtx
.MigrList
.Where(x => x.MigrationId == minMigration)
.ToList()
.Count;
//// cerco se ho la migration minima per proseguire...
//var numRec = dbCtx
// .MigrList
// .Where(x => x.MigrationId == minMigration)
// .ToList()
// .Count;
if (numRec == 0)
//string sqlQuery = $"SELECT 'Migration' AS TableName, COUNT(*) AS Count FROM __MigrationHistory WHERE MigrationId = '{minMigration}' LIMIT 1;";
//var table = dbCtx
// .DbSetCounts
// .SqlQuery(sqlQuery)
// .AsNoTracking()
//.ToList();
string sqlQuery = $"SELECT COUNT(*) AS Count FROM __MigrationHistory WHERE MigrationId = '{minMigration}' LIMIT 1;";
var total = dbCtx.Database.SqlQuery<int>(sqlQuery).Single();
//if (table != null && table.Count > 0)
if (total > 0)
{
// in primis refresh delle view
string sqlCommand = @"CREATE OR REPLACE VIEW v_expProdRaw AS
@@ -112,7 +125,7 @@ namespace EgtBEAMWALL.DataLayer.Controllers
string domain = "%";
// ora controllo utente con diritti
answ = doCheckCreate(username, pwd, answ, domain, true);
answ = doCheckCreate(username, pwd, domain, true);
if (answ)
{
doGrantPriv(username, domain, "USAGE ON *.*");
@@ -177,43 +190,49 @@ namespace EgtBEAMWALL.DataLayer.Controllers
/// </summary>
/// <param name="username"></param>
/// <param name="pwd"></param>
/// <param name="answ"></param>
/// <param name="domain"></param>
/// <param name="force">Force = esegue un drop prima...</param>
/// <returns></returns>
private bool doCheckCreate(string username, string pwd, bool answ, string domain, bool force)
private bool doCheckCreate(string username, string pwd, string domain, bool force)
{
// ricerca utente...
var numUser = adbCtx
.UserList
.Where(x => x.User == username && x.Host == domain)
.ToList()
.Count;
if (numUser > 0)
bool answ = false;
try
{
answ = true;
}
if (!answ)
{
// creo utente
string sqlCommand = $"DROP USER IF EXISTS {username};";
if (force)
// ricerca utente...
var numUser = adbCtx
.UserList
.Where(x => x.User == username && x.Host == domain)
.ToList()
.Count;
if (numUser > 0)
{
answ = true;
}
if (!answ)
{
// creo utente
string sqlCommand = $"DROP USER IF EXISTS {username};";
if (force)
{
adbCtx.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, sqlCommand);
Thread.Sleep(100);
}
sqlCommand = "FLUSH PRIVILEGES;";
adbCtx.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, sqlCommand);
Thread.Sleep(100);
sqlCommand = $"CREATE USER '{username}'@'{domain}' IDENTIFIED BY '{pwd}';";
adbCtx.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, sqlCommand);
Thread.Sleep(100);
sqlCommand = "FLUSH PRIVILEGES;";
adbCtx.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, sqlCommand);
Thread.Sleep(100);
answ = true;
}
sqlCommand = "FLUSH PRIVILEGES;";
adbCtx.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, sqlCommand);
Thread.Sleep(100);
sqlCommand = $"CREATE USER '{username}'@'{domain}' IDENTIFIED BY '{pwd}';";
adbCtx.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, sqlCommand);
Thread.Sleep(100);
sqlCommand = "FLUSH PRIVILEGES;";
adbCtx.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, sqlCommand);
Thread.Sleep(100);
answ = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione in doCheckCreate:{Environment.NewLine}{exc}");
}
return answ;
}
@@ -244,13 +244,18 @@ namespace EgtBEAMWALL.DataLayer.Controllers
/// <returns></returns>
public ProjModel FindByProjDbId(int ProjDbId)
{
ProjModel answ;
ProjModel answ = new ProjModel();
using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
{
answ = localDbCtx
.ProjList
.Where(x => x.ProjDbId == ProjDbId)
.SingleOrDefault();
try
{
answ = localDbCtx
.ProjList
.Where(x => x.ProjDbId == ProjDbId)
.SingleOrDefault();
}
catch
{ }
}
return answ;
}
@@ -262,13 +267,18 @@ namespace EgtBEAMWALL.DataLayer.Controllers
/// <returns></returns>
public ProjModel FindByProjId(int ProjId)
{
ProjModel answ;
ProjModel answ = new ProjModel();
using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
{
answ = localDbCtx
.ProjList
.Where(x => x.ProjId == ProjId)
.SingleOrDefault();
try
{
answ = localDbCtx
.ProjList
.Where(x => x.ProjId == ProjId)
.SingleOrDefault();
}
catch
{ }
}
return answ;
}
@@ -136,12 +136,17 @@ namespace EgtBEAMWALL.DataLayer.Controllers
// Initialize database context
using (DatabaseContext localDbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING))
{
// retrieve
answ = localDbCtx
.StatusMapList
.Where(x => x.Session == Session && x.ItemType == Core.StatusMapItemType.Prod)
.OrderByDescending(x => x.Index)
.ToList();
try
{
// retrieve
answ = localDbCtx
.StatusMapList
.Where(x => x.Session == Session && x.ItemType == Core.StatusMapItemType.Prod)
.OrderByDescending(x => x.Index)
.ToList();
}
catch
{ }
}
return answ;
}
+1
View File
@@ -117,6 +117,7 @@ namespace EgtBEAMWALL.DataLayer
/// </summary>
public DbSet<MagmanSyncModel> SyncList { get; set; }
#endregion Public Properties
#region Public Methods
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -9,17 +8,15 @@ using System.Threading.Tasks;
namespace EgtBEAMWALL.DataLayer.DatabaseModels
{
/// <summary>
/// Tabella migrations del DB
/// Classe fake x il conteggio tabelle e check preliminari
/// </summary>
[Table("__migrationhistory")]
public class MigrationHistoryModel
[NotMapped]
public class TableCount
{
#region Public Properties
[Key]
public string MigrationId { get; set; } = "";
public string ProductVersion { get; set; } = "";
public int Count { get; set; }
public string TableName { get; set; } = "";
#endregion Public Properties
}
+4 -2
View File
@@ -90,8 +90,10 @@ namespace EgtBEAMWALL.DataLayer
/// <returns></returns>
public static bool CheckViews(string nKey, string sKey)
{
// esecuzione script di install locale
return Controllers.DbController.man.refreshViews(DATABASE_NAME);
return false;
// commento, scambiare con migrations da file?!?
//// esecuzione script di install locale
//return Controllers.DbController.man.refreshViews(DATABASE_NAME);
}
/// <summary>
@@ -163,8 +163,8 @@
<Compile Include="DatabaseModels\AliasModel.cs" />
<Compile Include="DatabaseModels\MagmanSyncModel.cs" />
<Compile Include="DatabaseModels\MaterialModel.cs" />
<Compile Include="DatabaseModels\MigrationHistoryModel.cs" />
<Compile Include="DatabaseModels\RawItemModel.cs" />
<Compile Include="DatabaseModels\TableCount.cs" />
<Compile Include="DatabaseModels\UserPrivModel.cs" />
<Compile Include="DbConfig.cs" />
<Compile Include="Controllers\LogMachineController.cs" />