diff --git a/src/lib/db/api-token-store.ts b/src/lib/db/api-token-store.ts index d23ccebe5a..5943c2ddc2 100644 --- a/src/lib/db/api-token-store.ts +++ b/src/lib/db/api-token-store.ts @@ -39,7 +39,7 @@ const tokenRowReducer = (acc, tokenRow) => { if (!acc[tokenRow.secret]) { acc[tokenRow.secret] = { secret: token.secret, - tokenName: token.token_name, + tokenName: token.token_name ? token.token_name : token.username, type: token.type.toLowerCase(), project: ALL, projects: [ALL], @@ -48,7 +48,7 @@ const tokenRowReducer = (acc, tokenRow) => { createdAt: token.created_at, alias: token.alias, seenAt: token.seen_at, - username: token.token_name, + username: token.token_name ? token.token_name : token.username, }; } const currentToken = acc[tokenRow.secret]; @@ -63,6 +63,7 @@ const tokenRowReducer = (acc, tokenRow) => { }; const toRow = (newToken: IApiTokenCreate) => ({ + username: newToken.tokenName ?? newToken.username, token_name: newToken.tokenName ?? newToken.username, secret: newToken.secret, type: newToken.type, @@ -125,6 +126,7 @@ export class ApiTokenStore implements IApiTokenStore { ) .select( 'tokens.secret', + 'username', 'token_name', 'type', 'expires_at', diff --git a/src/migrations/20230426110026-rename-api-token-username-field.js b/src/migrations/20230426110026-rename-api-token-username-field.js deleted file mode 100644 index 51e15848a0..0000000000 --- a/src/migrations/20230426110026-rename-api-token-username-field.js +++ /dev/null @@ -1,20 +0,0 @@ -/* eslint camelcase: "off" */ -'use strict'; - -exports.up = function (db, cb) { - db.runSql( - ` - ALTER TABLE api_tokens RENAME COLUMN username TO token_name; - `, - cb, - ); -}; - -exports.down = function (db, cb) { - db.runSql( - ` - ALTER TABLE api_tokens RENAME COLUMN token_name TO username; - `, - cb, - ); -}; diff --git a/src/migrations/20230510113903-fix-api-token-username-migration.js b/src/migrations/20230510113903-fix-api-token-username-migration.js new file mode 100644 index 0000000000..46c7d94eaf --- /dev/null +++ b/src/migrations/20230510113903-fix-api-token-username-migration.js @@ -0,0 +1,36 @@ +'use strict'; + +exports.up = function (db, callback) { + db.runSql( + ` + SELECT * FROM migrations WHERE name = '/20230426110026-rename-api-token-username-field'; + `, + (err, results) => { + if (results.rows.length > 0) { + db.runSql( + ` + ALTER TABLE api_tokens RENAME COLUMN token_name TO username; + ALTER TABLE api_tokens ADD COLUMN "token_name" text; + UPDATE api_tokens SET token_name = username; + DELETE FROM migrations WHERE name = '/20230426110026-rename-api-token-username-field'; + `, + ); + } else { + db.runSql( + ` + ALTER TABLE api_tokens ADD COLUMN "token_name" text; + UPDATE api_tokens SET token_name = username; + `, + ); + } + callback(); + }, + ); +}; + +exports.down = function (db, callback) { + db.runSql( + `ALTER TABLE api_tokens DROP COLUMN IF EXISTS "token_name";`, + callback, + ); +};