mirror of
https://github.com/outline/outline.git
synced 2026-11-28 16:19:46 +03:00
perf: Make the personalOwnerId migration safe on large tables
The index builds with CONCURRENTLY and the check constraint is added NOT VALID then validated separately, so neither blocks writes to documents. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
01236c3841
commit
32db24a4b0
1 file changed
+13
-2
@@ -3,6 +3,8 @@
|
||||
/** @type {import('sequelize-cli').Migration} */
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
// Adding a nullable column with a foreign key takes only a brief lock, as
|
||||
// there are no existing rows to validate.
|
||||
await queryInterface.addColumn("documents", "personalOwnerId", {
|
||||
type: Sequelize.UUID,
|
||||
allowNull: true,
|
||||
@@ -13,21 +15,30 @@ module.exports = {
|
||||
onDelete: "set null",
|
||||
});
|
||||
|
||||
// CONCURRENTLY so the build does not block writes to the table. It cannot
|
||||
// run inside a transaction.
|
||||
await queryInterface.sequelize.query(`
|
||||
CREATE INDEX documents_personal_owner_id
|
||||
CREATE INDEX CONCURRENTLY IF NOT EXISTS documents_personal_owner_id
|
||||
ON documents ("personalOwnerId")
|
||||
WHERE "personalOwnerId" IS NOT NULL AND "deletedAt" IS NULL
|
||||
`);
|
||||
|
||||
// A published document lives in exactly one place: a collection, or a
|
||||
// person's own space. Drafts and templates live in neither.
|
||||
//
|
||||
// NOT VALID makes the ADD CONSTRAINT a metadata-only change, and the
|
||||
// separate VALIDATE scans existing rows under a lock that does not block
|
||||
// reads or writes.
|
||||
await queryInterface.sequelize.query(`
|
||||
ALTER TABLE documents ADD CONSTRAINT documents_single_home CHECK (
|
||||
"publishedAt" IS NULL
|
||||
OR template = true
|
||||
OR (("collectionId" IS NULL) <> ("personalOwnerId" IS NULL))
|
||||
)
|
||||
) NOT VALID
|
||||
`);
|
||||
await queryInterface.sequelize.query(
|
||||
`ALTER TABLE documents VALIDATE CONSTRAINT documents_single_home`
|
||||
);
|
||||
},
|
||||
|
||||
async down(queryInterface) {
|
||||
|
||||
Reference in new issue
Block a user