20220228163642_initial.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * This is the initial migration, meant to replicate the current state of the
  3. * history database. If tables already exist, this migration is a noop.
  4. */
  5. exports.up = async function (knex) {
  6. await knex.raw(`
  7. CREATE TABLE IF NOT EXISTS chunks (
  8. id SERIAL,
  9. doc_id integer NOT NULL,
  10. end_version integer NOT NULL,
  11. end_timestamp timestamp without time zone,
  12. CONSTRAINT chunks_version_non_negative CHECK (end_version >= 0)
  13. )
  14. `)
  15. await knex.raw(`
  16. CREATE UNIQUE INDEX IF NOT EXISTS index_chunks_on_doc_id_and_end_version
  17. ON chunks (doc_id, end_version)
  18. `)
  19. await knex.raw(`
  20. CREATE TABLE IF NOT EXISTS old_chunks (
  21. chunk_id integer NOT NULL PRIMARY KEY,
  22. doc_id integer NOT NULL,
  23. end_version integer,
  24. end_timestamp timestamp without time zone,
  25. deleted_at timestamp without time zone
  26. )
  27. `)
  28. await knex.raw(`
  29. CREATE INDEX IF NOT EXISTS index_old_chunks_on_doc_id_and_end_version
  30. ON old_chunks (doc_id, end_version)
  31. `)
  32. await knex.raw(`
  33. CREATE TABLE IF NOT EXISTS pending_chunks (
  34. id SERIAL,
  35. doc_id integer NOT NULL,
  36. end_version integer NOT NULL,
  37. end_timestamp timestamp without time zone,
  38. CONSTRAINT chunks_version_non_negative CHECK (end_version >= 0)
  39. )
  40. `)
  41. await knex.raw(`
  42. CREATE INDEX IF NOT EXISTS index_pending_chunks_on_doc_id_and_id
  43. ON pending_chunks (doc_id, id)
  44. `)
  45. await knex.raw(`
  46. CREATE TABLE IF NOT EXISTS blobs (
  47. hash_bytes bytea NOT NULL PRIMARY KEY,
  48. byte_length integer NOT NULL,
  49. string_length integer,
  50. global boolean,
  51. CONSTRAINT blobs_byte_length_non_negative CHECK (byte_length >= 0),
  52. CONSTRAINT blobs_string_length_non_negative
  53. CHECK (string_length IS NULL OR string_length >= 0)
  54. )
  55. `)
  56. await knex.raw(`
  57. CREATE TABLE IF NOT EXISTS project_blobs (
  58. project_id integer NOT NULL,
  59. hash_bytes bytea NOT NULL,
  60. byte_length integer NOT NULL,
  61. string_length integer,
  62. PRIMARY KEY (project_id, hash_bytes),
  63. CONSTRAINT project_blobs_byte_length_non_negative
  64. CHECK (byte_length >= 0),
  65. CONSTRAINT project_blobs_string_length_non_negative
  66. CHECK (string_length IS NULL OR string_length >= 0)
  67. )
  68. `)
  69. await knex.raw(`CREATE SEQUENCE IF NOT EXISTS docs_id_seq`)
  70. }
  71. exports.down = async function (knex) {
  72. // Don't do anything on the down migration
  73. }