Update DB to version 23: Make EntityFramework happy with existing database schema.

This commit is contained in:
2018-08-05 22:39:55 +01:00
parent 9418e3b776
commit b720cd6122
9 changed files with 89 additions and 8 deletions

View File

@@ -3,6 +3,9 @@
<TargetFramework>netcoreapp2.0</TargetFramework> <TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="MySql.Data" Version="6.10.6" /> <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.1" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -438,7 +438,7 @@ namespace Cicm.Database
? DateTime.MinValue ? DateTime.MinValue
: Convert.ToDateTime(dataRow["sold"].ToString()), : Convert.ToDateTime(dataRow["sold"].ToString()),
SoldTo = dataRow["sold_to"] == DBNull.Value ? null : GetCompany((int)dataRow["sold_to"]), SoldTo = dataRow["sold_to"] == DBNull.Value ? null : GetCompany((int)dataRow["sold_to"]),
Country = dataRow["country"] == DBNull.Value ? null : GetIso3166((ushort)dataRow["country"]) Country = dataRow["country"] == DBNull.Value ? null : GetIso3166((short)dataRow["country"])
}; };
if(GetCompanyLogosByCompany(out List<CompanyLogo> logos, entry.Id)) if(GetCompanyLogosByCompany(out List<CompanyLogo> logos, entry.Id))

View File

