From 7fcf088573e0f6e272a698110e2862b3c3e63a94 Mon Sep 17 00:00:00 2001 From: Jure Triglav <juretriglav@gmail.com> Date: Sat, 9 Feb 2019 16:35:34 +1300 Subject: [PATCH] feat(db-manager): remove addCollection and addFragment functions BREAKING CHANGE: If you were relying on db-manager to add collections and fragments, this will no longer work. Since Collections and Fragments are now merely model components (previously part of core), on equal footing with the rest of the model components, the recommended migration path is to use your own seed script, e.g. https://gitlab.coko.foundation/pubsweet/pubsweet-starter/blob/master/scripts/seed.js to perform database setup/seed. --- .../db-manager/src/commands/add-collection.js | 24 ---------- .../db-manager/src/commands/add-fragment.js | 20 -------- packages/db-manager/src/index.js | 2 - .../test/commands/add-collection.test.js | 46 ------------------- .../test/commands/add-fragment.test.js | 33 ------------- 5 files changed, 125 deletions(-) delete mode 100644 packages/db-manager/src/commands/add-collection.js delete mode 100644 packages/db-manager/src/commands/add-fragment.js delete mode 100644 packages/db-manager/test/commands/add-collection.test.js delete mode 100644 packages/db-manager/test/commands/add-fragment.test.js diff --git a/packages/db-manager/src/commands/add-collection.js b/packages/db-manager/src/commands/add-collection.js deleted file mode 100644 index a66223025..000000000 --- a/packages/db-manager/src/commands/add-collection.js +++ /dev/null @@ -1,24 +0,0 @@ -const logger = require('@pubsweet/logger') - -module.exports = async (collectionData, fragment = null) => { - const { User, Collection } = require('@pubsweet/models') - - logger.info('Creating collection') - - const collection = await new Collection(collectionData).save() - const [user] = await User.all() - - if (user) collection.setOwners([user.id]) - await collection.save() - - if (fragment) collection.addFragment(fragment) - await collection.save() - - logger.info( - `Successfully created collection ${ - user ? `and set ${user.id} as owner` : 'with no owner' - }`, - ) - - return collection -} diff --git a/packages/db-manager/src/commands/add-fragment.js b/packages/db-manager/src/commands/add-fragment.js deleted file mode 100644 index c21c7ca4d..000000000 --- a/packages/db-manager/src/commands/add-fragment.js +++ /dev/null @@ -1,20 +0,0 @@ -const logger = require('@pubsweet/logger') - -module.exports = async fragmentData => { - const { Fragment, User } = require('@pubsweet/models') - - logger.info('Creating fragment') - - const fragment = await new Fragment(fragmentData).save() - const [user] = await User.all() - - if (user) fragment.setOwners([user.id]) - await fragment.save() - - logger.info( - `Successfully created fragment ${ - user ? `and set ${user.id} as owner` : 'with no owner' - }`, - ) - return fragment -} diff --git a/packages/db-manager/src/index.js b/packages/db-manager/src/index.js index e817214f6..30a4f3095 100644 --- a/packages/db-manager/src/index.js +++ b/packages/db-manager/src/index.js @@ -3,6 +3,4 @@ module.exports.createTables = require('./commands/create-tables') module.exports.setupDb = require('./commands/setup-db') module.exports.migrate = require('./commands/migrate') module.exports.addUser = require('./commands/add-user') -module.exports.addCollection = require('./commands/add-collection') -module.exports.addFragment = require('./commands/add-fragment') module.exports.dbExists = require('./helpers/db-exists') diff --git a/packages/db-manager/test/commands/add-collection.test.js b/packages/db-manager/test/commands/add-collection.test.js deleted file mode 100644 index d4ae308e8..000000000 --- a/packages/db-manager/test/commands/add-collection.test.js +++ /dev/null @@ -1,46 +0,0 @@ -const { addCollection, createTables } = require('../../src') - -const { Collection, Fragment, User } = require('@pubsweet/models') - -describe('add-collection', () => { - beforeEach(() => createTables(true)) - - it('adds a collection to the database', async () => { - await addCollection({}) - const [actualCollection] = await Collection.all() - expect(actualCollection).toMatchObject({ - type: 'collection', - owners: [], - }) - }) - - it('adds user as owner', async () => { - const user = await new User({ - username: 'user', - email: 'test@example.com', - password: 'password', - }).save() - - await addCollection({}) - const [actualCollection] = await Collection.all() - expect(actualCollection).toMatchObject({ - type: 'collection', - owners: [user.id], - }) - }) - - it('adds fragment to collection', async () => { - const fragment = await new Fragment({ - fragmentType: 'version', - }).save() - - await addCollection({}, fragment) - const [actualCollection] = await Collection.all() - - expect(actualCollection).toMatchObject({ - type: 'collection', - owners: [], - fragments: [fragment.id], - }) - }) -}) diff --git a/packages/db-manager/test/commands/add-fragment.test.js b/packages/db-manager/test/commands/add-fragment.test.js deleted file mode 100644 index eb6298a32..000000000 --- a/packages/db-manager/test/commands/add-fragment.test.js +++ /dev/null @@ -1,33 +0,0 @@ -const { addFragment, createTables } = require('../../src') - -const { Fragment, User } = require('@pubsweet/models') - -describe('add-fragment', () => { - beforeEach(() => createTables(true)) - - it('adds a fragment to the database', async () => { - await addFragment({ fragmentType: 'version' }) - const [actualFragment] = await Fragment.all() - expect(actualFragment).toMatchObject({ - type: 'fragment', - fragmentType: 'version', - owners: [], - }) - }) - - it('adds user as owner', async () => { - const user = await new User({ - username: 'user', - email: 'test@example.com', - password: 'password', - }).save() - - await addFragment({ fragmentType: 'version' }) - const [actualFragment] = await Fragment.all() - expect(actualFragment).toMatchObject({ - type: 'fragment', - fragmentType: 'version', - owners: [user.id], - }) - }) -}) -- GitLab