-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschemaVersioning.js
More file actions
73 lines (64 loc) · 1.58 KB
/
Copy pathschemaVersioning.js
File metadata and controls
73 lines (64 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// assuming we have this model
const bookingSchema = new mongoose.Schema({
customerName: {
type: String,
required: true
},
pickupLocation: {
type: String,
required: true
},
dropoffLocation: {
type: String,
required: true
},
timestamp: {
type: Date,
default: Date.now
},
paymentMethod: {
type: String,
required: true
}
});
// Then we need to add one more file to it, so we have to add schena versioning colums in it
// so that we can distinguish data relations to which version
const bookingSchema = new mongoose.Schema({
schemaVersion: {
type: Number,
default: 1
},
customerName: {
type: String,
required: true
},
pickupLocation: {
type: String,
required: true
},
dropoffLocation: {
type: String,
required: true
},
timestamp: {
type: Date,
default: Date.now
},
paymentMethod: {
type: String,
required: true
}
});
// after adding the new column we have to sync data in previous data as well so when we will migrate data we should
// follo this
const Booking = require('./path/to/booking/model');
async function migrateBookingSchema() {
const bookings = await Booking.find({ schemaVersion: { $lt: 2 } });
for (const booking of bookings) {
booking.paymentMethod = 'unknown';
booking.schemaVersion = 2;
await booking.save();
}
console.log('Booking schema migration completed.');
}
migrateBookingSchema().catch(console.error);