-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: fix grant continuation fetch * fix: move finalizationReason addition to separate migration * chore: lint * feat: check for column existence before migrating * feat: update migration to use enum update
- Loading branch information
Showing
3 changed files
with
106 additions
and
3 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
packages/auth/migrations/20230925183132_add_finalization_reason_grants.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const grantsTable = 'grants' | ||
const finalizationReasonColumn = 'finalizationReason' | ||
const finalzationReasonType = 'finalization_reason_type' | ||
const finalizationReasonList = ['ISSUED', 'REVOKED', 'REJECTED'] | ||
|
||
const toPsqlList = (list) => `(${list.map((el) => `'${el}'`).join(', ')})` | ||
|
||
/** | ||
* @param { import("knex").Knex } knex | ||
* @returns { Promise<void> } | ||
*/ | ||
exports.up = function (knex) { | ||
return knex.schema | ||
.hasColumn(grantsTable, finalizationReasonColumn) | ||
.then((exists) => { | ||
if (!exists) { | ||
return knex.schema.alterTable(grantsTable, (table) => { | ||
table.enum(finalizationReasonColumn, finalizationReasonList, { | ||
useNative: true, | ||
enumName: finalzationReasonType | ||
}) | ||
}) | ||
} | ||
|
||
return knex.schema.raw( | ||
`CREATE TYPE ${finalzationReasonType} AS ENUM ${toPsqlList( | ||
finalizationReasonList | ||
)}; | ||
ALTER TABLE "${grantsTable}" ALTER COLUMN "${finalizationReasonColumn}" TYPE ${finalzationReasonType} USING "${finalizationReasonColumn}"::${finalzationReasonType}` | ||
) | ||
}) | ||
} | ||
|
||
/** | ||
* @param { import("knex").Knex } knex | ||
* @returns { Promise<void> } | ||
*/ | ||
exports.down = function (knex) { | ||
return knex.schema | ||
.hasColumn(grantsTable, finalizationReasonColumn) | ||
.then((exists) => { | ||
if (exists) { | ||
return knex.schema | ||
.alterTable(grantsTable, (table) => { | ||
table.dropColumn(finalizationReasonColumn) | ||
}) | ||
.then(() => { | ||
return knex.schema.raw( | ||
`DROP TYPE IF EXISTS ${finalzationReasonType}` | ||
) | ||
}) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters