Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Commit

Permalink
Merge pull request #30 from reactioncommerce/fix-28-mohan-multishop-d…
Browse files Browse the repository at this point in the history
…up-tag-create

fix: allow different shops to have same tag name
  • Loading branch information
MohanNarayana authored Aug 18, 2021
2 parents 0413a6d + 8be6d95 commit fe93538
Show file tree
Hide file tree
Showing 8 changed files with 22,222 additions and 100 deletions.
2 changes: 2 additions & 0 deletions index.js
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;
20 changes: 20 additions & 0 deletions migrations/2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @summary remove index slug_1 from tags
* @param {Object} args - the arguments
* @param {Object} args.db - the DB client
* @param {Function} args.progress - a function to set the progress of the operation
* @returns {undefined}
*/
async function up({ db, progress }) {
progress(0);

const index = "slug_1";
await db.collection("Tags").dropIndex(index);

progress(100);
}

export default {
down: "unnecessary",
up
};
13 changes: 13 additions & 0 deletions migrations/index.js
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 migrations/migrationsNamespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const migrationsNamespace = "tags";
22,247 changes: 22,148 additions & 99 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"dependencies": {
"@reactioncommerce/api-utils": "^1.16.5",
"@reactioncommerce/data-factory": "~1.0.1",
"@reactioncommerce/db-version-check": "^1.0.0",
"@reactioncommerce/file-collections": "~0.9.3",
"@reactioncommerce/random": "~1.0.2",
"@reactioncommerce/reaction-error": "~1.0.1",
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import queries from "./queries/index.js";
import resolvers from "./resolvers/index.js";
import schemas from "./schemas/index.js";
import { Tag } from "./simpleSchemas.js";
import preStartup from "./preStartup.js";

/**
* @summary Import and call this function to add this plugin to your API.
Expand All @@ -18,6 +19,9 @@ export default async function register(app) {
name: "tags",
version: pkg.version,
i18n,
functionsByType: {
preStartup: [preStartup]
},
collections: {
Tags: {
name: "Tags",
Expand All @@ -27,7 +31,7 @@ export default async function register(app) {
[{ name: 1 }, { name: "c2_name" }],
[{ relatedTagIds: 1 }, { name: "c2_relatedTagIds" }],
[{ shopId: 1 }, { name: "c2_shopId" }],
[{ slug: 1 }, { unique: true }]
[{ slug: 1, shopId: 1 }, { unique: true }]
]
}
},
Expand Down
32 changes: 32 additions & 0 deletions src/preStartup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import doesDatabaseVersionMatch from "@reactioncommerce/db-version-check";
import { migrationsNamespace } from "../migrations/migrationsNamespace.js";

const expectedVersion = 2;

/**
* @summary Called before startup
* @param {Object} context Startup context
* @returns {undefined}
*/
export default async function preStartup(context) {
const setToExpectedIfMissing = async () => {
const anyAccount = await context.collections.Accounts.findOne();
const anyGroup = await context.collections.Groups.findOne();
return !anyAccount && !anyGroup;
};

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}. See docs for more information on migrations: https://github.com/reactioncommerce/api-migrations`);
}
}

0 comments on commit fe93538

Please sign in to comment.