-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New data migration fulfillment #6633
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
Merged
sujithvn
merged 10 commits into
00-fulfillment-base
from
new-data-migration-fulfillment
May 18, 2023
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
79eac5e
feat: data migration for fulfillment feature
sujithvn c5ab64e
feat: migrate shopsetting for default ff-type
sujithvn a6356cb
fix: migration script fix
sujithvn 3d016d7
fix: review comment fixes
sujithvn 3acaeb4
fix: typo in 2.js
sujithvn bc2974d
fix: review comment fixes2
sujithvn 3404407
fix: pnpm-lock without snyk
sujithvn 1b09f81
Merge branch '00-fulfillment-base' of github.com:reactioncommerce/rea…
sujithvn 9193072
Merge branch '00-fulfillment-base' into new-data-migration-fulfillment
sujithvn 13bab48
fix: linter error
sujithvn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import register from "./src/index.js"; | ||
|
||
export { default as migrations } from "./migrations/index.js"; | ||
|
||
export default register; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
const COLL_FF_SOURCE = "Shipping"; | ||
const COLL_FF_DEST = "Fulfillment"; | ||
const COLL_FFR_SOURCE = "FlatRateFulfillmentRestrictions"; | ||
const COLL_FFR_DEST = "FulfillmentRestrictions"; | ||
const FF_TYPE = "shipping"; | ||
const FF_METHOD = "flatRate"; | ||
const COLL_DEST = "Fulfillment"; | ||
|
||
const newGlobalPermissions = [ | ||
"reaction:legacy:fulfillmentTypes/update:settings", | ||
"reaction:legacy:fulfillmentRestrictions/create", | ||
"reaction:legacy:fulfillmentRestrictions/delete", | ||
"reaction:legacy:fulfillmentRestrictions/read", | ||
"reaction:legacy:fulfillmentRestrictions/update", | ||
"reaction:legacy:fulfillmentTypes/create", | ||
"reaction:legacy:fulfillmentTypes/delete", | ||
"reaction:legacy:fulfillmentTypes/read", | ||
"reaction:legacy:fulfillmentTypes/update", | ||
"reaction:legacy:fulfillmentMethods/create", | ||
"reaction:legacy:fulfillmentMethods/delete", | ||
"reaction:legacy:fulfillmentMethods/read", | ||
"reaction:legacy:fulfillmentMethods/update" | ||
]; | ||
|
||
/** | ||
* @summary migrates the database down one version | ||
* @param {Object} context Migration context | ||
* @param {Object} context.db MongoDB `Db` instance | ||
* @param {Function} context.progress A function to report progress, takes percent | ||
* number as argument. | ||
* @return {undefined} | ||
*/ | ||
async function down({ db, progress }) { | ||
progress(0); | ||
|
||
const allGroups = await db.collection("Groups").find({}).toArray(); | ||
const affectedGlobalGroups = []; | ||
allGroups.forEach((group) => { | ||
const perms = group.permissions; | ||
if (perms && Array.isArray(perms) && perms.length) { | ||
const found = newGlobalPermissions.some((elem) => perms.includes(elem)); | ||
if (found) affectedGlobalGroups.push(group.slug); | ||
} | ||
}); | ||
|
||
await db.collection("Groups").updateMany({ | ||
slug: { $in: affectedGlobalGroups } | ||
}, { | ||
$pullAll: { permissions: newGlobalPermissions } | ||
}); | ||
progress(10); | ||
|
||
await db.collection(COLL_FF_DEST).drop(); | ||
progress(50); | ||
|
||
await db.collection(COLL_FFR_DEST).drop(); | ||
progress(100); | ||
} | ||
|
||
/** | ||
* @summary Performs migration up from previous data version | ||
* @param {Object} context Migration context | ||
* @param {Object} context.db MongoDB `Db` instance | ||
* @param {Function} context.progress A function to report progress, takes percent | ||
* number as argument. | ||
* @return {undefined} | ||
*/ | ||
async function up({ db, progress }) { | ||
progress(0); | ||
|
||
const shippingCopyResp = await db.collection(COLL_FF_SOURCE).aggregate([{ $match: {} }, { $out: COLL_FF_DEST }]).next(); | ||
if (shippingCopyResp) throw new Error("Error in copying Shipping collection"); // above command returns null if successful | ||
progress(20); | ||
|
||
const flatRateRestCopyResp = await db.collection(COLL_FFR_SOURCE).aggregate([{ $match: {} }, { $out: COLL_FFR_DEST }]).next(); | ||
if (flatRateRestCopyResp) throw new Error("Error in copying FlatRateFulfillmentRestrictions collection"); // above command returns null if successful | ||
progress(40); | ||
|
||
const operations = []; | ||
|
||
const ffTypeUpdate = { | ||
updateMany: { | ||
filter: { fulfillmentType: { $exists: false } }, | ||
update: { | ||
$set: { | ||
fulfillmentType: FF_TYPE | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const ffMethodUpdate = { | ||
updateMany: { | ||
filter: { methods: { $exists: true } }, | ||
update: { | ||
$set: { | ||
"methods.$[eachMethod].fulfillmentMethod": FF_METHOD | ||
} | ||
}, | ||
arrayFilters: [ | ||
{ | ||
"eachMethod.fulfillmentMethod": { $exists: false } | ||
} | ||
] | ||
} | ||
}; | ||
|
||
operations.push(ffTypeUpdate); | ||
operations.push(ffMethodUpdate); | ||
|
||
const bulkWriteResp = await db.collection(COLL_DEST).bulkWrite(operations); | ||
if (bulkWriteResp.writeErrors && bulkWriteResp.writeErrors.length) throw new Error("Error while updating Fulfillment collection"); | ||
progress(50); | ||
|
||
const oldPerms = [ | ||
"reaction:legacy:shippingMethods/create", | ||
"reaction:legacy:shippingMethods/delete", | ||
"reaction:legacy:shippingMethods/read", | ||
"reaction:legacy:shippingMethods/update", | ||
"reaction:legacy:shippingRestrictions/create", | ||
"reaction:legacy:shippingRestrictions/delete", | ||
"reaction:legacy:shippingRestrictions/read", | ||
"reaction:legacy:shippingRestrictions/update" | ||
]; | ||
|
||
const mapperSetFFMethodsRestricts = { | ||
"reaction:legacy:shippingMethods/create": "reaction:legacy:fulfillmentMethods/create", | ||
"reaction:legacy:shippingMethods/delete": "reaction:legacy:fulfillmentMethods/delete", | ||
"reaction:legacy:shippingMethods/read": "reaction:legacy:fulfillmentMethods/read", | ||
"reaction:legacy:shippingMethods/update": "reaction:legacy:fulfillmentMethods/update", | ||
"reaction:legacy:shippingRestrictions/create": "reaction:legacy:fulfillmentRestrictions/create", | ||
"reaction:legacy:shippingRestrictions/delete": "reaction:legacy:fulfillmentRestrictions/delete", | ||
"reaction:legacy:shippingRestrictions/read": "reaction:legacy:fulfillmentRestrictions/read", | ||
"reaction:legacy:shippingRestrictions/update": "reaction:legacy:fulfillmentRestrictions/update" | ||
}; | ||
const mapperSetFFTypes = { | ||
"reaction:legacy:shippingMethods/create": "reaction:legacy:fulfillmentTypes/create", | ||
"reaction:legacy:shippingMethods/delete": "reaction:legacy:fulfillmentTypes/delete", | ||
"reaction:legacy:shippingMethods/read": "reaction:legacy:fulfillmentTypes/read", | ||
"reaction:legacy:shippingMethods/update": "reaction:legacy:fulfillmentTypes/update" | ||
}; | ||
const allGroups = await db.collection("Groups").find({}).toArray(); | ||
|
||
for (let index = 0; index < allGroups.length; index += 1) { | ||
const currentGroup = allGroups[index]; | ||
const currentPerms = currentGroup.permissions; | ||
const permsToAdd = []; | ||
|
||
if (currentPerms && Array.isArray(currentPerms) && currentPerms.length) { | ||
oldPerms.forEach((oldPerm) => { | ||
if (currentPerms.includes(oldPerm)) { | ||
permsToAdd.push(mapperSetFFMethodsRestricts[oldPerm]); | ||
if (mapperSetFFTypes[oldPerm]) { | ||
permsToAdd.push(mapperSetFFTypes[oldPerm]); | ||
} | ||
} | ||
}); | ||
} | ||
if (permsToAdd.length) { | ||
permsToAdd.push("reaction:legacy:fulfillmentTypes/update:settings"); // add this setting to groups deailing with ff-types | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo in comment: dealing |
||
// eslint-disable-next-line no-await-in-loop | ||
await db.collection("Groups").updateOne( | ||
{ _id: currentGroup._id }, | ||
{ | ||
$addToSet: { permissions: { $each: permsToAdd } } | ||
} | ||
); | ||
} | ||
} | ||
|
||
progress(100); | ||
} | ||
|
||
export default { | ||
down, | ||
up | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { migrationsNamespace } from "./migrationsNamespace.js"; | ||
import migration2 from "./2.js"; | ||
|
||
export default { | ||
tracks: [ | ||
{ | ||
namespace: migrationsNamespace, | ||
migrations: { | ||
2: migration2 | ||
} | ||
} | ||
] | ||
}; |
1 change: 1 addition & 0 deletions
1
packages/api-plugin-fulfillment/migrations/migrationsNamespace.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const migrationsNamespace = "fulfillment"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,45 @@ | ||
import doesDatabaseVersionMatch from "@reactioncommerce/db-version-check"; | ||
import { migrationsNamespace } from "../migrations/migrationsNamespace.js"; | ||
import { extendFulfillmentSchemas } from "./simpleSchemas.js"; | ||
|
||
const expectedVersion = 4; | ||
|
||
/** | ||
* @summary Checks if the version of the database matches requirement | ||
* @param {Object} context Startup context | ||
* @returns {undefined} | ||
*/ | ||
async function dbVersionCheck(context) { | ||
const setToExpectedIfMissing = async () => { | ||
const anyFulfillment = await context.collections.Fulfillment.findOne(); | ||
const anyRestriction = await context.collections.FulfillmentRestrictions.findOne(); | ||
|
||
return !anyFulfillment || !anyRestriction; | ||
}; | ||
|
||
const ok = await doesDatabaseVersionMatch({ | ||
// `db` is a Db instance from the `mongodb` NPM package, | ||
// such as what is returned when you do `client.db()` | ||
db: context.app.db, | ||
// These must match one of the namespaces and versions | ||
// your package exports in the `migrations` named export | ||
expectedVersion, | ||
namespace: migrationsNamespace, | ||
setToExpectedIfMissing | ||
}); | ||
|
||
if (!ok) { | ||
throw new Error(`Database needs migrating. The "${migrationsNamespace}" namespace must be at version ${expectedVersion}.`); | ||
} | ||
} | ||
|
||
/** | ||
* @summary Called before startup to extend schemas | ||
* @param {Object} context Startup context | ||
* @returns {undefined} | ||
*/ | ||
export default async function fulfillmentPreStartup(context) { | ||
await dbVersionCheck(context); | ||
const allFulfillmentTypesArray = context.fulfillment?.registeredFulfillmentTypes; | ||
extendFulfillmentSchemas(context.simpleSchemas, allFulfillmentTypesArray); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will probably work but it's not really what the bulkWrite API is really for an would probably be better as written as a single query. Bulkwrite is when you are doing a large amount of atomic operations, not one large one.