Skip to content

Commit

Permalink
chore: add another migration that remigrates the proper way (#3719)
Browse files Browse the repository at this point in the history
## About the changes
<!-- Describe the changes introduced. What are they and why are they
being introduced? Feel free to also add screenshots or steps to view the
changes if they're visual. -->

Adds a migration that renames `token_name` back to `username`, then adds
a new optional column named `token_name`

## Discussion points
<!-- Anything about the PR you'd like to discuss before it gets merged?
Got any questions or doubts? -->

I've added fallbacks for resolving username/tokenname on insert and on
making rows from results.
But this adds another column renaming, which is worth discussing
properly
  • Loading branch information
daveleek committed May 11, 2023
1 parent 9943179 commit 534e1f1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
6 changes: 4 additions & 2 deletions src/lib/db/api-token-store.ts
Expand Up @@ -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],
Expand 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];
Expand All @@ -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,
Expand Down Expand Up @@ -125,6 +126,7 @@ export class ApiTokenStore implements IApiTokenStore {
)
.select(
'tokens.secret',
'username',
'token_name',
'type',
'expires_at',
Expand Down
20 changes: 0 additions & 20 deletions src/migrations/20230426110026-rename-api-token-username-field.js

This file was deleted.

36 changes: 36 additions & 0 deletions 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,
);
};

0 comments on commit 534e1f1

Please sign in to comment.