Comments (10)
I'm a little late to this party but I fought with the same issue. I had to do this:
import { schema } from 'mongoose';
import nameSchema from "../models/name";
require (nameSchema);
from migrate-mongoose.
@NetOperatorWibby is there a chance, that you share your code as an example for others?
from migrate-mongoose.
Absolutely @nexorianus! I was quite busy when I posted earlier and intended to come back but never did. Here we go!
Scripts
In my project, I have a migrations
folder at the root. Here's the first one:
// migration.js
/* jshint undef: true, unused: true, esversion: 6, node: true */
"use strict";
//
// G E T
// P A C K A G E S
const DB = process.env.DB || "YOUR_DATABASE_NAME";
const debug = require("debug")("migrate");
const MongoClient = require("mongodb").MongoClient;
const PORT = process.env.PORT || "27017";
//
// B E G I N
function done(db, err) {
if (db && db.close) db.close();
if (err) debug(`err (name = ${err.name}, message = ${err.message}): ${err.stack}`);
process.exit(0);
}
function run(migrate) {
MongoClient.connect(`mongodb://localhost:${PORT}/${DB}`, (err, db) => {
try {
migrate(err, db, done);
} catch (err) {
done(db, err);
}
});
}
module.exports = { run };
And here's the second file (example), where the migrations will be taking place (I actually have two migration scripts because I couldn't figure out how to rename and turn a String
into an Array
):
// update-domains.js
/* jshint undef: true, unused: true, esversion: 6, node: true */
"use strict";
//
// G E T
// P A C K A G E S
const debug = require("debug")("migrate");
const migration = require("./migration");
const test = require("assert");
//
// B E G I N
migration.run((err, db, done) => {
if (err) done(db, err);
const ds = db.collection("domains");
ds.find().toArray().then(domains => {
test.ok(domains.length > 0, "Expecting one or more domains");
debug("Domains:", domains.length);
const promises = [];
domains.forEach(domain => {
const registrar = domain.registrar;
promises.push(
ds.findAndModify(
{ _id: domain._id },
[["_id", 1]],
{ $rename: {
domain: "name",
expiration: "expires" // this is new but doesn't need to be set below
}}
),
ds.update(
{ _id: domain._id },
{
$unset: {
registrar: ""
},
$set: {
created: "",
changed: "",
dnssec: "",
registered: "",
status: "",
nameservers: [],
"contacts.admin.handle": "",
"contacts.admin.type": "",
"contacts.admin.name": "",
"contacts.admin.organization": "",
"contacts.admin.email": "",
"contacts.admin.address": "",
"contacts.admin.zipcode": "",
"contacts.admin.city": "",
"contacts.admin.state": "",
"contacts.admin.country": "",
"contacts.admin.phone": "",
"contacts.admin.fax": "",
"contacts.admin.created": "",
"contacts.admin.changed": "",
"contacts.owner.handle": "",
"contacts.owner.type": "",
"contacts.owner.name": "",
"contacts.owner.organization": "",
"contacts.owner.email": "",
"contacts.owner.address": "",
"contacts.owner.zipcode": "",
"contacts.owner.city": "",
"contacts.owner.state": "",
"contacts.owner.country": "",
"contacts.owner.phone": "",
"contacts.owner.fax": "",
"contacts.owner.created": "",
"contacts.owner.changed": "",
"contacts.tech.handle": "",
"contacts.tech.type": "",
"contacts.tech.name": "",
"contacts.tech.organization": "",
"contacts.tech.email": "",
"contacts.tech.address": "",
"contacts.tech.zipcode": "",
"contacts.tech.city": "",
"contacts.tech.state": "",
"contacts.tech.country": "",
"contacts.tech.phone": "",
"contacts.tech.fax": "",
"contacts.tech.created": "",
"contacts.tech.changed": "",
"official.id": "",
"official.name": registrar,
"official.email": "",
"official.url": "",
"official.phone": ""
}
},
{ upsert: true }
)
);
});
Promise.all(promises)
.then(convertedDomains => {
debug("Converted " + convertedDomains.length + " domains");
done(db);
})
.catch(err => {
done(db, err);
});
})
.catch(err => {
done(db, err);
});
});
Commands
Make sure to back up database first
Export everything
mongodump
A folder named dump
will be automatically created in migrations
.
Export database
mongodump --db YOUR_DATABASE_NAME
Export specific collection of a database
mongodump --db YOUR_DATABASE_NAME --collection users
(where users
is the collection name)
Migrate!
DEBUG=migrate DB=YOUR_DATABASE_NAME node migrations/update-domains.js
Restore specific collection to a database (because you done goofed)
mongorestore --collection domains --db YOUR_DATABASE_NAME dump/YOUR_DATABASE_NAME/domains.bson
End
And there you have it! I split my migration into two files and ran:
DEBUG=migrate DB=beachfrontd node migrations/update-domains-step-01.js
and
DEBUG=migrate DB=beachfrontd node migrations/update-domains-step-02.js
in succession. The process takes a couple seconds depending on the size of your database which is really quick. Hope this helps!
from migrate-mongoose.
@emmya since migrate-mongoose opens a NEW connection to the db, it won't use your existing connection with all the schemas registered on it.
You should probably just use your models in the migration as you would anywhere else in your code. Don't use the this('model')
method and just import your models on top of the file like usual
from migrate-mongoose.
I hate drive-by "just do this" comments without code. Anyone figure this out yet? I'll update if I find a solution.
from migrate-mongoose.
I figured out how to write a migration script thanks to the help of someone in the FeathersJS Slack group. It's just querying direct from Mongo.
from migrate-mongoose.
I was able to run migrations by including extra mongoose connect call in each migration file, like this
if (mongoose.connection.readyState != mongoose.STATE_OPEN) {
mongoose.connect('mongodb://localhost/db'); // path to your db
}
then importing mongoose models should be working without throwing any schema error
from migrate-mongoose.
Is there a temporary fix for this?
from migrate-mongoose.
@balmasi could you elaborate a bit?
README.md file contains the following instructions:
Access to mongoose models
// Lets say you have a user model like this
// models/User.js
const UserSchema = new Schema({
firstName: String,
lastName: String,
});
module.exports = mongoose.model('user', UserSchema);
// 1459287720919-my-migration.js
export async function up() {
// Then you can access it in the migration like so
await this('user').update({}, {
$rename: { firstName: 'first' }
}, { multi: true });
// Or something such as
const users = this('user').find();
/* Do something with users */
}
Which is clearly what I'm trying to do, but it doesn't work this way and you admit this. So what's the point of this('model')
if it cannot be used? And how it should look like in the simplest case? Thanks!
from migrate-mongoose.
I can totally see why the this('modelname')
parts of the doc are confusing.
They only apply (or are useful I should say) if you're using the library programmatically.
If you set up your model/schema registrations correctly, you simply import them into the migration and use them.
Another potentially confusing fact is that this library opens a separate connection to the db. This is why your model schemas are not registered with the connection when you use this
. This is not an issue if you're using it programmatically and pass in your connection.
I've made sure to outline all these in the README and provided even more examples.
I hope that clears things up.
TLDR:
- You're probably using it wrong cause my docs sucked. Fixed.
- won't be changing this behaviour unless someone points out something I've missed
Closing but leaving discussions open.
from migrate-mongoose.
Related Issues (20)
- New Server Discover and Monitoring engine for MongoClient HOT 2
- Cannot find module '@/enums/roles'
- Use NPX instead of referencing the node module
- Ability to specify migrations for a given NODE_ENV, or support seeding for local versus migrations for production
- Microservice environment
- Are ES6 imports supported in 4.0.0? HOT 2
- Deprecation warning HOT 1
- Support Migration files with *.ts extension HOT 4
- Is this repo still maintained? HOT 4
- How do we setup migrate.json HOT 1
- I am getting "Cannot use import statement outside a module" HOT 4
- can i use migrate-mongoose in midway framework?
- up never get's past "Synchronizing database with file system migrations" HOT 1
- Unexpected token { when using the package in typescript project
- Is there a way to grab the `connectionUri` or any other useful information in the `up` or `down` functions?
- Schema hasn't been registered HOT 1
- Security vulnerability on dangling dependency of yargs "^4.8.1"
- Compromised colors package HOT 1
- Engine warning and timeout HOT 6
- Generate initial migrations?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from migrate-mongoose.