This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/server/models/Revision.js
Tom Moor 65a1e2630c
perf: Remove no-longer-used 'backup' columns (#2396)
* perf: Remove no-longer-used 'backup' columns

These were added as part of the move to the v2 editor over a year ago incase any text was not correctly converted. After a year of use no cases of failed conversion have occurred that required the use of this column

* Remove migration, will do in 2-step release
2021-07-30 07:22:17 -07:00

90 lines
2.1 KiB
JavaScript

// @flow
import MarkdownSerializer from "slate-md-serializer";
import { DataTypes, sequelize } from "../sequelize";
const serializer = new MarkdownSerializer();
const Revision = sequelize.define("revision", {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
version: DataTypes.SMALLINT,
editorVersion: DataTypes.STRING,
title: DataTypes.STRING,
text: DataTypes.TEXT,
});
Revision.associate = (models) => {
Revision.belongsTo(models.Document, {
as: "document",
foreignKey: "documentId",
onDelete: "cascade",
});
Revision.belongsTo(models.User, {
as: "user",
foreignKey: "userId",
});
Revision.addScope(
"defaultScope",
{
include: [{ model: models.User, as: "user", paranoid: false }],
},
{ override: true }
);
};
Revision.findLatest = function (documentId) {
return Revision.findOne({
where: {
documentId,
},
order: [["createdAt", "DESC"]],
});
};
Revision.createFromDocument = function (document, options) {
return Revision.create(
{
title: document.title,
text: document.text,
userId: document.lastModifiedById,
editorVersion: document.editorVersion,
version: document.version,
documentId: document.id,
// revision time is set to the last time document was touched as this
// handler can be debounced in the case of an update
createdAt: document.updatedAt,
},
options
);
};
Revision.prototype.migrateVersion = function () {
let migrated = false;
// migrate from document version 0 -> 1
if (!this.version) {
// removing the title from the document text attribute
this.text = this.text.replace(/^#\s(.*)\n/, "");
this.version = 1;
migrated = true;
}
// migrate from document version 1 -> 2
if (this.version === 1) {
const nodes = serializer.deserialize(this.text);
this.text = serializer.serialize(nodes, { version: 2 });
this.version = 2;
migrated = true;
}
if (migrated) {
return this.save({ silent: true, hooks: false });
}
};
export default Revision;