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

show full error, add connectionOptions in config #57

Open
wants to merge 7 commits 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
39 changes: 22 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ A node based migration framework for ES6+ for mongoose

#### Motivation
migrate-mongoose is a migration framework for projects which are already using mongoose.


#### Update by juji

- add `connectionOptions` in config
- log full error


**Most other migration frameworks:**
- Use a local state file to keep track of which migrations have been run: This is a problem for PaS providers like heroku where the file system is wiped each time you deploy
Expand All @@ -12,13 +17,13 @@ migrate-mongoose is a migration framework for projects which are already using m

**migrate-mongoose:**
- Stores migration state in MongoDB
- Provides plenty of features such as
- Provides plenty of features such as
- Access to mongoose models in migrations
- Use of promises or standard callbacks
- custom config files or env variables for migration options
- ability to delete unused migrations
- Relies on a simple *GLOBAL* state of whether or not each migration has been called
- Relies on a simple *GLOBAL* state of whether or not each migration has been called



### Getting Started with the CLI
Expand Down Expand Up @@ -58,8 +63,8 @@ Commands:
prune Allows you to delete extraneous migrations by
removing extraneous local migration files/database
migrations.


Options:
-d, --dbConnectionUri The URI of the database connection [string] [required]
--collection The mongo collection name to use for migrations [string] [default: "migrations"]
Expand All @@ -70,8 +75,8 @@ Options:
rather than asking interactively (use in scripts)
-h, --help Show help [boolean]



Examples:
node_modules/.bin/migrate list -d mongodb://localhost/migrations
node_modules/.bin/migrate create add_users -d mongodb://localhost/migrations
Expand Down Expand Up @@ -101,7 +106,7 @@ MIGRATE_dbConnectionUri=mongodb://localhost:27017/mydb
```bash
# If you have migrate.json in the directory, you don't need to do anything
migrate list

# Otherwise you can provide a config file
migrate list --config somePath/myCustomConfigFile[.json]
```
Expand All @@ -124,22 +129,22 @@ Here's an example of a migration created using `migrate create some-migration-na
/**
* Easy flow control
*/
// Notice no need for callback
// Notice no need for callback
async function up() {
// Error handling is as easy as throwing an error
if (condition) {
throw new Error('This is an error. Could not complete migration');
}

// You can just run your updates and when function finishes the migration is assumed to be done!
await new Promise((resolve, reject) => {
setTimeout(()=> { resolve('ok'); }, 3000);
});

// ======== OR ===========
// just return the promise! It will succeed when it resolves or fail when rejected
// just return the promise! It will succeed when it resolves or fail when rejected
return lib.getPromise();

}

module.exports = { up, down };
Expand Down Expand Up @@ -185,7 +190,7 @@ import { User } from '../models'
async function up() {
// Then you can use it in the migration like so
await User.create({ firstName: 'Ada', lastName: 'Lovelace' });

// OR do something such as
const users = await User.find();
/* Do something with users */
Expand All @@ -197,7 +202,7 @@ If you're using the package programmatically. You can access your models using t
```javascript
async function up() {
// "this('user')" is the same as calling "connection.model('user')" using the connection you passed to the Migrator constructor.
//
//
await this('user').create({ firstName: 'Ada', lastName: 'Lovelace' });
}
```
Expand All @@ -206,7 +211,7 @@ async function up() {

Currently, the **-d**/**dbConnectionUri** must include the database to use for migrations in the uri.

example: `-d mongodb://localhost:27017/development` .
example: `-d mongodb://localhost:27017/development` .

If you don't want to pass it in every time feel free to use the `migrate.json` config file or an environment variable

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "migrate-mongoose",
"version": "4.0.0",
"name": "juji-migrate-mongoose",
"version": "4.0.2",
"description": "A mongoose based migration framework for node",
"main": "./src/lib.js",
"scripts": {
Expand All @@ -24,7 +24,7 @@
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/balmasi/migrate-mongoose.git"
"url": "https://github.com/juji/migrate-mongoose"
},
"dependencies": {
"bluebird": "^3.3.3",
Expand Down
2 changes: 2 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ let migrator = new Migrator({
migrationsPath: path.resolve(args['migrations-dir']),
templatePath: args['template-file'],
dbConnectionUri: args.dbConnectionUri,
connectionOptions: args.connectionOptions, // juji's addition
collectionName: args.collection,
autosync: args.autosync,
cli: true
Expand Down Expand Up @@ -165,6 +166,7 @@ switch (command) {
promise
.then(() => { process.exit(0); })
.catch((err) => {
console.error(err) // juji's addition
console.warn(err.message.yellow);
process.exit(1);
});
Expand Down
13 changes: 12 additions & 1 deletion src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Migrator {
templatePath,
migrationsPath = './migrations',
dbConnectionUri,
connectionOptions,
collectionName = 'migrations',
autosync = false,
cli = false,
Expand All @@ -44,7 +45,10 @@ class Migrator {
const defaultTemplate = migrationTemplate;
this.template = templatePath ? fs.readFileSync(templatePath, 'utf-8') : defaultTemplate;
this.migrationPath = path.resolve(migrationsPath);
this.connection = connection || mongoose.createConnection(dbConnectionUri, { useNewUrlParser: true });
this.connection = connection || mongoose.createConnection(
dbConnectionUri,
connectionOptions || { useNewUrlParser: true } // juji's addition
);
this.collection = collectionName;
this.autosync = autosync;
this.cli = cli;
Expand Down Expand Up @@ -198,6 +202,11 @@ class Migrator {
}

/**

* sync file system -> database
*
* Imports any migrations that are on the file system but
* missing in the database into the database
* Looks at the file system migrations and imports any migrations that are
* on the file system but missing in the database into the database
*
Expand Down Expand Up @@ -253,6 +262,8 @@ class Migrator {
}

/**
* sync migration database -> file system
*
* Opposite of sync().
* Removes files in migration directory which don't exist in database.
*/
Expand Down