Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new scripts to server #3734

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions server/src/scripts/clearData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env node
const PouchDB = require('pouchdb');
const fetch = require('node-fetch');

const couchUrl = process.env.T_COUCHDB_ENDPOINT

async function clearDatabases(groupsToClear) {
try {
const url = couchUrl + `/_all_dbs`; // Replace with your CouchDB URL

// Get all database names
const response = await fetch(url);
const databaseNames = await response.json();

// Filter databases based on groupsToClear
const filteredDatabases = databaseNames.filter(dbName => groupsToClear.includes(dbName));


for (const dbName of filteredDatabases) {
const deleteUrl = `${couchUrl}/${dbName}/_bulk_docs`;
const docsToDelete = [];

// Get all documents (including design docs)
const allDocsUrl = `${couchUrl}/${dbName}/_all_docs?include_docs=true`;
const allDocsResponse = await fetch(allDocsUrl);
const allDocs = await allDocsResponse.json();

// Filter documents (skip design docs)
for (const row of allDocs.rows) {
if (row.doc && !row.id.startsWith('_design/')) {
docsToDelete.push({ _id: row.doc._id, _rev: row.doc._rev, _deleted: true });
}
}

console.log (`dbName ${dbName}`)
console.log (`docsToDelete ${docsToDelete.length}`)

// Bulk delete if there are documents to delete
if (docsToDelete.length > 0) {
const deleteResponse = await fetch(deleteUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ docs: docsToDelete }),
});

if (!deleteResponse.ok) {
throw new Error(`Error deleting documents in ${dbName}: ${await deleteResponse.text()}`);
}

console.log(`Successfully deleted ${docsToDelete.length} documents from group ${dbName}`);
} else {
console.log(`No documents to delete from group ${dbName}`);
}




}
} catch (error) {
console.error(`Error clearing databases:`, error);
}
}

// Example usage
const groupsToClear = ["group-bdec473f-df02-4e2a-96a0-67d52c4098d7"];
clearDatabases(groupsToClear);
Loading
Loading