overleaf/services/history-v1/migrations/20221027201324_unique_start_version.js
Alf Eaton ee85d948e2 Avoid duplicating a math-closing dollar sign (#11227)
GitOrigin-RevId: ef2ef77e26df59d1af3df6dc664e284d3c70102d
2023-01-16 08:41:42 +00:00

41 lines
1.1 KiB
JavaScript

exports.config = {
// CREATE INDEX CONCURRENTLY can't be run inside a transaction
// If this migration fails in the middle, indexes and constraints will have
// to be cleaned up manually.
transaction: false,
}
exports.up = async function (knex) {
await knex.raw(`
ALTER TABLE chunks
ADD CONSTRAINT chunks_start_version_non_negative
CHECK (start_version IS NOT NULL AND start_version >= 0)
NOT VALID
`)
await knex.raw(`
ALTER TABLE chunks
VALIDATE CONSTRAINT chunks_start_version_non_negative
`)
await knex.raw(`
CREATE UNIQUE INDEX CONCURRENTLY index_chunks_on_doc_id_and_start_version
ON chunks (doc_id, start_version)
`)
await knex.raw(`
ALTER TABLE chunks
ADD UNIQUE USING INDEX index_chunks_on_doc_id_and_start_version
`)
}
exports.down = async function (knex) {
await knex.raw(`
ALTER TABLE chunks
DROP CONSTRAINT IF EXISTS index_chunks_on_doc_id_and_start_version
`)
await knex.raw(`
DROP INDEX IF EXISTS index_chunks_on_doc_id_and_start_version
`)
await knex.raw(`
ALTER TABLE chunks
DROP CONSTRAINT IF EXISTS chunks_start_version_non_negative
`)
}