Quellcode durchsuchen

Fixes #2596 incorrect date types for created & updated

Justin Reynolds vor 5 Jahren
Ursprung
Commit
3b9f2ca7c2

+ 2 - 0
models/accountSettings.js

@@ -20,6 +20,8 @@ AccountSettings.attachSchema(
       autoValue() {
         if (this.isInsert) {
           return new Date();
+        } else if (this.isUpsert) {
+          return { $setOnInsert: new Date() };
         } else {
           this.unset();
         }

+ 10 - 0
models/actions.js

@@ -14,6 +14,16 @@ Actions.allow({
   },
 });
 
+Actions.before.insert((userId, doc) => {
+  doc.createdAt = new Date();
+  doc.modifiedAt = doc.createdAt;
+});
+
+Actions.before.update((userId, doc, fieldNames, modifier) => {
+  modifier.$set = modifier.$set || {};
+  modifier.$set.modifiedAt = new Date();
+});
+
 Actions.helpers({
   description() {
     return this.desc;

+ 6 - 0
models/activities.js

@@ -62,8 +62,14 @@ Activities.helpers({
   //},
 });
 
+Activities.before.update((userId, doc, fieldNames, modifier) => {
+  modifier.$set = modifier.$set || {};
+  modifier.$set.modifiedAt = new Date();
+});
+
 Activities.before.insert((userId, doc) => {
   doc.createdAt = new Date();
+  doc.modifiedAt = doc.createdAt;
 });
 
 Activities.after.insert((userId, doc) => {

+ 2 - 0
models/announcements.js

@@ -25,6 +25,8 @@ Announcements.attachSchema(
       autoValue() {
         if (this.isInsert) {
           return new Date();
+        } else if (this.isUpsert) {
+          return { $setOnInsert: new Date() };
         } else {
           this.unset();
         }

+ 2 - 0
models/boards.js

@@ -55,6 +55,8 @@ Boards.attachSchema(
       autoValue() {
         if (this.isInsert) {
           return new Date();
+        } else if (this.isUpsert) {
+          return { $setOnInsert: new Date() };
         } else {
           this.unset();
         }

+ 2 - 0
models/cardComments.js

@@ -34,6 +34,8 @@ CardComments.attachSchema(
       autoValue() {
         if (this.isInsert) {
           return new Date();
+        } else if (this.isUpsert) {
+          return { $setOnInsert: new Date() };
         } else {
           this.unset();
         }

+ 2 - 0
models/cards.js

@@ -107,6 +107,8 @@ Cards.attachSchema(
       autoValue() {
         if (this.isInsert) {
           return new Date();
+        } else if (this.isUpsert) {
+          return { $setOnInsert: new Date() };
         } else {
           this.unset();
         }

+ 2 - 0
models/checklistItems.js

@@ -44,6 +44,8 @@ ChecklistItems.attachSchema(
       autoValue() {
         if (this.isInsert) {
           return new Date();
+        } else if (this.isUpsert) {
+          return { $setOnInsert: new Date() };
         } else {
           this.unset();
         }

+ 2 - 0
models/checklists.js

@@ -35,6 +35,8 @@ Checklists.attachSchema(
       autoValue() {
         if (this.isInsert) {
           return new Date();
+        } else if (this.isUpsert) {
+          return { $setOnInsert: new Date() };
         } else {
           this.unset();
         }

+ 2 - 0
models/customFields.js

@@ -78,6 +78,8 @@ CustomFields.attachSchema(
       autoValue() {
         if (this.isInsert) {
           return new Date();
+        } else if (this.isUpsert) {
+          return { $setOnInsert: new Date() };
         } else {
           this.unset();
         }

+ 2 - 0
models/integrations.js

@@ -63,6 +63,8 @@ Integrations.attachSchema(
       autoValue() {
         if (this.isInsert) {
           return new Date();
+        } else if (this.isUpsert) {
+          return { $setOnInsert: new Date() };
         } else {
           this.unset();
         }

+ 2 - 0
models/invitationCodes.js

@@ -18,6 +18,8 @@ InvitationCodes.attachSchema(
       autoValue() {
         if (this.isInsert) {
           return new Date();
+        } else if (this.isUpsert) {
+          return { $setOnInsert: new Date() };
         } else {
           this.unset();
         }

+ 2 - 0
models/lists.js

@@ -45,6 +45,8 @@ Lists.attachSchema(
       autoValue() {
         if (this.isInsert) {
           return new Date();
+        } else if (this.isUpsert) {
+          return { $setOnInsert: new Date() };
         } else {
           this.unset();
         }

+ 2 - 0
models/org.js

@@ -98,6 +98,8 @@ Org.attachSchema(
       autoValue() {
         if (this.isInsert) {
           return new Date();
+        } else if (this.isUpsert) {
+          return { $setOnInsert: new Date() };
         } else {
           this.unset();
         }

+ 2 - 0
models/orgUser.js

@@ -49,6 +49,8 @@ OrgUser.attachSchema(
       autoValue() {
         if (this.isInsert) {
           return new Date();
+        } else if (this.isUpsert) {
+          return { $setOnInsert: new Date() };
         } else {
           this.unset();
         }

+ 2 - 0
models/rules.js

@@ -27,6 +27,8 @@ Rules.attachSchema(
       autoValue() {
         if (this.isInsert) {
           return new Date();
+        } else if (this.isUpsert) {
+          return { $setOnInsert: new Date() };
         } else {
           this.unset();
         }

+ 2 - 0
models/settings.js

@@ -60,6 +60,8 @@ Settings.attachSchema(
       autoValue() {
         if (this.isInsert) {
           return new Date();
+        } else if (this.isUpsert) {
+          return { $setOnInsert: new Date() };
         } else {
           this.unset();
         }

+ 2 - 0
models/swimlanes.js

@@ -38,6 +38,8 @@ Swimlanes.attachSchema(
       autoValue() {
         if (this.isInsert) {
           return new Date();
+        } else if (this.isUpsert) {
+          return { $setOnInsert: new Date() };
         } else {
           this.unset();
         }

+ 10 - 0
models/triggers.js

@@ -12,6 +12,16 @@ Triggers.mutations({
   },
 });
 
+Triggers.before.insert((userId, doc) => {
+  doc.createdAt = new Date();
+  doc.updatedAt = doc.createdAt;
+});
+
+Triggers.before.update((userId, doc, fieldNames, modifier) => {
+  modifier.$set = modifier.$set || {};
+  modifier.$set.updatedAt = new Date();
+});
+
 Triggers.allow({
   insert(userId, doc) {
     return allowIsBoardAdmin(userId, Boards.findOne(doc.boardId));

+ 2 - 0
models/unsavedEdits.js

@@ -29,6 +29,8 @@ UnsavedEditCollection.attachSchema(
       autoValue() {
         if (this.isInsert) {
           return new Date();
+        } else if (this.isUpsert) {
+          return { $setOnInsert: new Date() };
         } else {
           this.unset();
         }

+ 2 - 0
models/users.js

@@ -54,6 +54,8 @@ Users.attachSchema(
       autoValue() {
         if (this.isInsert) {
           return new Date();
+        } else if (this.isUpsert) {
+          return { $setOnInsert: new Date() };
         } else {
           this.unset();
         }

+ 41 - 33
server/migrations.js

@@ -684,39 +684,6 @@ Migrations.add('mutate-boardIds-in-customfields', () => {
   });
 });
 
-const firstBatchOfDbsToAddCreatedAndUpdated = [
-  AccountSettings,
-  Actions,
-  Activities,
-  Announcements,
-  Boards,
-  CardComments,
-  Cards,
-  ChecklistItems,
-  Checklists,
-  CustomFields,
-  Integrations,
-  InvitationCodes,
-  Lists,
-  Rules,
-  Settings,
-  Swimlanes,
-  Triggers,
-  UnsavedEdits,
-];
-
-firstBatchOfDbsToAddCreatedAndUpdated.forEach(db => {
-  db.before.insert((userId, doc) => {
-    doc.createdAt = Date.now();
-    doc.updatedAt = doc.createdAt;
-  });
-
-  db.before.update((userId, doc, fieldNames, modifier) => {
-    modifier.$set = modifier.$set || {};
-    modifier.$set.updatedAt = new Date();
-  });
-});
-
 const modifiedAtTables = [
   AccountSettings,
   Actions,
@@ -769,3 +736,44 @@ Migrations.add('add-missing-created-and-modified', () => {
       console.error(e);
     });
 });
+
+Migrations.add('fix-incorrect-dates', () => {
+  const tables = [
+    AccountSettings,
+    Actions,
+    Activities,
+    Announcements,
+    Boards,
+    CardComments,
+    Cards,
+    ChecklistItems,
+    Checklists,
+    CustomFields,
+    Integrations,
+    InvitationCodes,
+    Lists,
+    Rules,
+    Settings,
+    Swimlanes,
+    Triggers,
+    UnsavedEdits,
+  ];
+
+  // Dates were previously created with Date.now() which is a number, not a date
+  tables.forEach(t =>
+    t
+      .rawCollection()
+      .find({ $or: [{ createdAt: { $type: 1 } }, { updatedAt: { $type: 1 } }] })
+      .forEach(({ _id, createdAt, updatedAt }) => {
+        t.rawCollection().update(
+          { _id },
+          {
+            $set: {
+              createdAt: new Date(createdAt),
+              updatedAt: new Date(updatedAt),
+            },
+          },
+        );
+      }),
+  );
+});