diff --git a/db/migrations/1690296539500_add-user-constraints.js b/db/migrations/1690296539500_add-user-constraints.js new file mode 100644 index 00000000..d95d1862 --- /dev/null +++ b/db/migrations/1690296539500_add-user-constraints.js @@ -0,0 +1,40 @@ +/* eslint-disable no-unused-vars */ + +/** + * Defines changes to be made to the database schema to accommodate new functionality. + * + * See {@link https://salsita.github.io/node-pg-migrate/#/migrations?id=defining-migrations the docs}. + * + * @param {MigrationBuilder} pgm + * @returns {void | Promise} + */ +exports.up = (pgm) => { + pgm.alterColumn("users", "name", { notNull: true }); + pgm.alterColumn("users", "github_id", { notNull: true }); + pgm.addConstraint("users", "unique_github_ids", { unique: ["github_id"] }); +}; + +/** + * Reverses the "up" migration to return the database to its initial state. + * + * @param {MigrationBuilder} pgm + * @returns {void | Promise} + */ +exports.down = (pgm) => { + pgm.dropConstraint("users", "unique_github_ids"); + pgm.alterColumn("users", "github_id", { notNull: false }); + pgm.alterColumn("users", "name", { notNull: false }); +}; + +/** + * Create new shorthand column definitions for this and any future migrations. + * + * @type {ColumnDefinitions | undefined} + */ +exports.shorthands = undefined; + +/** + * Importing library types for less verbose type hints. + * @typedef {import("node-pg-migrate").MigrationBuilder} MigrationBuilder + * @typedef {import("node-pg-migrate").ColumnDefinitions} ColumnDefinitions + */