migrate from SQLite to PostgreSQL with Drizzle ORM
- Updated all packages to latest versions (React 19, Next.js 14.2.32) - Replaced sqlite3 with pg and drizzle-orm dependencies - Created complete PostgreSQL schema with relationships and indexes - Migrated all API endpoints from SQLite to Drizzle queries - Added database seeding with sample data - Updated authentication to use bcrypt instead of pbkdf2 - Configured connection pooling for PostgreSQL - Updated app version to 1.0.0 - All endpoints tested and working correctly
This commit is contained in:
@@ -1,99 +1,60 @@
|
||||
import sqlite3 from 'sqlite3'
|
||||
import path from 'path'
|
||||
import { db, schema } from './db/connection'
|
||||
import { sql, count } from 'drizzle-orm'
|
||||
|
||||
export async function optimizeDatabase(): Promise<void> {
|
||||
const dbPath = path.join(process.cwd(), 'database', 'antihoax.db')
|
||||
const db = new sqlite3.Database(dbPath)
|
||||
|
||||
try {
|
||||
// Create performance indexes
|
||||
const indexes = [
|
||||
'CREATE INDEX IF NOT EXISTS idx_sources_domain ON sources(domain)',
|
||||
'CREATE INDEX IF NOT EXISTS idx_sources_status_risk ON sources(status, risk_level)',
|
||||
'CREATE INDEX IF NOT EXISTS idx_sources_created ON sources(created_at)',
|
||||
'CREATE INDEX IF NOT EXISTS idx_reports_status ON reports(status)',
|
||||
'CREATE INDEX IF NOT EXISTS idx_reports_created ON reports(created_at)',
|
||||
'CREATE INDEX IF NOT EXISTS idx_api_keys_hash ON api_keys(key_hash)',
|
||||
'CREATE INDEX IF NOT EXISTS idx_audit_logs_created ON audit_logs(created_at)',
|
||||
'CREATE INDEX IF NOT EXISTS idx_source_categories ON source_categories(source_id, category_id)'
|
||||
// PostgreSQL automatically creates indexes defined in schema.ts
|
||||
// Run ANALYZE to update statistics for query optimization
|
||||
const tables = [
|
||||
'sources',
|
||||
'reports',
|
||||
'api_keys',
|
||||
'source_categories',
|
||||
'categories',
|
||||
'users'
|
||||
]
|
||||
|
||||
for (const indexQuery of indexes) {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
db.run(indexQuery, (err) => {
|
||||
if (err) reject(err)
|
||||
else resolve()
|
||||
})
|
||||
})
|
||||
for (const tableName of tables) {
|
||||
await db.execute(sql`ANALYZE ${sql.raw(tableName)}`)
|
||||
}
|
||||
|
||||
// Analyze tables for query optimization
|
||||
const analyzeTables = [
|
||||
'ANALYZE sources',
|
||||
'ANALYZE reports',
|
||||
'ANALYZE api_keys',
|
||||
'ANALYZE audit_logs',
|
||||
'ANALYZE source_categories'
|
||||
]
|
||||
|
||||
for (const analyzeQuery of analyzeTables) {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
db.run(analyzeQuery, (err) => {
|
||||
if (err) reject(err)
|
||||
else resolve()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// Vacuum database to optimize storage
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
db.run('VACUUM', (err) => {
|
||||
if (err) reject(err)
|
||||
else resolve()
|
||||
})
|
||||
})
|
||||
// PostgreSQL equivalent of VACUUM - VACUUM ANALYZE updates statistics and cleans up
|
||||
await db.execute(sql`VACUUM ANALYZE`)
|
||||
|
||||
console.log('Database optimization completed successfully')
|
||||
|
||||
} catch (error) {
|
||||
console.error('Database optimization failed:', error)
|
||||
throw error
|
||||
} finally {
|
||||
db.close()
|
||||
}
|
||||
}
|
||||
|
||||
export async function getDatabaseStats(): Promise<any> {
|
||||
const dbPath = path.join(process.cwd(), 'database', 'antihoax.db')
|
||||
const db = new sqlite3.Database(dbPath)
|
||||
|
||||
try {
|
||||
const stats = await new Promise<any>((resolve, reject) => {
|
||||
db.all(`
|
||||
SELECT
|
||||
name as table_name,
|
||||
COUNT(*) as row_count
|
||||
FROM (
|
||||
SELECT 'sources' as name UNION
|
||||
SELECT 'reports' as name UNION
|
||||
SELECT 'api_keys' as name UNION
|
||||
SELECT 'audit_logs' as name
|
||||
) tables
|
||||
`, (err, rows) => {
|
||||
if (err) reject(err)
|
||||
else resolve(rows)
|
||||
})
|
||||
})
|
||||
// Get row counts for each table
|
||||
const [sourcesCount] = await db.select({ count: count() }).from(schema.sources)
|
||||
const [reportsCount] = await db.select({ count: count() }).from(schema.reports)
|
||||
const [apiKeysCount] = await db.select({ count: count() }).from(schema.apiKeys)
|
||||
const [categoriesCount] = await db.select({ count: count() }).from(schema.categories)
|
||||
const [usersCount] = await db.select({ count: count() }).from(schema.users)
|
||||
const [sourceCategoriesCount] = await db.select({ count: count() }).from(schema.sourceCategories)
|
||||
|
||||
const tables = [
|
||||
{ table_name: 'sources', row_count: sourcesCount.count },
|
||||
{ table_name: 'reports', row_count: reportsCount.count },
|
||||
{ table_name: 'api_keys', row_count: apiKeysCount.count },
|
||||
{ table_name: 'categories', row_count: categoriesCount.count },
|
||||
{ table_name: 'users', row_count: usersCount.count },
|
||||
{ table_name: 'source_categories', row_count: sourceCategoriesCount.count }
|
||||
]
|
||||
|
||||
return {
|
||||
tables: stats,
|
||||
tables,
|
||||
optimized_at: new Date().toISOString()
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Failed to get database stats:', error)
|
||||
throw error
|
||||
} finally {
|
||||
db.close()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user