From af7e14fa6f4645cbf4ec3ec22659d0341c34b36d Mon Sep 17 00:00:00 2001 From: Thuc Tran Date: Sun, 30 Jun 2024 22:21:52 -0400 Subject: [PATCH] Issue #3200 Add grant_notes and grant_notes_revisions table migration (#3209) * Create migration to add grant_notes and grant_notes_revisions tables * Create migration to add grant_notes and grant_notes_revisions tables * Add useConstraint modifier to the creation of the unique index. Knex documentation says for the unique() function, that useConstraint will use a unique constraint and is default true for postgres when there is no predicate defined. Issue #3200 specifies that we want a unique index. Documentation can be found here: https://knexjs.org/guide/schema-builder.html#unique * Lint --- .../20240622205204_add-grant-notes-table.js | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 packages/server/migrations/20240622205204_add-grant-notes-table.js diff --git a/packages/server/migrations/20240622205204_add-grant-notes-table.js b/packages/server/migrations/20240622205204_add-grant-notes-table.js new file mode 100644 index 000000000..df30dc9ae --- /dev/null +++ b/packages/server/migrations/20240622205204_add-grant-notes-table.js @@ -0,0 +1,37 @@ +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.up = function (knex) { + return knex.schema + .createTable('grant_notes', (table) => { + table.increments('id').notNullable().primary(); + table.text('grant_id').notNullable(); + table.integer('user_id').notNullable(); + table.timestamp('created_at', { useTz: true }).notNullable().defaultTo(knex.fn.now()); + + table.foreign('grant_id').references('grants.grant_id'); + table.foreign('user_id').references('users.id'); + + table.unique(['grant_id', 'user_id'], { + indexName: 'idx_grant_id_user_id', + useConstraint: false, + }); + }) + .createTable('grant_notes_revisions', (table) => { + table.increments('id').notNullable().primary(); + table.integer('grant_note_id').notNullable(); + table.timestamp('created_at', { useTz: true }).notNullable().defaultTo(knex.fn.now()); + table.text('text').notNullable(); + table.foreign('grant_note_id').references('grant_notes.id'); + }); +}; + +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.down = function (knex) { + return knex.schema.dropTable('grant_notes') + .dropTable('grant_notes_revisions'); +};