Files
gwms/GWMS.Data/DbAdmin.cs
T
Samuele Locatelli 179d19cd94 RImossa migrazione DB
2021-08-02 12:02:17 +02:00

100 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using NLog;
namespace GWMS.Data
{
public class DbAdmin : IDisposable
{
#region Private Fields
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
#endregion Private Fields
#region Public Constructors
public DbAdmin()
{
}
#endregion Public Constructors
#region Public Methods
public static bool checkCreateUser(string username, string pwd)
{
bool answ = false;
using (AdminContext adbCtx = new AdminContext())
{
// ricerca utente...
var numUser = adbCtx
.UserList
.Where(x => x.User == username)
.ToList()
.Count;
if (numUser > 0)
{
answ = true;
}
if (!answ)
{
// creo utente
string sqlCommand = "FLUSH PRIVILEGES;";
adbCtx.Database.ExecuteSqlRaw(sqlCommand);
sqlCommand = $"CREATE USER '{username}'@'localhost' IDENTIFIED BY '{pwd}';";
adbCtx.Database.ExecuteSqlRaw(sqlCommand);
sqlCommand = $"GRANT ALL ON *.* TO '{username}'@'localhost';";
adbCtx.Database.ExecuteSqlRaw(sqlCommand);
sqlCommand = "FLUSH PRIVILEGES;";
adbCtx.Database.ExecuteSqlRaw(sqlCommand);
}
}
return answ;
}
public static async Task<bool> migrateDbIdentity()
{
bool answ = false;
using (UserIdentityDbContext dbCtx = new UserIdentityDbContext())
{
await dbCtx.Database.MigrateAsync();
answ = true;
}
return answ;
}
public static async Task<bool> migrateDbMain()
{
bool answ = false;
using (GWMSContext dbCtx = new GWMSContext())
{
await dbCtx.Database.MigrateAsync();
answ = true;
}
return answ;
}
public static bool resetPlantLogTable()
{
bool answ = false;
using (GWMSContext dbCtx = new GWMSContext())
{
string sqlCommand = "TRUNCATE TABLE PlantLog;";
dbCtx.Database.ExecuteSqlRaw(sqlCommand);
answ = true;
}
return answ;
}
public void Dispose()
{
}
#endregion Public Methods
}
}