Skip to content

Commit

Permalink
migration done
Browse files Browse the repository at this point in the history
  • Loading branch information
farzaneh-haghani committed May 30, 2024
1 parent 98b1be9 commit 5eb8f5e
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions migrations/1717025690146_change-draft-to-status-in-resource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @type {import('node-pg-migrate').ColumnDefinitions | undefined}
*/
exports.shorthands = undefined;

/**
* @param pgm {import('node-pg-migrate').MigrationBuilder}
* @param run {() => void | undefined}
* @returns {Promise<void> | void}
*/
exports.up = (pgm) => {
pgm.renameColumn("resources", "draft", "status");
pgm.renameColumn("resources", "publication", "status_changed_date");
pgm.renameColumn("resources", "publisher", "status_changed_by");

pgm.createType("enum", ["drafted", "published", "rejected"]);
pgm.sql("ALTER TABLE resources ALTER COLUMN status DROP DEFAULT");
pgm.alterColumn("resources", "status", {
type: "enum",
using:
"CASE WHEN status = 'true' THEN 'drafted'::enum WHEN status = 'false' THEN 'published'::enum ELSE 'rejected'::enum END",
});
};

/**
* @param pgm {import('node-pg-migrate').MigrationBuilder}
* @param run {() => void | undefined}
* @returns {Promise<void> | void}
*/
exports.down = (pgm) => {
pgm.renameColumn("resources", "status", "draft");
pgm.renameColumn("resources", "status_changed_date", "publication");
pgm.renameColumn("resources", "status_changed_by", "publisher");

pgm.alterColumn("resources", "status", {
type: "boolean",
using:
"CASE WHEN status = 'drafted' THEN true WHEN status = 'published' THEN false ELSE null END",
});

pgm.dropType("enum");
};

0 comments on commit 5eb8f5e

Please sign in to comment.