-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
migrate token pooling to postgres #8922
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
103 changes: 103 additions & 0 deletions
103
core/token-pooling/sql-token-persistence.integration.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,103 @@ | ||
import pg from 'pg' | ||
import { expect } from 'chai' | ||
import configModule from 'config' | ||
import SqlTokenPersistence from './sql-token-persistence.js' | ||
|
||
const config = configModule.util.toObject() | ||
const postgresUrl = config?.private?.postgres_url | ||
const tableName = 'token_persistence_integration_test' | ||
|
||
describe('SQL token persistence', function () { | ||
let pool | ||
let persistence | ||
|
||
before('Mock db connection and load app', async function () { | ||
// Create a new pool with a connection limit of 1 | ||
pool = new pg.Pool({ | ||
connectionString: postgresUrl, | ||
|
||
// Reuse the connection to make sure we always hit the same pg_temp schema | ||
max: 1, | ||
|
||
// Disable auto-disconnection of idle clients to make sure we always hit the same pg_temp schema | ||
idleTimeoutMillis: 0, | ||
}) | ||
persistence = new SqlTokenPersistence({ | ||
url: postgresUrl, | ||
table: tableName, | ||
}) | ||
}) | ||
after(async function () { | ||
if (persistence) { | ||
await persistence.stop() | ||
persistence = undefined | ||
} | ||
}) | ||
|
||
beforeEach('Create temporary table', async function () { | ||
await pool.query( | ||
`CREATE TEMPORARY TABLE ${tableName} (LIKE github_user_tokens INCLUDING ALL);` | ||
) | ||
}) | ||
afterEach('Drop temporary table', async function () { | ||
await pool.query(`DROP TABLE IF EXISTS pg_temp.${tableName};`) | ||
}) | ||
|
||
context('when the key does not exist', function () { | ||
it('does nothing', async function () { | ||
const tokens = await persistence.initialize(pool) | ||
expect(tokens).to.deep.equal([]) | ||
}) | ||
}) | ||
|
||
context('when the key exists', function () { | ||
const initialTokens = ['a', 'b', 'c'].map(char => char.repeat(40)) | ||
|
||
beforeEach(async function () { | ||
initialTokens.forEach(async token => { | ||
await pool.query( | ||
`INSERT INTO pg_temp.${tableName} (token) VALUES ($1::text);`, | ||
[token] | ||
) | ||
}) | ||
}) | ||
chris48s marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
it('loads the contents', async function () { | ||
const tokens = await persistence.initialize(pool) | ||
expect(tokens.sort()).to.deep.equal(initialTokens) | ||
}) | ||
|
||
context('when tokens are added', function () { | ||
it('saves the change', async function () { | ||
const newToken = 'e'.repeat(40) | ||
const expected = initialTokens.slice() | ||
expected.push(newToken) | ||
|
||
await persistence.initialize(pool) | ||
await persistence.noteTokenAdded(newToken) | ||
|
||
const result = await pool.query( | ||
`SELECT token FROM pg_temp.${tableName};` | ||
) | ||
const savedTokens = result.rows.map(row => row.token) | ||
expect(savedTokens.sort()).to.deep.equal(expected) | ||
}) | ||
}) | ||
|
||
context('when tokens are removed', function () { | ||
it('saves the change', async function () { | ||
const expected = Array.from(initialTokens) | ||
const toRemove = expected.pop() | ||
|
||
await persistence.initialize(pool) | ||
await persistence.noteTokenRemoved(toRemove) | ||
|
||
const result = await pool.query( | ||
`SELECT token FROM pg_temp.${tableName};` | ||
) | ||
const savedTokens = result.rows.map(row => row.token) | ||
expect(savedTokens.sort()).to.deep.equal(expected) | ||
chris48s marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
}) | ||
}) | ||
}) |
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,55 @@ | ||
import pg from 'pg' | ||
import log from '../server/log.js' | ||
|
||
export default class SqlTokenPersistence { | ||
constructor({ url, table }) { | ||
this.url = url | ||
this.table = table | ||
this.noteTokenAdded = this.noteTokenAdded.bind(this) | ||
this.noteTokenRemoved = this.noteTokenRemoved.bind(this) | ||
} | ||
|
||
async initialize(pool) { | ||
if (pool) { | ||
this.pool = pool | ||
} else { | ||
this.pool = new pg.Pool({ connectionString: this.url }) | ||
} | ||
const result = await this.pool.query(`SELECT token FROM ${this.table};`) | ||
return result.rows.map(row => row.token) | ||
} | ||
|
||
async stop() { | ||
await this.pool.end() | ||
} | ||
|
||
async onTokenAdded(token) { | ||
return await this.pool.query( | ||
`INSERT INTO ${this.table} (token) VALUES ($1::text) ON CONFLICT (token) DO NOTHING;`, | ||
chris48s marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[token] | ||
) | ||
} | ||
|
||
async onTokenRemoved(token) { | ||
return await this.pool.query( | ||
`DELETE FROM ${this.table} WHERE token=$1::text;`, | ||
[token] | ||
) | ||
} | ||
|
||
async noteTokenAdded(token) { | ||
try { | ||
await this.onTokenAdded(token) | ||
} catch (e) { | ||
log.error(e) | ||
} | ||
} | ||
|
||
async noteTokenRemoved(token) { | ||
try { | ||
await this.onTokenRemoved(token) | ||
} catch (e) { | ||
log.error(e) | ||
} | ||
} | ||
} |
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,14 @@ | ||
/* eslint-disable camelcase */ | ||
chris48s marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
exports.shorthands = undefined | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
exports.up = pgm => { | ||
pgm.createTable('github_user_tokens', { | ||
id: 'id', | ||
token: { type: 'varchar(1000)', notNull: true, unique: true }, | ||
}) | ||
} | ||
|
||
exports.down = pgm => { | ||
pgm.dropTable('github_user_tokens') | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This copies our table to a temp schema for the purposes of running the tests then clears it up again after. This means we can run the tests against a local DB with real data in it and not break our local data. We don't need a separate test DB. It also means the tests will keep step with schema changes from the migrations. We do need to
migrate up
before running the tests though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No objections to this in the local/inner dev loop (nor CI) context, and agree the benefits outweigh the tradeoffs. Would be nervous abut this in the context of the real DB though (whether connected to deliberately or accidentally)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In principle it should be safe but I agree - I wouldn't want to run the tests while connected to the prod DB. Tbh that would be fairly unusual thing to do for any application. I think the fact that the dev dependencies (e.g: mocha, etc) aren't installed in the prod container is enough of a guard rail to stop this from happening.
Personally I'd tend to avoid connecting my dev environment up to a prod database for any application.