feat: migrate from Redis to SQLite for event handling and notifications

This commit is contained in:
Arunavo Ray
2025-05-20 19:09:48 +05:30
parent c179953649
commit 6d13ff29ca
17 changed files with 470 additions and 202 deletions

38
scripts/check-events.ts Normal file
View File

@@ -0,0 +1,38 @@
#!/usr/bin/env bun
/**
* Script to check events in the database
*/
import { Database } from "bun:sqlite";
import path from "path";
import fs from "fs";
// Define the database path
const dataDir = path.join(process.cwd(), "data");
if (!fs.existsSync(dataDir)) {
console.error("Data directory not found:", dataDir);
process.exit(1);
}
const dbPath = path.join(dataDir, "gitea-mirror.db");
if (!fs.existsSync(dbPath)) {
console.error("Database file not found:", dbPath);
process.exit(1);
}
// Open the database
const db = new Database(dbPath);
// Check if the events table exists
const tableExists = db.query("SELECT name FROM sqlite_master WHERE type='table' AND name='events'").get();
if (!tableExists) {
console.error("Events table does not exist");
process.exit(1);
}
// Get all events
const events = db.query("SELECT * FROM events").all();
console.log("Events in the database:");
console.log(JSON.stringify(events, null, 2));

33
scripts/cleanup-redis.ts Normal file
View File

@@ -0,0 +1,33 @@
#!/usr/bin/env bun
/**
* Cleanup script to remove Redis-related files and code
* This script should be run when migrating from Redis to SQLite
*/
import fs from "fs";
import path from "path";
// Files to remove
const filesToRemove = [
"src/lib/redis.ts"
];
// Remove files
console.log("Removing Redis-related files...");
for (const file of filesToRemove) {
const filePath = path.join(process.cwd(), file);
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
console.log(`Removed: ${file}`);
} else {
console.log(`File not found: ${file}`);
}
}
console.log("\nRedis cleanup completed successfully");
console.log("\nReminder: You should also remove Redis from your Docker Compose files and environment variables.");
console.log("The following files have been updated to use SQLite instead of Redis:");
console.log("- src/lib/helpers.ts");
console.log("- src/pages/api/sse/index.ts");
console.log("\nNew files created:");
console.log("- src/lib/events.ts");

View File

@@ -35,6 +35,7 @@ async function ensureTablesExist() {
"repositories",
"organizations",
"mirror_jobs",
"events",
];
for (const table of requiredTables) {
@@ -148,6 +149,24 @@ async function ensureTablesExist() {
)
`);
break;
case "events":
db.exec(`
CREATE TABLE events (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
channel TEXT NOT NULL,
payload TEXT NOT NULL,
read INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL DEFAULT (strftime('%s','now')),
FOREIGN KEY (user_id) REFERENCES users(id)
)
`);
db.exec(`
CREATE INDEX idx_events_user_channel ON events(user_id, channel);
CREATE INDEX idx_events_created_at ON events(created_at);
CREATE INDEX idx_events_read ON events(read);
`);
break;
}
console.log(`✅ Table '${table}' created successfully.`);
}
@@ -362,6 +381,24 @@ async function initializeDatabase() {
)
`);
db.exec(`
CREATE TABLE IF NOT EXISTS events (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
channel TEXT NOT NULL,
payload TEXT NOT NULL,
read INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL DEFAULT (strftime('%s','now')),
FOREIGN KEY (user_id) REFERENCES users(id)
)
`);
db.exec(`
CREATE INDEX IF NOT EXISTS idx_events_user_channel ON events(user_id, channel);
CREATE INDEX IF NOT EXISTS idx_events_created_at ON events(created_at);
CREATE INDEX IF NOT EXISTS idx_events_read ON events(read);
`);
// Insert default config if none exists
const configCountResult = db.query(`SELECT COUNT(*) as count FROM configs`).get();
const configCount = configCountResult?.count || 0;

53
scripts/migrate-db.ts Normal file
View File

@@ -0,0 +1,53 @@
#!/usr/bin/env bun
/**
* Database migration script to add the events table
* This script should be run when upgrading from a version that used Redis
*/
import { Database } from "bun:sqlite";
import fs from "fs";
import path from "path";
// Define the database path
const dataDir = path.join(process.cwd(), "data");
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true });
}
const dbPath = path.join(dataDir, "gitea-mirror.db");
if (!fs.existsSync(dbPath)) {
console.error("Database file not found:", dbPath);
process.exit(1);
}
// Open the database
const db = new Database(dbPath);
// Check if the events table already exists
const tableExists = db.query("SELECT name FROM sqlite_master WHERE type='table' AND name='events'").get();
if (tableExists) {
console.log("Events table already exists, skipping migration");
process.exit(0);
}
// Create the events table
console.log("Creating events table...");
db.exec(`
CREATE TABLE events (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
channel TEXT NOT NULL,
payload TEXT NOT NULL,
read INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL DEFAULT (unixepoch()),
FOREIGN KEY (user_id) REFERENCES users(id)
);
-- Create indexes for efficient querying
CREATE INDEX idx_events_user_channel ON events(user_id, channel);
CREATE INDEX idx_events_created_at ON events(created_at);
CREATE INDEX idx_events_read ON events(read);
`);
console.log("Migration completed successfully");