From 610d97bde39d1cec6acff4e51f3f3437430680e9 Mon Sep 17 00:00:00 2001 From: syntaxbullet Date: Thu, 12 Feb 2026 15:02:05 +0100 Subject: [PATCH] feat(scripts): add config migration script for guild settings Add script to migrate existing config.json values to database with bun run db:migrate-config command. --- package.json | 1 + shared/scripts/migrate-config-to-db.ts | 51 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 shared/scripts/migrate-config-to-db.ts diff --git a/package.json b/package.json index 462ef8b..4d2c458 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "db:push:local": "drizzle-kit push", "dev": "bun --watch bot/index.ts", "db:studio": "drizzle-kit studio --port 4983 --host 0.0.0.0", + "db:migrate-config": "docker compose run --rm app bun shared/scripts/migrate-config-to-db.ts", "remote": "bash shared/scripts/remote.sh", "logs": "bash shared/scripts/logs.sh", "db:backup": "bash shared/scripts/db-backup.sh", diff --git a/shared/scripts/migrate-config-to-db.ts b/shared/scripts/migrate-config-to-db.ts new file mode 100644 index 0000000..7a1d4e8 --- /dev/null +++ b/shared/scripts/migrate-config-to-db.ts @@ -0,0 +1,51 @@ +import { guildSettingsService } from "@shared/modules/guild-settings/guild-settings.service"; +import { config } from "@shared/lib/config"; +import { env } from "@shared/lib/env"; + +async function migrateConfigToDatabase() { + const guildId = env.DISCORD_GUILD_ID; + + if (!guildId) { + console.error("DISCORD_GUILD_ID not set. Cannot migrate config."); + console.log("Set DISCORD_GUILD_ID in your environment to migrate config."); + process.exit(1); + } + + console.log(`Migrating config for guild ${guildId}...`); + + const existing = await guildSettingsService.getSettings(guildId); + if (existing) { + console.log("Guild settings already exist in database:"); + console.log(JSON.stringify(existing, null, 2)); + console.log("\nSkipping migration. Delete existing settings first if you want to re-migrate."); + return; + } + + await guildSettingsService.upsertSettings({ + guildId, + studentRoleId: config.studentRole ?? undefined, + visitorRoleId: config.visitorRole ?? undefined, + colorRoleIds: config.colorRoles ?? [], + welcomeChannelId: config.welcomeChannelId ?? undefined, + welcomeMessage: config.welcomeMessage ?? undefined, + feedbackChannelId: config.feedbackChannelId ?? undefined, + terminalChannelId: config.terminal?.channelId ?? undefined, + terminalMessageId: config.terminal?.messageId ?? undefined, + moderationLogChannelId: config.moderation?.cases?.logChannelId ?? undefined, + moderationDmOnWarn: config.moderation?.cases?.dmOnWarn ?? true, + moderationAutoTimeoutThreshold: config.moderation?.cases?.autoTimeoutThreshold ?? undefined, + }); + + console.log("✅ Migration complete!"); + console.log("\nGuild settings are now stored in the database."); + console.log("You can manage them via:"); + console.log(" - /settings command in Discord"); + console.log(" - API endpoints at /api/guilds/:guildId/settings"); +} + +migrateConfigToDatabase() + .then(() => process.exit(0)) + .catch((error) => { + console.error("Migration failed:", error); + process.exit(1); + });