Update DB to version 21: Have full introduction date for machines, not only year.

This commit is contained in:
2018-04-28 21:29:01 +01:00
parent fd0b55dee1
commit 349a941392
13 changed files with 256 additions and 57 deletions

View File

@@ -49,123 +49,123 @@ namespace Cicm.Database
IDbCommand dbCmd = dbCon.CreateCommand();
Console.WriteLine("Creating table `admins`");
dbCmd.CommandText = V20.Admins;
dbCmd.CommandText = V21.Admins;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `browser_tests`");
dbCmd.CommandText = V20.BrowserTests;
dbCmd.CommandText = V21.BrowserTests;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `cicm_db`");
dbCmd.CommandText = V20.CicmDb;
dbCmd.CommandText = V21.CicmDb;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `companies`");
dbCmd.CommandText = V20.Companies;
dbCmd.CommandText = V21.Companies;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `machines`");
dbCmd.CommandText = V20.Machines;
dbCmd.CommandText = V21.Machines;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `forbidden`");
dbCmd.CommandText = V20.Forbidden;
dbCmd.CommandText = V21.Forbidden;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `gpus`");
dbCmd.CommandText = V20.Gpus;
dbCmd.CommandText = V21.Gpus;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `log`");
dbCmd.CommandText = V20.Logs;
dbCmd.CommandText = V21.Logs;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `money_donations`");
dbCmd.CommandText = V20.MoneyDonations;
dbCmd.CommandText = V21.MoneyDonations;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `news`");
dbCmd.CommandText = V20.News;
dbCmd.CommandText = V21.News;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `owned_computers`");
dbCmd.CommandText = V20.OwnedComputers;
dbCmd.CommandText = V21.OwnedComputers;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `owned_consoles`");
dbCmd.CommandText = V20.OwnedConsoles;
dbCmd.CommandText = V21.OwnedConsoles;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `instruction_sets`");
dbCmd.CommandText = V20.InstructionSets;
dbCmd.CommandText = V21.InstructionSets;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `instruction_set_extensions`");
dbCmd.CommandText = V20.InstructionSetExtensions;
dbCmd.CommandText = V21.InstructionSetExtensions;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `processors`");
dbCmd.CommandText = V20.Processors;
dbCmd.CommandText = V21.Processors;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `instruction_set_extensions_by_processor`");
dbCmd.CommandText = V20.InstructionSetExtensionsByProcessor;
dbCmd.CommandText = V21.InstructionSetExtensionsByProcessor;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `sound_synths`");
dbCmd.CommandText = V20.SoundSynths;
dbCmd.CommandText = V21.SoundSynths;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `iso3166_1_numeric`");
dbCmd.CommandText = V20.Iso3166Numeric;
dbCmd.CommandText = V21.Iso3166Numeric;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Filling table `iso3166_1_numeric`");
dbCmd.CommandText = V20.Iso3166NumericValues;
dbCmd.CommandText = V21.Iso3166NumericValues;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating foreign keys for table `companies`");
dbCmd.CommandText = V20.CompaniesForeignKeys;
dbCmd.CommandText = V21.CompaniesForeignKeys;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating foreign keys for table `machines`");
dbCmd.CommandText = V20.MachinesForeignKeys;
dbCmd.CommandText = V21.MachinesForeignKeys;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `company_logos`");
dbCmd.CommandText = V20.CompanyLogos;
dbCmd.CommandText = V21.CompanyLogos;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `company_descriptions`");
dbCmd.CommandText = V20.CompanyDescriptions;
dbCmd.CommandText = V21.CompanyDescriptions;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `processors_by_machine`");
dbCmd.CommandText = V20.ProcessorsByMachine;
dbCmd.CommandText = V21.ProcessorsByMachine;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `gpus_by_machine`");
dbCmd.CommandText = V20.GpusByMachine;
dbCmd.CommandText = V21.GpusByMachine;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `sound_by_machine`");
dbCmd.CommandText = V20.SoundByMachine;
dbCmd.CommandText = V21.SoundByMachine;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `memory_by_machine`");
dbCmd.CommandText = V20.MemoryByMachine;
dbCmd.CommandText = V21.MemoryByMachine;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `resolutions`");
dbCmd.CommandText = V20.Resolutions;
dbCmd.CommandText = V21.Resolutions;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `resolutions_by_gpu`");
dbCmd.CommandText = V20.ResolutionsByGpu;
dbCmd.CommandText = V21.ResolutionsByGpu;
dbCmd.ExecuteNonQuery();
Console.WriteLine("Creating table `storage_by_machine`");
dbCmd.CommandText = V20.StorageByMachine;
dbCmd.CommandText = V21.StorageByMachine;
dbCmd.ExecuteNonQuery();
return true;

