From 83891d666e8f7ccfe95d55e95f077893d8f7e4fd Mon Sep 17 00:00:00 2001 From: Alon Amster Date: Thu, 27 Oct 2016 12:59:58 +0300 Subject: [PATCH] Guard against accidental deletion of external DB --- README.md | 10 ++++++++++ cleaner.js | 15 +++++++++++++++ package.js | 3 +++ 3 files changed, 28 insertions(+) diff --git a/README.md b/README.md index df9411e6..d3171d39 100644 --- a/README.md +++ b/README.md @@ -60,3 +60,13 @@ var cleaner = Package['xolvio:cleaner']; // delete all collections except myCollection with optional callback cleaner.resetDatabase({excludedCollections: ['myCollection']}, callback); ``` + +### When using a database outside of localhost +This package resets the database configured for Meteor. +In order to prevent the accidental deletion of a production database, a special flag was added. + +If your `MONGO_URL` includes hosts other than `localhost` and you actually intend for this external database to be reset by this package, please set the `ALLOW_CLEANING_REMOTE_DATABASE_DURING_TEST` environment variable to `1`. + +```sh +export ALLOW_CLEANING_REMOTE_DATABASE_DURING_TEST=1 +``` diff --git a/cleaner.js b/cleaner.js index 67e1c779..60ebcd66 100644 --- a/cleaner.js +++ b/cleaner.js @@ -7,6 +7,21 @@ if (Meteor.isServer) { ); } + // avoid the unintentional deletions of production databases + if (typeof process.env.MONGO_URL === 'string') { + var mongodbUri = Npm.require('mongodb-uri'); + const hosts = mongodbUri.parse(process.env.MONGO_URL).hosts; + + if (hosts.find(h => h.host !== "localhost") + && process.env.ALLOW_CLEANING_REMOTE_DATABASE_DURING_TEST !== '1') { + throw Error("Since $MONGO_URL contains hosts other than `localhost` " + + "you should set the special environment variable " + + "ALLOW_CLEANING_REMOTE_DATABASE_DURING_TEST to 1 " + + "as a safety measure." + ); + } + } + options = options || {}; var excludedCollections = ['system.indexes']; if (options.excludedCollections) { diff --git a/package.js b/package.js index 31aec19d..22bc3f1a 100644 --- a/package.js +++ b/package.js @@ -6,6 +6,9 @@ Package.describe({ documentation: 'README.md', debugOnly: true, }); +Npm.depends({ + "mongodb-uri": "0.9.7" +}); Package.onUse(function(api) { api.versionsFrom('1.3');