fix migrate scripts, added initial migration

This commit is contained in:
Toastie 2025-01-24 18:43:53 +13:00
parent 3e96405954
commit 6d70f28db7
Signed by: toastie_t0ast
GPG key ID: 0861BE54AD481DC7
11 changed files with 6869 additions and 6712 deletions

View file

@ -13,6 +13,11 @@ public class GuildConfig : DbEntity
public string AutoAssignRoleIds { get; set; }
//todo FUTURE: DELETE, UNUSED
public bool ExclusiveSelfAssignedRoles { get; set; }
public bool AutoDeleteSelfAssignedRoleMessages { get; set; }
//stream notifications
public HashSet<FollowedStream> FollowedStreams { get; set; } = new();

View file

@ -0,0 +1,133 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Migrations;
namespace EllieBot.Migrations;
public static class MigrationQueries
{
public static void MergeAwardedXp(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql("""
UPDATE UserXpStats
SET Xp = AwardedXp + Xp,
AwardedXp = 0
WHERE AwardedXp > 0;
""");
}
public static void MigrateSar(MigrationBuilder migrationBuilder)
{
if (migrationBuilder.IsNpgsql())
return;
migrationBuilder.Sql("""
INSERT INTO GroupName (Number, GuildConfigId)
SELECT DISTINCT "Group", GC.Id
FROM SelfAssignableRoles as SAR
INNER JOIN GuildConfigs as GC
ON SAR.GuildId = GC.GuildId
WHERE SAR.GuildId not in (SELECT GuildConfigs.GuildId from GroupName LEFT JOIN GuildConfigs ON GroupName.GuildConfigId = GuildConfigs.Id);
INSERT INTO SarGroup (Id, GroupNumber, Name, IsExclusive, GuildId)
SELECT GN.Id, GN.Number, GN.Name, GC.ExclusiveSelfAssignedRoles, GC.GuildId
FROM GroupName as GN
INNER JOIN GuildConfigs as GC ON GN.GuildConfigId = GC.Id;
INSERT INTO Sar (GuildId, RoleId, SarGroupId, LevelReq)
SELECT SAR.GuildId, SAR.RoleId, (SELECT Id FROM SarGroup WHERE SG.Number = SarGroup.GroupNumber AND SG.GuildId = SarGroup.GuildId), MIN(SAR.LevelRequirement)
FROM SelfAssignableRoles as SAR
INNER JOIN (SELECT GuildId, gn.Number FROM GroupName as gn
INNER JOIN GuildConfigs as gc ON gn.GuildConfigId =gc.Id
) as SG
ON SG.GuildId = SAR.GuildId
WHERE SG.Number IN (SELECT GroupNumber FROM SarGroup WHERE Sar.GuildId = SarGroup.GuildId)
GROUP BY SAR.GuildId, SAR.RoleId;
INSERT INTO SarAutoDelete (GuildId, IsEnabled)
SELECT GuildId, AutoDeleteSelfAssignedRoleMessages FROM GuildConfigs WHERE AutoDeleteSelfAssignedRoleMessages = TRUE;
""");
}
public static void UpdateUsernames(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql("UPDATE DiscordUser SET Username = '??' || Username WHERE Discriminator = '????';");
}
public static void MigrateRero(MigrationBuilder migrationBuilder)
{
if (migrationBuilder.IsSqlite())
{
migrationBuilder.Sql(
@"insert or ignore into reactionroles(guildid, channelid, messageid, emote, roleid, 'group', levelreq, dateadded)
select guildid, channelid, messageid, emotename, roleid, exclusive, 0, reactionrolemessage.dateadded
from reactionrole
left join reactionrolemessage on reactionrolemessage.id = reactionrole.reactionrolemessageid
left join guildconfigs on reactionrolemessage.guildconfigid = guildconfigs.id;");
}
else if (migrationBuilder.IsNpgsql())
{
migrationBuilder.Sql(
@"insert into reactionroles(guildid, channelid, messageid, emote, roleid, ""group"", levelreq, dateadded)
select guildid, channelid, messageid, emotename, roleid, exclusive::int, 0, reactionrolemessage.dateadded
from reactionrole
left join reactionrolemessage on reactionrolemessage.id = reactionrole.reactionrolemessageid
left join guildconfigs on reactionrolemessage.guildconfigid = guildconfigs.id
ON CONFLICT DO NOTHING;");
}
else
{
throw new NotSupportedException("This database provider doesn't have an implementation for MigrateRero");
}
}
public static void GuildConfigCleanup(MigrationBuilder builder)
{
builder.Sql($"""
DELETE FROM "DelMsgOnCmdChannel" WHERE "GuildConfigId" is NULL;
DELETE FROM "WarningPunishment" WHERE "GuildConfigId" NOT IN (SELECT "Id" from "GuildConfigs");
DELETE FROM "StreamRoleBlacklistedUser" WHERE "StreamRoleSettingsId" is NULL;
DELETE FROM "Permissions" WHERE "GuildConfigId" NOT IN (SELECT "Id" from "GuildConfigs");
""");
}
public static void GreetSettingsCopy(MigrationBuilder builder)
{
builder.Sql("""
INSERT INTO GreetSettings (GuildId, GreetType, MessageText, IsEnabled, ChannelId, AutoDeleteTimer)
SELECT GuildId, 0, ChannelGreetMessageText, SendChannelGreetMessage, GreetMessageChannelId, AutoDeleteGreetMessagesTimer
FROM GuildConfigs
WHERE SendChannelGreetMessage = TRUE;
INSERT INTO GreetSettings (GuildId, GreetType, MessageText, IsEnabled, ChannelId, AutoDeleteTimer)
SELECT GuildId, 1, DmGreetMessageText, SendDmGreetMessage, GreetMessageChannelId, 0
FROM GuildConfigs
WHERE SendDmGreetMessage = TRUE;
INSERT INTO GreetSettings (GuildId, GreetType, MessageText, IsEnabled, ChannelId, AutoDeleteTimer)
SELECT GuildId, 2, ChannelByeMessageText, SendChannelByeMessage, ByeMessageChannelId, AutoDeleteByeMessagesTimer
FROM GuildConfigs
WHERE SendChannelByeMessage = TRUE;
INSERT INTO GreetSettings (GuildId, GreetType, MessageText, IsEnabled, ChannelId, AutoDeleteTimer)
SELECT GuildId, 3, BoostMessage, SendBoostMessage, BoostMessageChannelId, BoostMessageDeleteAfter
FROM GuildConfigs
WHERE SendBoostMessage = TRUE;
""");
}
public static void AddGuildIdsToWarningPunishment(MigrationBuilder builder)
{
builder.Sql("""
DELETE FROM WarningPunishment WHERE GuildConfigId IS NULL OR GuildConfigId NOT IN (SELECT Id FROM GuildConfigs);
UPDATE WarningPunishment
SET GuildId = (SELECT GuildId FROM GuildConfigs WHERE Id = GuildConfigId);
DELETE FROM WarningPunishment as wp
WHERE (wp.Count, wp.GuildConfigId) in (
SELECT wp2.Count, wp2.GuildConfigId FROM WarningPunishment as wp2
GROUP BY wp2.Count, wp2.GuildConfigId
HAVING COUNT(id) > 1
);
""");
}
}

View file

@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace EllieBot.Migrations.PostgreSql
{
[DbContext(typeof(PostgreSqlContext))]
[Migration("20250123191330_init")]
[Migration("20250124023345_init")]
partial class init
{
/// <inheritdoc />
@ -1319,6 +1319,10 @@ namespace EllieBot.Migrations.PostgreSql
.HasColumnType("text")
.HasColumnName("autoassignroleids");
b.Property<bool>("AutoDeleteSelfAssignedRoleMessages")
.HasColumnType("boolean")
.HasColumnName("autodeleteselfassignedrolemessages");
b.Property<bool>("CleverbotEnabled")
.HasColumnType("boolean")
.HasColumnName("cleverbotenabled");
@ -1339,6 +1343,10 @@ namespace EllieBot.Migrations.PostgreSql
.HasColumnType("boolean")
.HasColumnName("disableglobalexpressions");
b.Property<bool>("ExclusiveSelfAssignedRoles")
.HasColumnType("boolean")
.HasColumnName("exclusiveselfassignedroles");
b.Property<bool>("FilterInvites")
.HasColumnType("boolean")
.HasColumnName("filterinvites");
@ -1383,10 +1391,6 @@ namespace EllieBot.Migrations.PostgreSql
.HasColumnType("boolean")
.HasColumnName("stickyroles");
b.Property<string>("TestMigration")
.HasColumnType("text")
.HasColumnName("testmigration");
b.Property<string>("TimeZoneId")
.HasColumnType("text")
.HasColumnName("timezoneid");

View file

@ -297,7 +297,8 @@ namespace EllieBot.Migrations.PostgreSql
prefix = table.Column<string>(type: "text", nullable: true),
deletemessageoncommand = table.Column<bool>(type: "boolean", nullable: false),
autoassignroleids = table.Column<string>(type: "text", nullable: true),
testmigration = table.Column<string>(type: "text", nullable: true),
exclusiveselfassignedroles = table.Column<bool>(type: "boolean", nullable: false),
autodeleteselfassignedrolemessages = table.Column<bool>(type: "boolean", nullable: false),
verbosepermissions = table.Column<bool>(type: "boolean", nullable: false),
permissionrole = table.Column<string>(type: "text", nullable: true),
filterinvites = table.Column<bool>(type: "boolean", nullable: false),

View file

@ -11,7 +11,7 @@ using EllieBot.Db;
namespace EllieBot.Migrations.Sqlite
{
[DbContext(typeof(SqliteContext))]
[Migration("20250123191300_init")]
[Migration("20250124023317_init")]
partial class init
{
/// <inheritdoc />
@ -983,6 +983,9 @@ namespace EllieBot.Migrations.Sqlite
b.Property<string>("AutoAssignRoleIds")
.HasColumnType("TEXT");
b.Property<bool>("AutoDeleteSelfAssignedRoleMessages")
.HasColumnType("INTEGER");
b.Property<bool>("CleverbotEnabled")
.HasColumnType("INTEGER");
@ -998,6 +1001,9 @@ namespace EllieBot.Migrations.Sqlite
b.Property<bool>("DisableGlobalExpressions")
.HasColumnType("INTEGER");
b.Property<bool>("ExclusiveSelfAssignedRoles")
.HasColumnType("INTEGER");
b.Property<bool>("FilterInvites")
.HasColumnType("INTEGER");
@ -1031,9 +1037,6 @@ namespace EllieBot.Migrations.Sqlite
b.Property<bool>("StickyRoles")
.HasColumnType("INTEGER");
b.Property<string>("TestMigration")
.HasColumnType("TEXT");
b.Property<string>("TimeZoneId")
.HasColumnType("TEXT");

View file

@ -296,7 +296,8 @@ namespace EllieBot.Migrations.Sqlite
Prefix = table.Column<string>(type: "TEXT", nullable: true),
DeleteMessageOnCommand = table.Column<bool>(type: "INTEGER", nullable: false),
AutoAssignRoleIds = table.Column<string>(type: "TEXT", nullable: true),
TestMigration = table.Column<string>(type: "TEXT", nullable: true),
ExclusiveSelfAssignedRoles = table.Column<bool>(type: "INTEGER", nullable: false),
AutoDeleteSelfAssignedRoleMessages = table.Column<bool>(type: "INTEGER", nullable: false),
VerbosePermissions = table.Column<bool>(type: "INTEGER", nullable: false),
PermissionRole = table.Column<string>(type: "TEXT", nullable: true),
FilterInvites = table.Column<bool>(type: "INTEGER", nullable: false),

File diff suppressed because it is too large Load diff

View file

@ -1,4 +1,3 @@
#nullable disable
using Microsoft.Extensions.Caching.Memory;
using EllieBot.Modules.Searches.Common;
using EllieBot.Modules.Searches.Services;

View file

@ -49,6 +49,8 @@ Get-ChildItem "Migrations/Postgresql" -File | Where-Object { $_.Name -like '*_*.
Remove-Item $_.FullName -ErrorAction SilentlyContinue
}
dotnet build
# Step 4: Create new initial migrations
Write-Output "Creating new initial migration..."
dotnet ef migrations add $MigrationName --context SqliteContext --output-dir "Migrations/Sqlite" --no-build

View file

@ -78,6 +78,8 @@ for file in "Migrations/Postgresql"/*; do
done
# Step 4: Adding new initial migration
dotnet build
echo "Creating new initial migration..."
dotnet ef migrations add "${MIGRATION_NAME}" --context SqliteContext --output-dir "Migrations/Sqlite" --no-build
dotnet ef migrations add "${MIGRATION_NAME}" --context PostgresqlContext --output-dir "Migrations/PostgreSql" --no-build