fix: database upgrade and added basic timer

This commit is contained in:
Michael Green
2023-03-11 01:04:03 +11:00
parent d484ee32cb
commit 13dc90883d
11 changed files with 157 additions and 25 deletions

View File

@@ -15,8 +15,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "gaseous-tools", "gaseous-to
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "gaseous-server", "gaseous-server\gaseous-server.csproj", "{A01D2EFF-C82E-473B-84D7-7C25E736F5D2}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "gaseous-server", "gaseous-server\gaseous-server.csproj", "{A01D2EFF-C82E-473B-84D7-7C25E736F5D2}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{B07A4655-A003-416B-A790-ADAA5B548E1A}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU

View File

@@ -12,7 +12,7 @@ using Microsoft.AspNetCore.Mvc;
namespace gaseous_server.Controllers namespace gaseous_server.Controllers
{ {
[ApiController] [ApiController]
[Route("api/[controller]/[action]")] [Route("api/v1/[controller]/[action]")]
public class SignaturesController : ControllerBase public class SignaturesController : ControllerBase
{ {
/// <summary> /// <summary>

View File

@@ -1,6 +1,12 @@
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using gaseous_server;
using gaseous_tools; using gaseous_tools;
// set up db
Database db = new gaseous_tools.Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
db.InitDB();
// set up server
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
@@ -13,6 +19,7 @@ builder.Services.AddControllers().AddJsonOptions(x =>
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.AddHostedService<TimedHostedService>();
var app = builder.Build(); var app = builder.Build();
@@ -29,10 +36,6 @@ app.UseAuthorization();
app.MapControllers(); app.MapControllers();
// set up db
Database db = new gaseous_tools.Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
db.InitDB();
// start the app // start the app
app.Run(); app.Run();

50
gaseous-server/Timer.cs Normal file
View File

@@ -0,0 +1,50 @@
using System;
namespace gaseous_server
{
// see: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-5.0&tabs=visual-studio-mac#timed-background-tasks-1
public class TimedHostedService : IHostedService, IDisposable
{
private int executionCount = 0;
private readonly ILogger<TimedHostedService> _logger;
private Timer _timer;
public TimedHostedService(ILogger<TimedHostedService> logger)
{
_logger = logger;
}
public Task StartAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Timed Hosted Service running.");
_timer = new Timer(DoWork, null, TimeSpan.Zero,
TimeSpan.FromSeconds(5));
return Task.CompletedTask;
}
private void DoWork(object state)
{
var count = Interlocked.Increment(ref executionCount);
_logger.LogInformation(
"Timed Hosted Service is working. Count: {Count}", count);
}
public Task StopAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Timed Hosted Service is stopping.");
_timer?.Change(Timeout.Infinite, 0);
return Task.CompletedTask;
}
public void Dispose()
{
_timer?.Dispose();
}
}
}

View File

@@ -66,6 +66,7 @@ if (Directory.Exists(tosecXML))
tosecXML = Path.GetFullPath(tosecXML); tosecXML = Path.GetFullPath(tosecXML);
string[] tosecPathContents = Directory.GetFiles(tosecXML); string[] tosecPathContents = Directory.GetFiles(tosecXML);
Array.Sort(tosecPathContents);
int lineFileNameLength = 0; int lineFileNameLength = 0;
int lineGameNameLength = 0; int lineGameNameLength = 0;
@@ -172,10 +173,10 @@ if (Directory.Exists(tosecXML))
if (sigDB.Rows.Count == 0) if (sigDB.Rows.Count == 0)
{ {
// entry not present, insert it // entry not present, insert it
sql = "INSERT INTO signatures_platforms (platform) VALUES (@platform); SELECT LAST_INSERT_ID()"; sql = "INSERT INTO signatures_platforms (platform) VALUES (@platform); SELECT CAST(LAST_INSERT_ID() AS SIGNED);";
sigDB = db.ExecuteCMD(sql, dbDict); sigDB = db.ExecuteCMD(sql, dbDict);
gameSystem = (int)sigDB.Rows[0][0]; gameSystem = Convert.ToInt32(sigDB.Rows[0][0]);
} }
else else
{ {
@@ -194,9 +195,9 @@ if (Directory.Exists(tosecXML))
if (sigDB.Rows.Count == 0) if (sigDB.Rows.Count == 0)
{ {
// entry not present, insert it // entry not present, insert it
sql = "INSERT INTO signatures_publishers (publisher) VALUES (@publisher); SELECT LAST_INSERT_ID()"; sql = "INSERT INTO signatures_publishers (publisher) VALUES (@publisher); SELECT CAST(LAST_INSERT_ID() AS SIGNED);";
sigDB = db.ExecuteCMD(sql, dbDict); sigDB = db.ExecuteCMD(sql, dbDict);
gamePublisher = (int)sigDB.Rows[0][0]; gamePublisher = Convert.ToInt32(sigDB.Rows[0][0]);
} }
else else
{ {
@@ -215,10 +216,10 @@ if (Directory.Exists(tosecXML))
// entry not present, insert it // entry not present, insert it
sql = "INSERT INTO signatures_games " + sql = "INSERT INTO signatures_games " +
"(name, description, year, publisherid, demo, systemid, systemvariant, video, country, language, copyright) VALUES " + "(name, description, year, publisherid, demo, systemid, systemvariant, video, country, language, copyright) VALUES " +
"(@name, @description, @year, @publisherid, @demo, @systemid, @systemvariant, @video, @country, @language, @copyright); SELECT LAST_INSERT_ID()"; "(@name, @description, @year, @publisherid, @demo, @systemid, @systemvariant, @video, @country, @language, @copyright); SELECT CAST(LAST_INSERT_ID() AS SIGNED);";
sigDB = db.ExecuteCMD(sql, dbDict); sigDB = db.ExecuteCMD(sql, dbDict);
gameId = (int)sigDB.Rows[0][0]; gameId = Convert.ToInt32(sigDB.Rows[0][0]);
} }
else else
{ {
@@ -264,11 +265,11 @@ if (Directory.Exists(tosecXML))
if (sigDB.Rows.Count == 0) if (sigDB.Rows.Count == 0)
{ {
// entry not present, insert it // entry not present, insert it
sql = "INSERT INTO signatures_roms (gameid, name, size, crc, md5, sha1, developmentstatus, flags, romtype, romtypemedia, medialabel) VALUES (@gameid, @name, @size, @crc, @md5, @sha1, @developmentstatus, @flags, @romtype, @romtypemedia, @medialabel); SELECT LAST_INSERT_ID()"; sql = "INSERT INTO signatures_roms (gameid, name, size, crc, md5, sha1, developmentstatus, flags, romtype, romtypemedia, medialabel) VALUES (@gameid, @name, @size, @crc, @md5, @sha1, @developmentstatus, @flags, @romtype, @romtypemedia, @medialabel); SELECT CAST(LAST_INSERT_ID() AS SIGNED);";
sigDB = db.ExecuteCMD(sql, dbDict); sigDB = db.ExecuteCMD(sql, dbDict);
romId = (int)sigDB.Rows[0][0]; romId = Convert.ToInt32(sigDB.Rows[0][0]);
} }
else else
{ {

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Data;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace gaseous_tools namespace gaseous_tools
@@ -84,10 +85,44 @@ namespace gaseous_tools
File.WriteAllText(ConfigurationFilePath, configRaw); File.WriteAllText(ConfigurationFilePath, configRaw);
} }
private static string ReadSetting(string SettingName, string DefaultValue)
{
Database db = new Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
string sql = "SELECT * FROM settings WHERE setting = @settingname";
Dictionary<string, object> dbDict = new Dictionary<string, object>();
dbDict.Add("settingname", SettingName);
dbDict.Add("value", DefaultValue);
DataTable dbResponse = db.ExecuteCMD(sql, dbDict);
if (dbResponse.Rows.Count == 0)
{
// no value with that name stored - respond with the default value
return DefaultValue;
}
else
{
return (string)dbResponse.Rows[0][0];
}
}
private static void SetSetting(string SettingName, string Value)
{
Database db = new Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
string sql = "REPLACE INTO settings (setting, value) VALUES (@settingname, @value)";
Dictionary<string, object> dbDict = new Dictionary<string, object>();
dbDict.Add("settingname", SettingName);
dbDict.Add("value", Value);
db.ExecuteCMD(sql, dbDict);
}
public class ConfigFile public class ConfigFile
{ {
public Database DatabaseConfiguration = new Database(); public Database DatabaseConfiguration = new Database();
[JsonIgnore]
public Library LibraryConfiguration = new Library();
public class Database public class Database
{ {
public string HostName = "localhost"; public string HostName = "localhost";
@@ -106,6 +141,45 @@ namespace gaseous_tools
} }
} }
} }
public class Library
{
public string LibraryRootDirectory
{
get
{
return ReadSetting("LibraryRootDirectory", Path.Combine(Config.ConfigurationPath, "Data"));
}
set
{
SetSetting("LibraryRootDirectory", value);
}
}
public string LibraryUploadDirectory
{
get
{
return Path.Combine(LibraryRootDirectory, "Upload");
}
}
public string LibraryImportDirectory
{
get
{
return Path.Combine(LibraryRootDirectory, "Import");
}
}
public string LibraryDataDirectory
{
get
{
return Path.Combine(LibraryRootDirectory, "Library");
}
}
}
} }
} }
} }

