0216__move_content_into_blobs.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. module.exports = () => {
  2. const sql = require('../../src/services/sql.js');
  3. const utils = require('../../src/services/utils.js');
  4. const existingBlobIds = new Set();
  5. for (const noteId of sql.getColumn(`SELECT noteId FROM note_contents`)) {
  6. const row = sql.getRow(`SELECT noteId, content, dateModified, utcDateModified FROM note_contents WHERE noteId = ?`, [noteId]);
  7. const blobId = utils.hashedBlobId(row.content);
  8. if (!existingBlobIds.has(blobId)) {
  9. existingBlobIds.add(blobId);
  10. sql.insert('blobs', {
  11. blobId,
  12. content: row.content,
  13. dateModified: row.dateModified,
  14. utcDateModified: row.utcDateModified
  15. });
  16. sql.execute("UPDATE entity_changes SET entityName = 'blobs', entityId = ? WHERE entityName = 'note_contents' AND entityId = ?", [blobId, row.noteId]);
  17. } else {
  18. // duplicates
  19. sql.execute("DELETE FROM entity_changes WHERE entityName = 'note_contents' AND entityId = ?", [row.noteId]);
  20. }
  21. sql.execute('UPDATE notes SET blobId = ? WHERE noteId = ?', [blobId, row.noteId]);
  22. }
  23. for (const noteRevisionId of sql.getColumn(`SELECT noteRevisionId FROM note_revision_contents`)) {
  24. const row = sql.getRow(`SELECT noteRevisionId, content, utcDateModified FROM note_revision_contents WHERE noteRevisionId = ?`, [noteRevisionId]);
  25. const blobId = utils.hashedBlobId(row.content);
  26. if (!existingBlobIds.has(blobId)) {
  27. existingBlobIds.add(blobId);
  28. sql.insert('blobs', {
  29. blobId,
  30. content: row.content,
  31. dateModified: row.utcDateModified,
  32. utcDateModified: row.utcDateModified
  33. });
  34. sql.execute("UPDATE entity_changes SET entityName = 'blobs', entityId = ? WHERE entityName = 'note_revision_contents' AND entityId = ?", [blobId, row.noteRevisionId]);
  35. } else {
  36. // duplicates
  37. sql.execute("DELETE FROM entity_changes WHERE entityName = 'note_revision_contents' AND entityId = ?", [row.noteId]);
  38. }
  39. sql.execute('UPDATE note_revisions SET blobId = ? WHERE noteRevisionId = ?', [blobId, row.noteRevisionId]);
  40. }
  41. const notesWithoutBlobIds = sql.getColumn("SELECT noteId FROM notes WHERE blobId IS NULL");
  42. if (notesWithoutBlobIds.length > 0) {
  43. throw new Error("BlobIds were not filled correctly in notes: " + JSON.stringify(notesWithoutBlobIds));
  44. }
  45. const noteRevisionsWithoutBlobIds = sql.getColumn("SELECT noteRevisionId FROM note_revisions WHERE blobId IS NULL");
  46. if (noteRevisionsWithoutBlobIds.length > 0) {
  47. throw new Error("BlobIds were not filled correctly in note revisions: " + JSON.stringify(noteRevisionsWithoutBlobIds));
  48. }
  49. };