@@ -115,7 +115,7 @@ namespace Cicm.Database
/// </summary> /// </summary>
/// <param name="id">Id</param> /// <param name="id">Id</param>
/// <returns>ISO 3166-1 code with specified id, <c>null</c> if not found or error</returns> /// <returns>ISO 3166-1 code with specified id, <c>null</c> if not found or error</returns>
public Iso3166 GetIso3166(ushort id) public Iso3166 GetIso3166(short id)
{ {
#if DEBUG #if DEBUG
Console.WriteLine("Getting ISO 3166-1 code with id {0}...", id); Console.WriteLine("Getting ISO 3166-1 code with id {0}...", id);
@@ -270,7 +270,7 @@ namespace Cicm.Database
foreach(DataRow dataRow in dataTable.Rows) foreach(DataRow dataRow in dataTable.Rows)
{ {
Iso3166 entry = new Iso3166 {Id = (ushort)dataRow["id"], Name = (string)dataRow["name"]}; Iso3166 entry = new Iso3166 {Id = (short)dataRow["id"], Name = (string)dataRow["name"]};
entries.Add(entry); entries.Add(entry);
} }

View File

@@ -35,7 +35,7 @@ namespace Cicm.Database
public partial class Operations public partial class Operations
{ {
/// <summary>Last known database version</summary> /// <summary>Last known database version</summary>
const int DB_VERSION = 22; const int DB_VERSION = 23;
/// <summary>The column with this value indicates there is no item of this type.</summary> /// <summary>The column with this value indicates there is no item of this type.</summary>
public const int DB_NONE = -1; public const int DB_NONE = -1;
/// <summary> /// <summary>

View File

@@ -179,6 +179,11 @@ namespace Cicm.Database
UpdateDatabaseToV22(); UpdateDatabaseToV22();
break; break;
} }
case 22:
{
UpdateDatabaseToV23();
break;
}
} }
OptimizeDatabase(); OptimizeDatabase();
@@ -2544,6 +2549,78 @@ namespace Cicm.Database
dbCmd.Dispose(); dbCmd.Dispose();
} }
void UpdateDatabaseToV23()
{
Console.WriteLine("Updating database to version 23");
Console.WriteLine("Altering `browser_tests` primary key");
IDbCommand dbCmd = dbCon.CreateCommand();
IDbTransaction trans = dbCon.BeginTransaction();
dbCmd.Transaction = trans;
dbCmd.CommandText = "ALTER TABLE browser_tests MODIFY id int NOT NULL auto_increment;";
dbCmd.ExecuteNonQuery();
trans.Commit();
dbCmd.Dispose();
Console.WriteLine("Dropping foreign key between `iso3166_1_numeric` and `companies`");
dbCmd = dbCon.CreateCommand();
trans = dbCon.BeginTransaction();
dbCmd.Transaction = trans;
dbCmd.CommandText = "ALTER TABLE companies DROP FOREIGN KEY `fk_companies_country`;";
dbCmd.ExecuteNonQuery();
trans.Commit();
dbCmd.Dispose();
Console.WriteLine("Altering `iso3166_1_numeric` primary key");
dbCmd = dbCon.CreateCommand();
trans = dbCon.BeginTransaction();
dbCmd.Transaction = trans;
dbCmd.CommandText = "ALTER TABLE iso3166_1_numeric MODIFY id smallint(3) NOT NULL;";
dbCmd.ExecuteNonQuery();
trans.Commit();
dbCmd.Dispose();
Console.WriteLine("Altering `country` column in `companies`");
dbCmd = dbCon.CreateCommand();
trans = dbCon.BeginTransaction();
dbCmd.Transaction = trans;
dbCmd.CommandText =
"ALTER TABLE companies MODIFY country smallint(3);";
dbCmd.ExecuteNonQuery();
trans.Commit();
dbCmd.Dispose();
Console.WriteLine("Re-adding new foreign keys to table `companies`");
dbCmd = dbCon.CreateCommand();
trans = dbCon.BeginTransaction();
dbCmd.Transaction = trans;
dbCmd.CommandText =
"ALTER TABLE `companies` ADD FOREIGN KEY `fk_companies_country` (country) REFERENCES `iso3166_1_numeric` (`id`);";
dbCmd.ExecuteNonQuery();
trans.Commit();
dbCmd.Dispose();
Console.WriteLine("Adding primary keys to several tables");
dbCmd = dbCon.CreateCommand();
trans = dbCon.BeginTransaction();
dbCmd.Transaction = trans;
dbCmd.CommandText =
"ALTER TABLE gpus_by_machine ADD id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT;\n" +
"ALTER TABLE memory_by_machine ADD id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT;\n" +
"ALTER TABLE processors_by_machine ADD id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT;\n" +
"ALTER TABLE resolutions_by_gpu ADD id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT;\n" +
"ALTER TABLE sound_by_machine ADD id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT;\n" +
"ALTER TABLE storage_by_machine ADD id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT;";
dbCmd.ExecuteNonQuery();
trans.Commit();
dbCmd.Dispose();
Console.WriteLine("Setting new database version to 23...");
dbCmd = dbCon.CreateCommand();
dbCmd.CommandText = "INSERT INTO cicm_db (version) VALUES ('23')";
dbCmd.ExecuteNonQuery();
dbCmd.Dispose();
}
void OptimizeDatabase() void OptimizeDatabase()
{ {
IDbCommand dbCmd = dbCon.CreateCommand(); IDbCommand dbCmd = dbCon.CreateCommand();

View File

@@ -34,7 +34,7 @@ namespace Cicm.Database.Schemas
public class Iso3166 public class Iso3166
{ {
/// <summary>ISO assigned ID</summary> /// <summary>ISO assigned ID</summary>
public ushort Id; public short Id;
/// <summary>English name</summary> /// <summary>English name</summary>
public string Name; public string Name;
} }

View File

@@ -68,7 +68,7 @@ namespace cicm_web.Controllers
return View(company); return View(company);
} }
public IActionResult ByCountry(ushort id) public IActionResult ByCountry(short id)
{ {
Iso3166 iso3166 = Program.Database.Operations.GetIso3166(id); Iso3166 iso3166 = Program.Database.Operations.GetIso3166(id);

View File

@@ -67,6 +67,7 @@ namespace cicm_web.Models
public static Processor GetItem(int id) public static Processor GetItem(int id)
{ {
Cicm.Database.Schemas.Processor dbItem = Program.Database?.Operations.GetProcessor(id); Cicm.Database.Schemas.Processor dbItem = Program.Database?.Operations.GetProcessor(id);
return dbItem == null return dbItem == null
? null ? null
: new Processor : new Processor

View File

@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework> <TargetFramework>netcoreapp2.0</TargetFramework>
<Version>3.0.99.232</Version> <Version>3.0.99.271</Version>
<Company>Canary Islands Computer Museum</Company> <Company>Canary Islands Computer Museum</Company>
<Copyright>Copyright © 2003-2018 Natalia Portillo</Copyright> <Copyright>Copyright © 2003-2018 Natalia Portillo</Copyright>
<Product>Canary Islands Computer Museum Website</Product> <Product>Canary Islands Computer Museum Website</Product>