View File

@@ -91,6 +91,7 @@ namespace gaseous_tools
// apply script // apply script
sql = "SELECT schema_version FROM schema_version;"; sql = "SELECT schema_version FROM schema_version;";
dbDict = new Dictionary<string, object>();
DataTable SchemaVersion = ExecuteCMD(sql, dbDict); DataTable SchemaVersion = ExecuteCMD(sql, dbDict);
if (SchemaVersion.Rows.Count == 0) if (SchemaVersion.Rows.Count == 0)
{ {
@@ -106,6 +107,7 @@ namespace gaseous_tools
ExecuteCMD(dbScript, dbDict); ExecuteCMD(dbScript, dbDict);
sql = "UPDATE schema_version SET schema_version=@schemaver"; sql = "UPDATE schema_version SET schema_version=@schemaver";
dbDict = new Dictionary<string, object>();
dbDict.Add("schemaver", i); dbDict.Add("schemaver", i);
ExecuteCMD(sql, dbDict); ExecuteCMD(sql, dbDict);
} }

View File

@@ -42,7 +42,7 @@ CREATE TABLE `signatures_games` (
KEY `ingest_idx` (`name`,`year`,`publisherid`,`systemid`,`country`,`language`) USING BTREE, KEY `ingest_idx` (`name`,`year`,`publisherid`,`systemid`,`country`,`language`) USING BTREE,
CONSTRAINT `publisher` FOREIGN KEY (`publisherid`) REFERENCES `signatures_publishers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `publisher` FOREIGN KEY (`publisherid`) REFERENCES `signatures_publishers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `system` FOREIGN KEY (`systemid`) REFERENCES `signatures_platforms` (`id`) ON DELETE CASCADE ON UPDATE CASCADE CONSTRAINT `system` FOREIGN KEY (`systemid`) REFERENCES `signatures_platforms` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1466355 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */; /*!40101 SET character_set_client = @saved_cs_client */;
-- --
@@ -58,7 +58,7 @@ CREATE TABLE `signatures_platforms` (
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
UNIQUE KEY `idsignatures_platforms_UNIQUE` (`id`), UNIQUE KEY `idsignatures_platforms_UNIQUE` (`id`),
KEY `platforms_idx` (`platform`,`id`) USING BTREE KEY `platforms_idx` (`platform`,`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1231 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */; /*!40101 SET character_set_client = @saved_cs_client */;
-- --
@@ -74,7 +74,7 @@ CREATE TABLE `signatures_publishers` (
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`), UNIQUE KEY `id_UNIQUE` (`id`),
KEY `publisher_idx` (`publisher`,`id`) KEY `publisher_idx` (`publisher`,`id`)
) ENGINE=InnoDB AUTO_INCREMENT=97693 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */; /*!40101 SET character_set_client = @saved_cs_client */;
-- --
@@ -104,7 +104,7 @@ CREATE TABLE `signatures_roms` (
KEY `sha1_idx` (`sha1`) USING BTREE, KEY `sha1_idx` (`sha1`) USING BTREE,
KEY `flags_idx` ((cast(`flags` as char(255) array))), KEY `flags_idx` ((cast(`flags` as char(255) array))),
CONSTRAINT `gameid` FOREIGN KEY (`gameid`) REFERENCES `signatures_games` (`id`) ON DELETE CASCADE ON UPDATE CASCADE CONSTRAINT `gameid` FOREIGN KEY (`gameid`) REFERENCES `signatures_games` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3350963 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */; /*!40101 SET character_set_client = @saved_cs_client */;
-- --
@@ -131,7 +131,7 @@ CREATE TABLE `signatures_sources` (
UNIQUE KEY `id_UNIQUE` (`id`), UNIQUE KEY `id_UNIQUE` (`id`),
KEY `sourcemd5_idx` (`sourcemd5`,`id`) USING BTREE, KEY `sourcemd5_idx` (`sourcemd5`,`id`) USING BTREE,
KEY `sourcesha1_idx` (`sourcesha1`,`id`) USING BTREE KEY `sourcesha1_idx` (`sourcesha1`,`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=7573 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */; /*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

View File

@@ -1,8 +1,4 @@
CREATE CREATE VIEW `view_signatures_games` AS
ALGORITHM = UNDEFINED
DEFINER = `root`@`localhost`
SQL SECURITY DEFINER
VIEW `view_signatures_games` AS
SELECT SELECT
`signatures_games`.`id` AS `id`, `signatures_games`.`id` AS `id`,
`signatures_games`.`name` AS `name`, `signatures_games`.`name` AS `name`,

View File

@@ -0,0 +1,6 @@
CREATE TABLE `gaseous`.`settings` (
`setting` VARCHAR(45) NOT NULL,
`value` LONGTEXT NULL,
UNIQUE INDEX `setting_UNIQUE` (`setting` ASC) VISIBLE,
PRIMARY KEY (`setting`));

View File

@@ -16,6 +16,7 @@
<None Remove="Database\MySQL\" /> <None Remove="Database\MySQL\" />
<None Remove="Database\MySQL\gaseous-1000.sql" /> <None Remove="Database\MySQL\gaseous-1000.sql" />
<None Remove="Database\MySQL\gaseous-1001.sql" /> <None Remove="Database\MySQL\gaseous-1001.sql" />
<None Remove="Database\MySQL\gaseous-1002.sql" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Database\" /> <Folder Include="Database\" />
@@ -24,5 +25,6 @@
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="Database\MySQL\gaseous-1000.sql" /> <EmbeddedResource Include="Database\MySQL\gaseous-1000.sql" />
<EmbeddedResource Include="Database\MySQL\gaseous-1001.sql" /> <EmbeddedResource Include="Database\MySQL\gaseous-1001.sql" />
<EmbeddedResource Include="Database\MySQL\gaseous-1002.sql" />
</ItemGroup> </ItemGroup>
</Project> </Project>