Skip to content

Commit

Permalink
Add delete-old-items script
Browse files Browse the repository at this point in the history
  • Loading branch information
sandervspl committed Jul 18, 2024
1 parent c89ed71 commit 205eef2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
1 change: 1 addition & 0 deletions apps/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"remeda": "^1.61.0"
},
"devDependencies": {
"@types/node": "latest",
"bun-types": "latest",
"drizzle-kit": "^0.21.4"
}
Expand Down
34 changes: 34 additions & 0 deletions apps/server/src/scripts/delete-old-items.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import postgres from 'postgres';

// Configure your PostgreSQL connection
const sql = postgres(process.env.DB_URL!);

async function cleanupDatabase() {
try {
// Delete old records
const deleteResult = await sql`
DELETE FROM items
WHERE timestamp < CURRENT_DATE - INTERVAL '30 days'
`;
console.log(`Deleted ${deleteResult.count} rows`);

// VACUUM FULL ANALYZE
console.log('Starting VACUUM FULL ANALYZE...');
await sql.unsafe('VACUUM FULL ANALYZE items');
console.log('VACUUM FULL ANALYZE completed');

// REINDEX
console.log('Starting REINDEX...');
await sql.unsafe('REINDEX TABLE items');
console.log('REINDEX completed');

console.log('Database cleanup completed successfully');
} catch (err) {
console.error('Error during database cleanup:', err);
} finally {
await sql.end();
}
}

// Run the cleanup function
cleanupDatabase();
19 changes: 14 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 205eef2

Please sign in to comment.