View File

@@ -214,7 +214,7 @@ namespace Cicm.Database
dbcmd.Transaction = trans;
const string SQL =
"INSERT INTO machines (company, year, model, type) VALUES (@company, @year, @model, @type)";
"INSERT INTO machines (company, introduced, model, type) VALUES (@company, @introduced, @model, @type)";
dbcmd.CommandText = SQL;
@@ -246,8 +246,9 @@ namespace Cicm.Database
IDbTransaction trans = dbCon.BeginTransaction();
dbcmd.Transaction = trans;
string sql = "UPDATE machines SET company = @company, year = @year, model = @model, type = @type " +
$"WHERE id = {entry.Id}";
string sql =
"UPDATE machines SET company = @company, introduced = @introduced, model = @model, type = @type " +
$"WHERE id = {entry.Id}";
dbcmd.CommandText = sql;
@@ -294,17 +295,17 @@ namespace Cicm.Database
IDbDataParameter param4 = dbcmd.CreateParameter();
param1.ParameterName = "@company";
param2.ParameterName = "@year";
param2.ParameterName = "@introduced";
param3.ParameterName = "@model";
param4.ParameterName = "@type";
param1.DbType = DbType.Int32;
param2.DbType = DbType.Int32;
param2.DbType = DbType.DateTime;
param3.DbType = DbType.String;
param4.DbType = DbType.Int32;
param1.Value = entry.Company;
param2.Value = entry.Year;
param2.Value = entry.Introduced;
param3.Value = entry.Model;
param4.Value = entry.Type;
@@ -326,9 +327,10 @@ namespace Cicm.Database
{
Id = (int)dataRow["id"],
Company = (int)dataRow["company"],
Year = (int)dataRow["year"],
Model = (string)dataRow["model"],
Type = (MachineType)dataRow["type"]
Introduced =
dataRow["introduced"] == DBNull.Value ? DateTime.MinValue : (DateTime)dataRow["introduced"],
Model = (string)dataRow["model"],
Type = (MachineType)dataRow["type"]
};
entries.Add(entry);

View File

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

View File

@@ -169,6 +169,11 @@ namespace Cicm.Database
UpdateDatabaseToV20();
break;
}
case 20:
{
UpdateDatabaseToV21();
break;
}
}
OptimizeDatabase();
@@ -2411,6 +2416,76 @@ namespace Cicm.Database
dbCmd.Dispose();
}
void UpdateDatabaseToV21()
{
Console.WriteLine("Updating database to version 21");
Console.WriteLine("Adding new columns to table `machines`");
IDbCommand dbCmd = dbCon.CreateCommand();
IDbTransaction trans = dbCon.BeginTransaction();
dbCmd.Transaction = trans;
dbCmd.CommandText = "ALTER TABLE `machines` ADD COLUMN `introduced` DATETIME NULL;\n" +
"CREATE INDEX idx_machines_introduced ON machines (introduced);";
dbCmd.ExecuteNonQuery();
trans.Commit();
dbCmd.Dispose();
Console.WriteLine("Getting all items from `machines`");
dbCmd = dbCon.CreateCommand();
IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter();
dbCmd.CommandText = "SELECT * from machines";
DataSet dataSet = new DataSet();
dataAdapter.SelectCommand = dbCmd;
dataAdapter.Fill(dataSet);
foreach(DataRow dataRow in dataSet.Tables[0].Rows)
{
IDbCommand dbcmd = dbCon.CreateCommand();
IDbDataParameter param1 = dbcmd.CreateParameter();
param1.ParameterName = "@introduced";
param1.DbType = DbType.DateTime;
if((int)dataRow["year"] > 0) param1.Value = new DateTime((int)dataRow["year"], 1, 1);
else param1.Value = null;
string sql = $"UPDATE `machines` SET introduced = @introduced WHERE id = {(int)dataRow["id"]}";
dbcmd.Parameters.Add(param1);
trans = dbCon.BeginTransaction();
dbcmd.Transaction = trans;
Console.WriteLine("Adding introduction date {0} to machine {1}", (int)dataRow["year"],
(int)dataRow["id"]);
dbcmd.CommandText = sql;
dbcmd.ExecuteNonQuery();
trans.Commit();
dbcmd.Dispose();
}
Console.WriteLine("Removing year column from table `machines`");
dbCmd = dbCon.CreateCommand();
trans = dbCon.BeginTransaction();
dbCmd.Transaction = trans;
dbCmd.CommandText = "ALTER TABLE `machines` DROP COLUMN `year`;";
dbCmd.ExecuteNonQuery();
trans.Commit();
dbCmd.Dispose();
Console.WriteLine("Setting new database version to 21...");
dbCmd = dbCon.CreateCommand();
dbCmd.CommandText = "INSERT INTO cicm_db (version) VALUES ('21')";
dbCmd.ExecuteNonQuery();
dbCmd.Dispose();
}
void OptimizeDatabase()
{
IDbCommand dbCmd = dbCon.CreateCommand();

View File

@@ -28,6 +28,8 @@
// Copyright © 2003-2018 Natalia Portillo
*******************************************************************************/
using System;
namespace Cicm.Database.Schemas
{
/// <summary>Computer</summary>
@@ -37,11 +39,11 @@ namespace Cicm.Database.Schemas
public int Company;
/// <summary>ID</summary>
public int Id;
/// <summary>Introduction date, null if unknown, 1000 if prototype</summary>
public DateTime Introduced;
/// <summary>Model name</summary>
public string Model;
/// <summary>Machine type</summary>
public MachineType Type;
/// <summary>Introduction date, 0 if unknown, 1000 if prototype</summary>
public int Year;
}
}

