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

Guard against accidental deletion of external DB #18

Open
wants to merge 1 commit into
base: master
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
15 changes: 15 additions & 0 deletions cleaner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down