Skip to content

Commit 267e558

Browse files
authored
Rework the database structure (#112)
* Change Database Provider to PostgreSQL * Rework the check if the database is online * Modify docker-compose to use the correct image * Fix default port * Fix AzuraCast update command not working * Fix update check if instance is online again * Rework DbActions.cs * Adjust .editorconfig * Fix all build errors * Simplify checks * Fix small bug * Fix database stuff not working and formatting * Expand some more stuff * Dead code removal * Fix remaining commands * Fix the last stuff * Whatever * Fix wrong json property type * Fix checks not working The wrong id was selected * Add documentation to prevent errors * Do eager loading when possible * Fix analyzers * Update EFCore * Add indexing and restructure linq queries to improve database performance * Add AsNoTracking()
1 parent d174088 commit 267e558

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+842
-545
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ end_of_line = crlf
1717
[*{_AssemblyInfo.cs,.notsupported.cs,AsmOffsets.cs}]
1818
generated_code = true
1919

20-
[/AzzyBot-Next/Migrations/**]
20+
[src/AzzyBot-Next/Migrations/**]
2121
generated_code = true
2222

2323
# C# files

.vscode/launch.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"console": "externalTerminal",
1919
"stopAtEntry": false,
20+
"enableStepFiltering": false
2021
},
2122
{
2223
"name": "Process attacher",

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"csharp.debug.sourceFileMap":
33
{
4-
"./": "${workspaceFolder}/AzzyBot-Next/"
4+
"./": "${workspaceFolder}/src/AzzyBot-Next/"
55
}
66
}

.vscode/tasks.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"type": "process",
88
"args": [
99
"build",
10-
"${workspaceFolder}/AzzyBot/AzzyBot.csproj"
10+
"${workspaceFolder}/src/AzzyBot/AzzyBot.csproj"
1111
],
1212
"problemMatcher": "$msCompile"
1313
},
@@ -17,7 +17,7 @@
1717
"type": "process",
1818
"args": [
1919
"build",
20-
"${workspaceFolder}/AzzyBot-Next/AzzyBot-Next.csproj"
20+
"${workspaceFolder}/src/AzzyBot-Next/AzzyBot-Next.csproj"
2121
],
2222
"problemMatcher": "$msCompile"
2323
}

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
## 2.0.0-preview3
2+
### Breaking Changes
3+
- The database has switched from MariaDB to PostgreSQL
4+
- You already know what that means (DATA DELETION)
5+
- Please clean your docker system too after you shut down the bot
6+
- docker system prune -a -f --volumes
7+
- docker volume prune -a -f
8+
9+
### General
10+
- Reworked the whole background database structure to make it easier, faster and less ressource-consuming
11+
12+
### Fixes
13+
- `azuracast update-instace` says the station is already up-to-date but it's not
14+
- `admin debug-servers remove-servers` autocomplete not working
15+
116
## 2.0.0-preview2
217
### Breaking Changes
318
- Your Database needs a complete reset, please DROP it and start from scratch

docker-compose.dev.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,19 @@ services:
2020

2121
AzzyBot-Db:
2222
container_name: AzzyBot-Db
23-
image: mariadb:lts
23+
image: postgres:16-alpine
2424
pull_policy: always
2525
restart: unless-stopped
2626
environment:
27-
- MYSQL_ROOT_PASSWORD=thisIsR00!P@ssw0rd
28-
- MYSQL_DATABASE=azzybot
29-
- MYSQL_USER=azzybot
30-
- MYSQL_PASSWORD=thisIsAzzyB0!P@ssw0rd
27+
- POSTGRES_DB=azzybot
28+
- POSTGRES_USER=azzybot
29+
- POSTGRES_PASSWORD=thisIsAzzyB0!P@ssw0rd
3130
- TZ=Europe/Berlin
3231
networks:
3332
- AzzyBot-Db-Nw
3433
hostname: AzzyBot-Db
3534
volumes:
36-
- azzybot_data:/var/lib/mysql
35+
- azzybot_data:/var/lib/postgresql/data
3736

3837
networks:
3938
AzzyBot-Db-Nw:

src/AzzyBot-Next/AzzyBot-Next.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,12 @@
100100
<PackageReference Include="DSharpPlus" Version="5.0.0-nightly-02271" />
101101
<PackageReference Include="DSharpPlus.Commands" Version="5.0.0-nightly-02271" />
102102
<PackageReference Include="DSharpPlus.Interactivity" Version="5.0.0-nightly-02271" />
103-
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.6">
103+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.7">
104104
<PrivateAssets>all</PrivateAssets>
105105
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
106106
</PackageReference>
107107
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
108-
<PackageReference Include="MySqlConnector" Version="2.3.7" />
109-
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
108+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
110109
<PackageReference Include="Roslynator.Analyzers" Version="4.12.4">
111110
<PrivateAssets>all</PrivateAssets>
112111
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

src/AzzyBot-Next/Commands/AdminCommands.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public sealed class AdminDebugServers(DbActions dbActions, DiscordBotService bot
6767
private readonly ILogger<AdminDebugServers> _logger = logger;
6868

6969
[Command("add-server"), Description("Adds the permission to execute debug commands to a server.")]
70-
public async ValueTask AddDebugGuildsAsync(CommandContext context, [Description("Select the server you want to add."), SlashAutoCompleteProvider<GuildsAutocomplete>] string serverId = "")
70+
public async ValueTask AddDebugGuildsAsync(CommandContext context, [Description("Select the server you want to add."), SlashAutoCompleteProvider<GuildsAutocomplete>] string serverId)
7171
{
7272
ArgumentNullException.ThrowIfNull(context, nameof(context));
7373

@@ -108,7 +108,7 @@ public async ValueTask GetDebugGuildsAsync(CommandContext context)
108108

109109
await context.DeferResponseAsync();
110110

111-
List<GuildsEntity> dbGuilds = await _dbActions.GetGuildsWithDebugAsync();
111+
IReadOnlyList<GuildsEntity> dbGuilds = await _dbActions.GetGuildsWithDebugAsync();
112112
if (dbGuilds.Count == 0)
113113
{
114114
await context.EditResponseAsync("No debug servers found.");
@@ -127,7 +127,7 @@ public async ValueTask GetDebugGuildsAsync(CommandContext context)
127127
}
128128

129129
[Command("remove-server"), Description("Removes the permission to execute debug commands from a server.")]
130-
public async ValueTask RemoveDebugGuildsAsync(CommandContext context, [Description("Select the server you want to remove."), SlashAutoCompleteProvider<GuildsAutocomplete>] string serverId = "")
130+
public async ValueTask RemoveDebugGuildsAsync(CommandContext context, [Description("Select the server you want to remove."), SlashAutoCompleteProvider<GuildsAutocomplete>] string serverId)
131131
{
132132
ArgumentNullException.ThrowIfNull(context, nameof(context));
133133

src/AzzyBot-Next/Commands/Autocompletes/AzuraCastMountAutocomplete.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ public async ValueTask<IReadOnlyDictionary<string, object>> AutoCompleteAsync(Au
2020
ArgumentNullException.ThrowIfNull(context, nameof(context));
2121
ArgumentNullException.ThrowIfNull(context.Guild, nameof(context.Guild));
2222

23+
Dictionary<string, object> results = [];
2324
int stationId = Convert.ToInt32(context.Options.Single(o => o.Name is "station" && o.Value is not null).Value, CultureInfo.InvariantCulture);
2425
if (stationId == 0)
25-
return new Dictionary<string, object>();
26+
return results;
2627

2728
// TODO Solve this more clean and nicer when it's possible
28-
Dictionary<string, object> results = [];
29-
List<AzuraCastStationMountEntity> mountsInDb;
29+
IReadOnlyList<AzuraCastStationMountEntity> mountsInDb;
3030
try
3131
{
3232
mountsInDb = await _dbActions.GetAzuraCastStationMountsAsync(context.Guild.Id, stationId);

src/AzzyBot-Next/Commands/Autocompletes/AzuraCastPlaylistAutocomplete.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ public async ValueTask<IReadOnlyDictionary<string, object>> AutoCompleteAsync(Au
2525
ArgumentNullException.ThrowIfNull(context, nameof(context));
2626
ArgumentNullException.ThrowIfNull(context.Guild, nameof(context.Guild));
2727

28+
Dictionary<string, object> results = [];
2829
int stationId = Convert.ToInt32(context.Options.Single(o => o.Name is "station_id" && o.Value is not null).Value, CultureInfo.InvariantCulture);
2930
if (stationId == 0)
30-
return new Dictionary<string, object>();
31+
return results;
3132

32-
Dictionary<string, object> results = [];
3333
string search = context.UserInput;
34-
3534
AzuraCastStationEntity? station;
3635
try
3736
{

src/AzzyBot-Next/Commands/Autocompletes/AzuraCastRequestAutocomplete.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public async ValueTask<IReadOnlyDictionary<string, object>> AutoCompleteAsync(Au
2424
ArgumentNullException.ThrowIfNull(context, nameof(context));
2525
ArgumentNullException.ThrowIfNull(context.Guild, nameof(context.Guild));
2626

27+
Dictionary<string, object> results = [];
2728
int stationId = Convert.ToInt32(context.Options.Single(o => o.Name is "station_id" && o.Value is not null).Value, CultureInfo.InvariantCulture);
2829
if (stationId == 0)
29-
return new Dictionary<string, object>();
30+
return results;
3031

31-
Dictionary<string, object> results = [];
3232
string search = context.UserInput;
3333
AzuraCastStationEntity? station;
3434
try

src/AzzyBot-Next/Commands/Autocompletes/AzuraCastStationsAutocomplete.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
namespace AzzyBot.Commands.Autocompletes;
1515

16-
public sealed class AzuraCastStationsAutocomplete(AzuraCastApiService azuraCast, DbActions dbActions) : IAutoCompleteProvider
16+
public sealed class AzuraCastStationsAutocomplete(AzuraCastApiService azuraCastApi, DbActions dbActions) : IAutoCompleteProvider
1717
{
18-
private readonly AzuraCastApiService _azuraCast = azuraCast;
18+
private readonly AzuraCastApiService _azuraCast = azuraCastApi;
1919
private readonly DbActions _dbActions = dbActions;
2020

2121
public async ValueTask<IReadOnlyDictionary<string, object>> AutoCompleteAsync(AutoCompleteContext context)
@@ -25,7 +25,11 @@ public async ValueTask<IReadOnlyDictionary<string, object>> AutoCompleteAsync(Au
2525

2626
// TODO Solve this more clean and nicer when it's possible
2727
Dictionary<string, object> results = [];
28-
List<AzuraCastStationEntity> stationsInDb;
28+
AzuraCastEntity? azuraCast = await _dbActions.GetAzuraCastAsync(context.Guild.Id);
29+
if (azuraCast is null)
30+
return results;
31+
32+
IReadOnlyList<AzuraCastStationEntity> stationsInDb;
2933
try
3034
{
3135
stationsInDb = await _dbActions.GetAzuraCastStationsAsync(context.Guild.Id);
@@ -37,14 +41,13 @@ public async ValueTask<IReadOnlyDictionary<string, object>> AutoCompleteAsync(Au
3741
return results;
3842
}
3943

40-
Uri baseUrl = new(Crypto.Decrypt(stationsInDb[0].AzuraCast.BaseUrl));
41-
string apiKey = Crypto.Decrypt(stationsInDb[0].AzuraCast.AdminApiKey);
42-
string command = context.Command.Name;
44+
Uri baseUrl = new(Crypto.Decrypt(azuraCast.BaseUrl));
45+
string apiKey = Crypto.Decrypt(azuraCast.AdminApiKey);
4346
foreach (AzuraCastStationEntity station in stationsInDb)
4447
{
4548
AzuraAdminStationConfigRecord config = await _azuraCast.GetStationAdminConfigAsync(baseUrl, apiKey, station.StationId);
4649

47-
switch (command)
50+
switch (context.Command.Name)
4851
{
4952
case "start-station" when config.IsEnabled:
5053
case "stop-station" when !config.IsEnabled:

src/AzzyBot-Next/Commands/Autocompletes/AzzyHelpAutocomplete.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ public sealed class AzzyHelpAutocomplete(AzzyBotSettingsRecord settings, DbActio
2121
public async ValueTask<IReadOnlyDictionary<string, object>> AutoCompleteAsync(AutoCompleteContext context)
2222
{
2323
ArgumentNullException.ThrowIfNull(context, nameof(context));
24+
ArgumentNullException.ThrowIfNull(context.Client.CurrentApplication.Owners, nameof(context.Client.CurrentApplication.Owners));
25+
ArgumentNullException.ThrowIfNull(context.Guild, nameof(context.Guild));
26+
ArgumentNullException.ThrowIfNull(context.Member, nameof(context.Member));
2427

2528
Dictionary<string, object> results = [];
2629
string search = context.UserInput;
2730

28-
IEnumerable<DiscordUser> botOwners = context.Client.CurrentApplication.Owners ?? throw new InvalidOperationException("Invalid bot owners");
29-
ulong guildId = context.Guild?.Id ?? throw new InvalidOperationException("Invalid guild id");
30-
DiscordMember member = context.Member ?? throw new InvalidOperationException("Invalid member");
31+
IEnumerable<DiscordUser> botOwners = context.Client.CurrentApplication.Owners;
32+
ulong guildId = context.Guild.Id;
33+
DiscordMember member = context.Member;
3134
GuildsEntity? guild = await _dbActions.GetGuildAsync(guildId);
3235
if (guild is null)
3336
return results;

src/AzzyBot-Next/Commands/Autocompletes/GuildsAutocomplete.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,15 @@ public async ValueTask<IReadOnlyDictionary<string, object>> AutoCompleteAsync(Au
2121
{
2222
ArgumentNullException.ThrowIfNull(context, nameof(context));
2323

24-
List<GuildsEntity> guildsInDb = [];
24+
IReadOnlyList<GuildsEntity> guildsInDb = [];
2525
IReadOnlyDictionary<ulong, DiscordGuild> guilds = _botService.GetDiscordGuilds;
26-
2726
switch (context.Command.FullName)
2827
{
2928
case "admin debug-servers add-server":
3029
guildsInDb = await _dbActions.GetGuildsWithDebugAsync(false);
3130
break;
3231

33-
case "admin debug-server remove-server":
32+
case "admin debug-servers remove-server":
3433
guildsInDb = await _dbActions.GetGuildsWithDebugAsync(true);
3534
break;
3635
}

0 commit comments

Comments
 (0)