├── .gitignore ├── logos └── logo-box-madefor.png ├── CHANGELOG.md ├── package.json ├── LICENSE.md ├── README.md └── bin └── rename-mongodb-database /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules -------------------------------------------------------------------------------- /logos/logo-box-madefor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apostrophecms/rename-mongodb-database/main/logos/logo-box-madefor.png -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## 1.0.1 (2024-04-04) 4 | 5 | Debugged legacy support for MongoDB 3.4.x command line tools without impacting newer tools. 6 | 7 | ## 1.0.0 (2024-04-02) 8 | 9 | Fully functional first release. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@apostrophecms/rename-mongodb-database", 3 | "version": "1.0.1", 4 | "description": "CLI tool to rename mongodb databases with one command", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo 'no tests yet'" 8 | }, 9 | "bin": { 10 | "rename-mongodb-database": "./bin/rename-mongodb-database" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/apostrophecms/rename-mongodb-database.git" 15 | }, 16 | "keywords": [ 17 | "mongodb", 18 | "database", 19 | "mongo" 20 | ], 21 | "author": "Apostrophe Technologies", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/apostrophecms/rename-mongodb-database/issues" 25 | }, 26 | "homepage": "https://github.com/apostrophecms/rename-mongodb-database#readme", 27 | "dependencies": { 28 | "boring": "^1.1.1", 29 | "shell-quote": "^1.8.1" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2024 Apostrophe Technologies, Inc. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rename-mongodb-database 2 | 3 | *A handy little utility from [apostrophecms](https://apostrophecms.com)* 4 | 5 | Rename a mongodb database with one command: 6 | 7 | ```bash 8 | # Talks to mongodb on localhost by default 9 | rename-mongodb-database oldname newname 10 | 11 | # Talk to some other mongodb installation (all MongoDB URIs work) 12 | rename-mongodb-database oldname newname --uri=mongodb://user:pass@somewhere 13 | 14 | # Rename many databases with a matching prefix at once 15 | # renames oldprefix-db1 to newprefix-db1, oldprefix-db2 to newprefix-db2, etc. 16 | # does not rename someotherprefix-db 17 | rename-mongodb-database oldprefix newprefix --many 18 | ``` 19 | 20 | This command will overwrite the new database with the contents of the old, if it already exists, and drop the old database. 21 | 22 | ## Installation 23 | 24 | ``` 25 | npm install -g @apostrophecms/rename-mongodb-database 26 | ``` 27 | 28 | ## Requirements 29 | 30 | **You must have `mongosh` (or `mongo`) `mongodump` and `mongorestore` installed.** Depending on how you installed MongoDB itself, this might not be automatic. 31 | See the MongoDB documentation for more information about the MongoDB command line tools. MongoDB must be at 32 | least version 3.4.x, but keep in mind that 5.0 is the oldest release MongoDB officially supports. 33 | -------------------------------------------------------------------------------- /bin/rename-mongodb-database: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | 'use strict'; 4 | 5 | const quote = require('shell-quote/quote'); 6 | const argv = require('boring')(); 7 | const { execSync } = require('child_process'); 8 | const many = !!argv.many; 9 | 10 | if (argv._.length !== 2) { 11 | if (many) { 12 | fail('Must specify oldprefix and newprefix'); 13 | } else { 14 | fail('Must specify oldname and newname'); 15 | } 16 | } 17 | 18 | const from = argv._[0]; 19 | const to = argv._[1]; 20 | 21 | const uri = argv.uri; 22 | 23 | const shell = getMongoShellName(); 24 | 25 | if (many) { 26 | renameMany(from, to); 27 | } else { 28 | renameOne(from, to); 29 | } 30 | 31 | function renameMany(from, to) { 32 | const dbNames = exec(quote([ shell, '--quiet', '--eval=db.adminCommand( { listDatabases: 1, nameOnly: true }).databases.map(({ name }) => name).join(String.fromCharCode(10))' ])).split('\n').filter(name => name.startsWith(from) && (name.length > 0)); 33 | console.log(dbNames); 34 | dbNames.forEach(name => { 35 | const fromDb = name; 36 | const toDb = name.replace(from, to); 37 | renameOne(fromDb, toDb); 38 | }); 39 | } 40 | 41 | function renameOne(from, to) { 42 | const fromUri = getDbUri(uri, from); 43 | const cleanUri = getCleanUri(uri); 44 | console.log(`Renaming ${from} to ${to}...`); 45 | execInherit(quote([ 'mongodump', `--uri=${fromUri}`, '--archive' ]) + ' | ' + quote([ 'mongorestore', `--uri=${cleanUri}`, `--nsInclude=${from}.*`, `--nsFrom=${from}.*`, `--nsTo=${to}.*`, '--archive', '--drop' ])); 46 | execInherit(quote([ shell, getDbUri(uri, from), '--eval="db.dropDatabase()"' ])); 47 | console.log('... Renamed'); 48 | } 49 | 50 | function exec(cmd) { 51 | if (argv.verbose) { 52 | console.log(`> ${cmd}`); 53 | } 54 | return execSync(cmd, { encoding: 'utf8' }); 55 | } 56 | 57 | function execInherit(cmd) { 58 | if (argv.verbose) { 59 | console.log(`> ${cmd}`); 60 | } 61 | return execSync(cmd, { stdio: 'inherit' }); 62 | } 63 | 64 | function getCleanUri(uri, name) { 65 | const url = new URL(uri || 'mongodb://localhost:27017/'); 66 | url.pathname = '/'; 67 | return url.toString(); 68 | } 69 | 70 | function getDbUri(uri, name) { 71 | const url = new URL(uri || 'mongodb://localhost:27017/'); 72 | url.pathname = `/${name}`; 73 | return url.toString(); 74 | } 75 | 76 | function getMongoShellName() { 77 | try { 78 | exec('mongosh --version'); 79 | return 'mongosh'; 80 | } catch (e) { 81 | try { 82 | exec('mongo --version'); 83 | return 'mongo'; 84 | } catch (e) { 85 | fail('Cannot find mongosh or mongo, install the mongodb command line tools'); 86 | } 87 | } 88 | } 89 | 90 | function fail(s) { 91 | console.error(s); 92 | console.error('\nUsage:\n\nrename-mongodb-database old-name new-name [--many] [--uri=mongodb://user:pass@somewhere.com]'); 93 | process.exit(1); 94 | } 95 | --------------------------------------------------------------------------------