View File

@@ -0,0 +1,120 @@
/******************************************************************************
// Canary Islands Computer Museum Website
// ----------------------------------------------------------------------------
//
// Filename : V21.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Contains SQL queries to create the database version 7.
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2018 Natalia Portillo
*******************************************************************************/
namespace Cicm.Database.Schemas.Sql
{
public static class V21
{
public static readonly string Admins = V20.Admins;
public static readonly string BrowserTests = V20.BrowserTests;
public static readonly string CicmDb = "CREATE TABLE `cicm_db` (\n" +
"`id` int(11) NOT NULL AUTO_INCREMENT,\n" +
"`version` int(11) NOT NULL,\n" +
"`updated` datetime DEFAULT CURRENT_TIMESTAMP,\n" +
"PRIMARY KEY (`id`));\n" +
"INSERT INTO cicm_db (version) VALUES ('20');";
public static readonly string Companies = V20.Companies;
public static readonly string Machines = "CREATE TABLE `machines` (;\n" +
"`id` int(11) NOT NULL AUTO_INCREMENT,;\n" +
"`company` int(11) NOT NULL DEFAULT '0',;\n" +
"`introduced` DATETIME NULL,;\n" +
"`model` char(50) NOT NULL DEFAULT '',;\n" +
"`type` int(11) NOT NULL DEFAULT '0',;\n" +
"PRIMARY KEY (`id`),;\n" +
"KEY `idx_machines_company` (`company`),;\n" +
"KEY `idx_machines_introduced` (`introduced`),;\n" +
"KEY `idx_machines_model` (`model`),;\n" +
"KEY `idx_machines_type` (`type`));";
public static readonly string Forbidden = V20.Forbidden;
public static readonly string Gpus = V20.Gpus;
public static readonly string Logs = V20.Logs;
public static readonly string MoneyDonations = V20.MoneyDonations;
public static readonly string News = V20.News;
public static readonly string OwnedComputers = V20.OwnedComputers;
public static readonly string OwnedConsoles = V20.OwnedConsoles;
public static readonly string Processors = V20.Processors;
public static readonly string SoundSynths = V20.SoundSynths;
public static readonly string MachinesForeignKeys =
"ALTER TABLE `machines` ADD FOREIGN KEY `fk_machines_company` (company) REFERENCES `companies` (`id`) ON UPDATE CASCADE;";
public static readonly string Iso3166Numeric = V20.Iso3166Numeric;
public static readonly string Iso3166NumericValues = V20.Iso3166NumericValues;
public static readonly string CompaniesForeignKeys = V20.CompaniesForeignKeys;
public static readonly string CompanyLogos = V20.CompanyLogos;
public static readonly string CompanyDescriptions = V20.CompanyDescriptions;
public static readonly string InstructionSets = V20.InstructionSets;
public static readonly string InstructionSetExtensions = V20.InstructionSetExtensions;
public static readonly string InstructionSetExtensionsByProcessor = V20.InstructionSetExtensionsByProcessor;
public static readonly string ProcessorsByMachine = V20.ProcessorsByMachine;
public static readonly string GpusByMachine = V20.GpusByMachine;
public static readonly string SoundByMachine = V20.SoundByMachine;
public static readonly string MemoryByMachine = V20.MemoryByMachine;
public static readonly string Resolutions = V20.Resolutions;
public static readonly string ResolutionsByGpu = V20.ResolutionsByGpu;
public static readonly string StorageByMachine = "CREATE TABLE `storage_by_machine` (\n" +
"`machine` INT NOT NULL,\n" +
"`type` INT NOT NULL DEFAULT 0,\n" +
"`interface` INT NOT NULL DEFAULT 0,\n" +
"`capacity` BIGINT DEFAULT NULL,\n" +
"KEY `idx_storage_machine` (`machine`),\n" +
"KEY `idx_storage_type` (`type`),\n" +
"KEY `idx_storage_interface` (`interface`),\n" +
"KEY `idx_storage_capacity` (`capacity`),\n" +
"CONSTRAINT `fk_storage_by_machine_machine` FOREIGN KEY (`machine`) REFERENCES `machines` (`id`) ON UPDATE CASCADE ON DELETE CASCADE);";
}
}

