mirror of
https://github.com/claunia/marechai.git
synced 2025-12-16 19:14:25 +00:00
Add database update up to first EF migration, and enable automatic migrations now on.
This commit is contained in:
@@ -35,7 +35,8 @@ namespace Cicm.Database
|
||||
public partial class Operations
|
||||
{
|
||||
/// <summary>Last known database version</summary>
|
||||
const int DB_VERSION = 23;
|
||||
const int DB_VERSION = 24;
|
||||
public const int DbVersionEntityFramework = 1984;
|
||||
/// <summary>The column with this value indicates there is no item of this type.</summary>
|
||||
public const int DB_NONE = -1;
|
||||
/// <summary>
|
||||
|
||||
@@ -184,6 +184,11 @@ namespace Cicm.Database
|
||||
UpdateDatabaseToV23();
|
||||
break;
|
||||
}
|
||||
case 23:
|
||||
{
|
||||
UpdateVersionToEntityFramework();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
OptimizeDatabase();
|
||||
@@ -2640,5 +2645,48 @@ namespace Cicm.Database
|
||||
dbCmd.CommandText = $"OPTIMIZE TABLE '{tableName}'";
|
||||
}
|
||||
}
|
||||
|
||||
public int GetVersion()
|
||||
{
|
||||
int currentDbVersion = 2;
|
||||
IDbCommand dbCmd = dbCon.CreateCommand();
|
||||
IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter();
|
||||
dbCmd.CommandText = "SELECT * FROM cicm_db";
|
||||
DataSet dataSet = new DataSet();
|
||||
dataAdapter.SelectCommand = dbCmd;
|
||||
dataAdapter.Fill(dataSet);
|
||||
|
||||
foreach(DataRow dataRow in dataSet.Tables[0].Rows)
|
||||
{
|
||||
int newId = int.Parse(dataRow["version"].ToString());
|
||||
if(newId > currentDbVersion) currentDbVersion = newId;
|
||||
}
|
||||
|
||||
return currentDbVersion;
|
||||
}
|
||||
|
||||
public void UpdateVersionToEntityFramework()
|
||||
{
|
||||
Console.WriteLine("Adding Entity Framework table...");
|
||||
IDbCommand dbCmd = dbCon.CreateCommand();
|
||||
dbCmd.CommandText = "create table `__EFMigrationsHistory`\n" +
|
||||
"(MigrationId varchar(95) not null primary key,\n" +
|
||||
"ProductVersion varchar(32) not null);";
|
||||
dbCmd.ExecuteNonQuery();
|
||||
dbCmd.Dispose();
|
||||
|
||||
Console.WriteLine("Adding Entity Framework first migration...");
|
||||
dbCmd = dbCon.CreateCommand();
|
||||
dbCmd.CommandText = "INSERT INTO cicm.`__EFMigrationsHistory` (MigrationId, ProductVersion)" +
|
||||
" VALUES ('20180805214952_InitialMigration', '2.1.1-rtm-30846');";
|
||||
dbCmd.ExecuteNonQuery();
|
||||
dbCmd.Dispose();
|
||||
|
||||
Console.WriteLine("Setting new database version to 1984 (Entity Framework)...");
|
||||
dbCmd = dbCon.CreateCommand();
|
||||
dbCmd.CommandText = "INSERT INTO cicm_db (version) VALUES ('1984')";
|
||||
dbCmd.ExecuteNonQuery();
|
||||
dbCmd.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,9 +30,12 @@
|
||||
|
||||
using System;
|
||||
using Cicm.Database;
|
||||
using Cicm.Database.Models;
|
||||
using DiscImageChef.Interop;
|
||||
using Microsoft.AspNetCore;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Version = DiscImageChef.Interop.Version;
|
||||
|
||||
namespace cicm_web
|
||||
@@ -106,18 +109,14 @@ namespace cicm_web
|
||||
DetectOS.IsMono ? "Mono" : ".NET Core",
|
||||
DetectOS.IsMono ? Version.GetMonoVersion() : Version.GetNetCoreVersion());
|
||||
|
||||
Console.WriteLine("\u001b[31;1mConnecting to MySQL database...\u001b[0m");
|
||||
Console.WriteLine("\u001b[31;1mUpdating MySQL database without Entity Framework if it exists...\u001b[0m");
|
||||
Database = new Mysql();
|
||||
bool res = Database.OpenDb("localhost", "cicm", "cicm", "cicmpass", 3306);
|
||||
if(!res)
|
||||
|
||||
if(res)
|
||||
{
|
||||
Console.WriteLine("\u001b[31;1mCould not open database, trying to create a new one...\u001b[0m");
|
||||
res = Database.CreateDb("localhost", "cicm", "cicm", "cicmpass", 3306);
|
||||
if(!res)
|
||||
{
|
||||
Console.WriteLine("\u001b[31;1mCould create database, exiting...\u001b[0m");
|
||||
return;
|
||||
}
|
||||
Console.WriteLine("\u001b[31;1mClosing database...\u001b[0m");
|
||||
Database.CloseDb();
|
||||
}
|
||||
|
||||
DateTime start = DateTime.Now;
|
||||
@@ -132,9 +131,31 @@ namespace cicm_web
|
||||
end = DateTime.Now;
|
||||
Console.WriteLine("\u001b[31;1mTook \u001b[32;1m{0} seconds\u001b[31;1m...\u001b[0m",
|
||||
(end - start).TotalSeconds);
|
||||
Console.WriteLine("\u001b[31;1mStarting web server...\u001b[0m");
|
||||
|
||||
BuildWebHost(args).Run();
|
||||
IWebHost host = BuildWebHost(args);
|
||||
|
||||
using(IServiceScope scope = host.Services.CreateScope())
|
||||
{
|
||||
IServiceProvider services = scope.ServiceProvider;
|
||||
try
|
||||
{
|
||||
start = DateTime.Now;
|
||||
Console.WriteLine("\u001b[31;1mUpdating database with Entity Framework...\u001b[0m");
|
||||
cicmContext context = services.GetRequiredService<cicmContext>();
|
||||
context.Database.Migrate();
|
||||
end = DateTime.Now;
|
||||
Console.WriteLine("\u001b[31;1mTook \u001b[32;1m{0} seconds\u001b[31;1m...\u001b[0m",
|
||||
(end - start).TotalSeconds);
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Console.WriteLine("\u001b[31;1mCould not open database...\u001b[0m");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("\u001b[31;1mStarting web server...\u001b[0m");
|
||||
host.Run();
|
||||
}
|
||||
|
||||
static IWebHost BuildWebHost(string[] args)
|
||||
|
||||
Reference in New Issue
Block a user