diff --git a/packages/server/migrations/20230915145941_grants-data-type-migration-add-temp-columns.js b/packages/server/migrations/20230915145941_grants-data-type-migration-add-temp-columns.js new file mode 100644 index 000000000..67e7462e7 --- /dev/null +++ b/packages/server/migrations/20230915145941_grants-data-type-migration-add-temp-columns.js @@ -0,0 +1,29 @@ +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.up = function (knex) { + return knex.raw(` + ALTER TABLE grants + ADD COLUMN award_ceiling_temp bigint, + ADD COLUMN award_floor_temp bigint, + ADD COLUMN open_date_temp date, + ADD COLUMN close_date_temp date, + ADD COLUMN archive_date date + `); +}; + +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.down = function (knex) { + return knex.raw(` + ALTER TABLE grants + DROP COLUMN award_ceiling_temp, + DROP COLUMN award_floor_temp, + DROP COlUMN open_date_temp, + DROP COLUMN close_date_temp, + DROP COLUMN archive_date + `); +}; diff --git a/packages/server/migrations/20230915145942_grants-data-type-migration-move-data-to-temp-columns.js b/packages/server/migrations/20230915145942_grants-data-type-migration-move-data-to-temp-columns.js new file mode 100644 index 000000000..821d71dcd --- /dev/null +++ b/packages/server/migrations/20230915145942_grants-data-type-migration-move-data-to-temp-columns.js @@ -0,0 +1,27 @@ +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.up = function (knex) { + return knex.raw(` + UPDATE grants + SET award_ceiling_temp = award_ceiling::bigint, + award_floor_temp = award_floor::bigint, + open_date_temp = open_date::date, + close_date_temp = close_date::date + `); +}; + +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.down = function (knex) { + return knex.raw(` + UPDATE grants + SET award_ceiling_temp = NULL, + award_floor_temp = NULL, + open_date_temp = NULL, + close_date_temp = NULL + `); +}; diff --git a/packages/server/migrations/20230915150001_grants-data-type-migration-rename-columns.js b/packages/server/migrations/20230915150001_grants-data-type-migration-rename-columns.js new file mode 100644 index 000000000..1d254543f --- /dev/null +++ b/packages/server/migrations/20230915150001_grants-data-type-migration-rename-columns.js @@ -0,0 +1,39 @@ +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.up = function (knex) { + const queries = [ + knex.raw(`ALTER TABLE grants RENAME COLUMN award_ceiling TO award_ceiling_old`), + knex.raw(`ALTER TABLE grants RENAME COLUMN award_floor TO award_floor_old`), + knex.raw(`ALTER TABLE grants RENAME COLUMN open_date TO open_date_old`), + knex.raw(`ALTER TABLE grants RENAME COLUMN close_date TO close_date_old`), + knex.raw(`ALTER TABLE grants RENAME COLUMN award_ceiling_temp TO award_ceiling`), + knex.raw(`ALTER TABLE grants RENAME COLUMN award_floor_temp TO award_floor`), + knex.raw(`ALTER TABLE grants RENAME COLUMN open_date_temp TO open_date`), + knex.raw(`ALTER TABLE grants RENAME COLUMN close_date_temp TO close_date`), + ]; + console.log(queries.join(';')); + const multiQuery = queries.join(';'); + return knex.raw(multiQuery); +}; + +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.down = function (knex) { + const queries = [ + knex.raw(`ALTER TABLE grants RENAME COLUMN award_ceiling TO award_ceiling_temp`), + knex.raw(`ALTER TABLE grants RENAME COLUMN award_floor TO award_floor_temp`), + knex.raw(`ALTER TABLE grants RENAME COLUMN open_date TO open_date_temp`), + knex.raw(`ALTER TABLE grants RENAME COLUMN close_date TO close_date_temp`), + knex.raw(`ALTER TABLE grants RENAME COLUMN award_ceiling_old TO award_ceiling`), + knex.raw(`ALTER TABLE grants RENAME COLUMN award_floor_old TO award_floor`), + knex.raw(`ALTER TABLE grants RENAME COLUMN open_date_old TO open_date`), + knex.raw(`ALTER TABLE grants RENAME COLUMN close_date_old TO close_date`), + ]; + console.log(queries.join(';')); + const multiQuery = queries.join(';'); + return knex.raw(multiQuery); +}; diff --git a/packages/server/migrations/20230915150014_grants-data-type-migration-drop-old-columns.js b/packages/server/migrations/20230915150014_grants-data-type-migration-drop-old-columns.js new file mode 100644 index 000000000..d5f53fd81 --- /dev/null +++ b/packages/server/migrations/20230915150014_grants-data-type-migration-drop-old-columns.js @@ -0,0 +1,27 @@ +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.up = function (knex) { + return knex.raw(` + ALTER TABLE grants + DROP COLUMN award_ceiling_old, + DROP COLUMN award_floor_old, + DROP COLUMN open_date_old, + DROP COLUMN close_date_old + `); +}; + +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.down = function (knex) { + return knex.raw(` + ALTER TABLE grants + ADD COLUMN award_ceiling_old text, + ADD COLUMN award_floor_old text, + ADD COLUMN open_date_old text, + ADD COLUMN close_date_old text + `); +}; diff --git a/packages/server/package.json b/packages/server/package.json index 55f554eaa..3eb5ae83c 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -23,6 +23,7 @@ "import-arpa-dump": "node src/scripts/import_arpa_reporter_dump.js", "lint": "eslint '**/*.js'", "migrate:make": "knex migrate:make", + "migrate:up": "knex migrate:up", "parse-output-templates": "node src/scripts/parseOutputTemplates.mjs", "pre-commit": "yarn lint", "serve": "nodemon src",