20221027201324_unique_start_version.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. exports.config = {
  2. // CREATE INDEX CONCURRENTLY can't be run inside a transaction
  3. // If this migration fails in the middle, indexes and constraints will have
  4. // to be cleaned up manually.
  5. transaction: false,
  6. }
  7. exports.up = async function (knex) {
  8. await knex.raw(`
  9. ALTER TABLE chunks
  10. ADD CONSTRAINT chunks_start_version_non_negative
  11. CHECK (start_version IS NOT NULL AND start_version >= 0)
  12. NOT VALID
  13. `)
  14. await knex.raw(`
  15. ALTER TABLE chunks
  16. VALIDATE CONSTRAINT chunks_start_version_non_negative
  17. `)
  18. await knex.raw(`
  19. CREATE UNIQUE INDEX CONCURRENTLY index_chunks_on_doc_id_and_start_version
  20. ON chunks (doc_id, start_version)
  21. `)
  22. await knex.raw(`
  23. ALTER TABLE chunks
  24. ADD UNIQUE USING INDEX index_chunks_on_doc_id_and_start_version
  25. `)
  26. }
  27. exports.down = async function (knex) {
  28. await knex.raw(`
  29. ALTER TABLE chunks
  30. DROP CONSTRAINT IF EXISTS index_chunks_on_doc_id_and_start_version
  31. `)
  32. await knex.raw(`
  33. DROP INDEX IF EXISTS index_chunks_on_doc_id_and_start_version
  34. `)
  35. await knex.raw(`
  36. ALTER TABLE chunks
  37. DROP CONSTRAINT IF EXISTS chunks_start_version_non_negative
  38. `)
  39. }