Skip to content

Commit

Permalink
feat: rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
fenos committed Jul 6, 2023
2 parents 4d46e8b + 658d583 commit c70f223
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 29 deletions.
12 changes: 0 additions & 12 deletions migrations/base/02-post-setup.sql

This file was deleted.

25 changes: 14 additions & 11 deletions src/database/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class TenantConnection {
public readonly role: string

protected constructor(
public readonly pool: Knex,
protected readonly pool: Knex,
protected readonly options: TenantConnectionOptions
) {
this.role = options.user.payload.role || 'anon'
Expand All @@ -56,7 +56,7 @@ export class TenantConnection {
let knexPool = connections.get(connectionString)

if (knexPool) {
return new this(await knexPool, options)
return new this(knexPool, options)
}

const isExternalPool = Boolean(options.isExternalPool)
Expand All @@ -67,7 +67,6 @@ export class TenantConnection {
pool: {
min: 0,
max: isExternalPool ? 1 : options.maxConnections || databaseMaxConnections,
propagateCreateError: false,
acquireTimeoutMillis: databaseConnectionTimeout,
idleTimeoutMillis: isExternalPool ? 100 : databaseFreePoolAfterInactivity,
reapIntervalMillis: isExternalPool ? 110 : undefined,
Expand Down Expand Up @@ -115,15 +114,19 @@ export class TenantConnection {
}
}

transaction(instance?: Knex): Knex.TransactionProvider {
return async () => {
const pool = instance || this.pool
const tnx = await pool.transaction()
async transaction(instance?: Knex) {
const pool = instance || this.pool
const tnx = await pool.transaction()

if (!instance) {
await tnx.raw(`set search_path to ${searchPath.join(', ')}`)
}
return tnx
if (!instance && this.options.isExternalPool) {
await tnx.raw(`SELECT set_config('search_path', ?, true)`, [searchPath.join(', ')])
}
return tnx
}

transactionProvider(instance?: Knex): Knex.TransactionProvider {
return async () => {
return this.transaction(instance)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/storage/database/knex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class StorageKnexDB implements Database {
transactionOptions?: TransactionOptions
) {
try {
const tnx = await this.connection.transaction(this.options.tnx)()
const tnx = await this.connection.transactionProvider(this.options.tnx)()

try {
await this.connection.setScope(tnx)
Expand Down Expand Up @@ -546,7 +546,7 @@ export class StorageKnexDB implements Database {
const needsNewTransaction = !tnx || differentScopes

if (!tnx || needsNewTransaction) {
tnx = await this.connection.transaction(this.options.tnx)()
tnx = await this.connection.transactionProvider(this.options.tnx)()
tnx.on('query-error', (error: DatabaseError) => {
throw DBError.fromDBError(error)
})
Expand Down
11 changes: 9 additions & 2 deletions src/test/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import { StorageBackendError } from '../storage'
import { useMockObject, useMockQueue } from './common'
import { getPostgresConnection } from '../database'
import { getServiceKeyUser } from '../database/tenant'
import { Knex } from 'knex'

dotenv.config({ path: '.env.test' })

const { anonKey, jwtSecret, serviceKey, tenantId } = getConfig()

let tnx: Knex.Transaction | undefined
async function getSuperuserPostgrestClient() {
const superUser = await getServiceKeyUser(tenantId)

Expand All @@ -26,14 +28,19 @@ async function getSuperuserPostgrestClient() {
tenantId,
host: 'localhost',
})
const tnx = await conn.pool

tnx = await conn.transaction()
return tnx
}

useMockObject()
useMockQueue()

afterEach(async () => {
if (tnx) {
await tnx.commit()
}
})

/*
* GET /object/:id
*/
Expand Down
2 changes: 1 addition & 1 deletion src/test/rls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe('RLS policies', () => {

afterAll(async () => {
await db.destroy()
await (storage.db as StorageKnexDB).connection.pool.destroy()
await (storage.db as StorageKnexDB).connection.dispose()
})

testSpec.tests.forEach((_test, index) => {
Expand Down
4 changes: 3 additions & 1 deletion src/test/webhooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ describe('Webhooks', () => {

async function createObject(pg: TenantConnection, bucketId: string) {
const objectName = Date.now()
const tnx = pg.pool
const tnx = await pg.transaction()

const [data] = await tnx
.from<Obj>('objects')
Expand All @@ -316,5 +316,7 @@ async function createObject(pg: TenantConnection, bucketId: string) {
])
.returning('*')

await tnx.commit()

return data as Obj
}

0 comments on commit c70f223

Please sign in to comment.