refactor: large codebase cleanup - consolidate schemas, migrate to Drizzle ORM, extract frontend components, modernize Docker builds
Build & Deploy / build-and-push (discord-gateway) (push) Failing after 2m22s
Build & Deploy / build-and-push (backend) (push) Failing after 3m22s
Build & Deploy / build-and-push (proxy) (push) Successful in 1m36s
Build & Deploy / deploy (push) Skipped

- Consolidate all DB schema definitions into packages/shared as single source of truth
- Migrate backend from raw SQL to Drizzle ORM across all modules
- Extract frontend inline UI into separate component files
- Refactor discord-gateway circuitBreaker into conversationState + moderationState
- Convert messageStore to Proxy singleton pattern
- Add validateBody/validateQuery middleware + Zod schemas for API endpoints
- Modernize Docker builds with multi-stage + pnpm deploy
- Migrate CI/CD from deployment to image-based pipeline
- Remove 60+ unused/dead files (~15K lines)
- Update color scheme from sky-blue to teal-cyan
- Move DB connection management to @bete/shared/database

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Developer
2026-07-27 21:54:31 +07:00
co-authored by Claude Opus 4.8
parent 63f21513bd
commit 5802d02e29
223 changed files with 11499 additions and 13350 deletions
@@ -0,0 +1,135 @@
import { createChildLogger, type Logger } from "@bete/shared/logger";
import { and, eq, isNull, or } from "drizzle-orm";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import type * as schema from "../../shared/database/schema.js";
import { retentionPoliciesTable } from "../../shared/database/schema.js";
import type { RetentionPolicy } from "../message-capture/types.js";
// ─── RetentionDb Class ──────────────────────────────────────────────────────
export class RetentionDb {
private logger: Logger;
constructor(
private db: NodePgDatabase<typeof schema>,
_parentLogger?: Logger,
) {
this.logger = createChildLogger("retention-db");
}
/**
* Look up the most specific retention policy for a guild + optional channel.
*
* Resolution order (most-specific-first):
* 1. Channel-level: guild_id + channel_id match
* 2. Guild default: guild_id match with channel_id IS NULL
* 3. No policy found → null
*/
async getRetentionPolicy(
guildId: string,
channelId?: string,
): Promise<RetentionPolicy | null> {
this.logger.debug({ guildId, channelId }, "getRetentionPolicy entry");
try {
const conditions = [eq(retentionPoliciesTable.guild_id, guildId)];
// If a channel is specified, look for a channel-specific policy first
if (channelId) {
const channelRows = await this.db
.select()
.from(retentionPoliciesTable)
.where(
and(
...conditions,
eq(retentionPoliciesTable.channel_id, channelId),
),
);
if (channelRows.length > 0)
return channelRows[0] as RetentionPolicy;
}
// Fall back to the guild default (channel_id IS NULL)
const guildRows = await this.db
.select()
.from(retentionPoliciesTable)
.where(and(...conditions, isNull(retentionPoliciesTable.channel_id)));
return (guildRows[0] as RetentionPolicy) || null;
} catch (error) {
this.logger.error(
{
guildId,
channelId,
error: error instanceof Error ? error.message : String(error),
},
"Failed to get retention policy",
);
throw error;
}
}
async upsertRetentionPolicy(
policy: Omit<RetentionPolicy, "created_at" | "updated_at">,
): Promise<RetentionPolicy> {
this.logger.debug(
{ guildId: policy.guild_id, channelId: policy.channel_id },
"upsertRetentionPolicy entry",
);
try {
const now = Date.now();
// Find existing policy that matches guild + optional channel
const conditions = [eq(retentionPoliciesTable.guild_id, policy.guild_id)];
if (policy.channel_id) {
conditions.push(
eq(retentionPoliciesTable.channel_id, policy.channel_id),
);
} else {
conditions.push(isNull(retentionPoliciesTable.channel_id));
}
const existing = await this.db
.select({ id: retentionPoliciesTable.id })
.from(retentionPoliciesTable)
.where(and(...conditions))
.limit(1);
const existingRow = existing[0];
if (existingRow) {
const rows = (await this.db
.update(retentionPoliciesTable)
.set({
...policy,
updated_at: now,
})
.where(eq(retentionPoliciesTable.id, existingRow.id))
.returning()) as RetentionPolicy[];
return rows[0] as RetentionPolicy;
}
const id = `policy-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
const rows = (await this.db
.insert(retentionPoliciesTable)
.values({
...policy,
id,
created_at: now,
updated_at: now,
})
.returning()) as RetentionPolicy[];
return rows[0] as RetentionPolicy;
} catch (error) {
this.logger.error(
{
guildId: policy.guild_id,
error: error instanceof Error ? error.message : String(error),
},
"Failed to upsert retention policy",
);
throw error;
}
}
}