Migrate atlasId -> collectionId

This commit is contained in:
Tom Moor
2018-08-07 23:23:26 -07:00
parent f49c495a9a
commit 18cfe26e83
11 changed files with 50 additions and 24 deletions

View File

@@ -53,7 +53,7 @@ const Collection = sequelize.define(
// Create intro document if first collection for team
const document = await Document.create({
parentDocumentId: null,
atlasId: collection.id,
collectionId: collection.id,
teamId: collection.teamId,
userId: collection.creatorId,
lastModifiedById: collection.creatorId,
@@ -78,7 +78,7 @@ const Collection = sequelize.define(
Collection.associate = models => {
Collection.hasMany(models.Document, {
as: 'documents',
foreignKey: 'atlasId',
foreignKey: 'collectionId',
onDelete: 'cascade',
});
Collection.belongsTo(models.Team, {
@@ -99,7 +99,7 @@ Collection.associate = models => {
Collection.addHook('afterDestroy', async model => {
await Document.destroy({
where: {
atlasId: model.id,
collectionId: model.id,
},
});
});

View File

@@ -124,7 +124,7 @@ describe('#updateDocument', () => {
// Add a child for testing
const newDocument = await Document.create({
parentDocumentId: document.id,
atlasId: collection.id,
collectionId: collection.id,
teamId: collection.teamId,
userId: collection.creatorId,
lastModifiedById: collection.creatorId,
@@ -160,7 +160,7 @@ describe('#moveDocument', () => {
// Add a child for testing
const newDocument = await Document.create({
parentDocumentId: document.id,
atlasId: collection.id,
collectionId: collection.id,
teamId: collection.teamId,
userId: collection.creatorId,
lastModifiedById: collection.creatorId,
@@ -195,7 +195,7 @@ describe('#removeDocument', () => {
// Verify that the document was removed
const collectionDocuments = await Document.findAndCountAll({
where: {
atlasId: collection.id,
collectionId: collection.id,
},
});
expect(collectionDocuments.count).toBe(1);
@@ -207,7 +207,7 @@ describe('#removeDocument', () => {
// Add a child for testing
const newDocument = await Document.create({
parentDocumentId: document.id,
atlasId: collection.id,
collectionId: collection.id,
teamId: collection.teamId,
userId: collection.creatorId,
lastModifiedById: collection.creatorId,
@@ -223,7 +223,7 @@ describe('#removeDocument', () => {
expect(collection.documentStructure.length).toBe(1);
const collectionDocuments = await Document.findAndCountAll({
where: {
atlasId: collection.id,
collectionId: collection.id,
},
});
expect(collectionDocuments.count).toBe(1);
@@ -235,7 +235,7 @@ describe('#removeDocument', () => {
// Add a child for testing
const newDocument = await Document.create({
parentDocumentId: document.id,
atlasId: collection.id,
collectionId: collection.id,
teamId: collection.teamId,
userId: collection.creatorId,
lastModifiedById: collection.creatorId,
@@ -257,7 +257,7 @@ describe('#removeDocument', () => {
const collectionDocuments = await Document.findAndCountAll({
where: {
atlasId: collection.id,
collectionId: collection.id,
},
});
expect(collectionDocuments.count).toBe(2);

View File

@@ -119,7 +119,7 @@ const Document = sequelize.define(
Document.associate = models => {
Document.belongsTo(models.Collection, {
as: 'collection',
foreignKey: 'atlasId',
foreignKey: 'collectionId',
onDelete: 'cascade',
});
Document.belongsTo(models.Team, {
@@ -266,7 +266,7 @@ Document.searchForUser = async (
Document.addHook('beforeSave', async model => {
if (!model.publishedAt) return;
const collection = await Collection.findById(model.atlasId);
const collection = await Collection.findById(model.collectionId);
if (collection.type !== 'atlas') return;
await collection.updateDocument(model);
@@ -276,7 +276,7 @@ Document.addHook('beforeSave', async model => {
Document.addHook('afterCreate', async model => {
if (!model.publishedAt) return;
const collection = await Collection.findById(model.atlasId);
const collection = await Collection.findById(model.collectionId);
if (collection.type !== 'atlas') return;
await collection.addDocumentToStructure(model);
@@ -295,7 +295,7 @@ Document.addHook('afterDestroy', model =>
Document.prototype.publish = async function() {
if (this.publishedAt) return this.save();
const collection = await Collection.findById(this.atlasId);
const collection = await Collection.findById(this.collectionId);
if (collection.type !== 'atlas') return this.save();
await collection.addDocumentToStructure(this);

View File

@@ -20,6 +20,10 @@ Event.associate = models => {
as: 'collection',
foreignKey: 'collectionId',
});
Event.belongsTo(models.Collection, {
as: 'document',
foreignKey: 'documentId',
});
Event.belongsTo(models.Team, {
as: 'team',
foreignKey: 'teamId',