View File

@@ -52,8 +52,8 @@ namespace cicm_web.Controllers
ViewBag.ItemCount = computers.Count;
ViewBag.MinYear = computers.Where(t => t.Year > 1000).Min(t => t.Year);
ViewBag.MaxYear = computers.Where(t => t.Year > 1000).Max(t => t.Year);
ViewBag.MinYear = computers.Where(t => t.Introduced.Year > 1000).Min(t => t.Introduced.Year);
ViewBag.MaxYear = computers.Where(t => t.Introduced.Year > 1000).Max(t => t.Introduced.Year);
return View();
}

View File

@@ -52,8 +52,8 @@ namespace cicm_web.Controllers
ViewBag.ItemCount = consoles.Count;
ViewBag.MinYear = consoles.Where(t => t.Year > 1000).Min(t => t.Year);
ViewBag.MaxYear = consoles.Where(t => t.Year > 1000).Max(t => t.Year);
ViewBag.MinYear = consoles.Where(t => t.Introduced.Year > 1000).Min(t => t.Introduced.Year);
ViewBag.MaxYear = consoles.Where(t => t.Introduced.Year > 1000).Max(t => t.Introduced.Year);
return View();
}

View File

@@ -101,7 +101,7 @@ namespace cicm_web.Models
List<MachineMini> items = new List<MachineMini>();
foreach(Cicm.Database.Schemas.Machine dbItem in dbItems)
if(dbItem.Year == year)
if(dbItem.Introduced.Year == year)
items.Add(MachineMini.TransformItem(dbItem));
return items.OrderBy(t => t.Company.Name).ThenBy(t => t.Model).ToArray();

View File

@@ -99,7 +99,7 @@ namespace cicm_web.Models
List<MachineMini> items = new List<MachineMini>();
foreach(Cicm.Database.Schemas.Machine dbItem in dbItems)
if(dbItem.Year == year)
if(dbItem.Introduced.Year == year)
items.Add(MachineMini.TransformItem(dbItem));
return items.OrderBy(t => t.Company.Name).ThenBy(t => t.Model).ToArray();

View File

@@ -40,13 +40,13 @@ namespace cicm_web.Models
public Company Company;
public GpuByMachine[] Gpus;
public int Id;
public DateTime Introduced;
public MemoryByMachine[] Memories;
public string Model;
public ProcessorByMachine[] Processors;
public SoundByMachine[] SoundSynths;
public StorageByMachine[] Storage;
public MachineType Type;
public int Year;
public static Machine[] GetAllItems()
{
@@ -83,7 +83,7 @@ namespace cicm_web.Models
Gpus = GpuByMachine.GetAllItems(dbItem.Id),
Id = dbItem.Id,
Model = dbItem.Model,
Year = dbItem.Year,
Introduced = dbItem.Introduced,
Type = dbItem.Type,
Processors = ProcessorByMachine.GetAllItems(dbItem.Id),
SoundSynths = SoundByMachine.GetAllItems(dbItem.Id),
@@ -134,7 +134,7 @@ namespace cicm_web.Models
List<MachineMini> items = new List<MachineMini>();
foreach(Cicm.Database.Schemas.Machine dbItem in dbItems)
if(dbItem.Year == year)
if(dbItem.Introduced.Year == year)
items.Add(TransformItem(dbItem));
return items.OrderBy(t => t.Company.Name).ThenBy(t => t.Model).ToArray();

View File

@@ -59,7 +59,7 @@
}
</p>
@if(Model.Year == 1000)
@if(Model.Introduced.Year == 1000)
{
<b>
<div style="text-align: center;">PROTOTYPE</div>
@@ -73,20 +73,20 @@
@Model.Company.Name</a> @Model.Model</b>
<table width=100%>
@if(Model.Year != 1000)
@if(Model.Introduced.Year != 1000)
{
<tr>
<th scope=row
width="37%">
<div align=right>
Year
Introduction date
</div>
</th>
<td width="63%">
<a asp-controller="Computer"
asp-action="ByYear"
asp-route-id="@Model.Year">
@Model.Year</a>
asp-route-id="@Model.Introduced.Year">
@Model.Introduced.ToLongDateString()</a>
</td>
</tr>
}

View File

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