Skip to content

Commit c70f223

Browse files
committed
feat: rebase
2 parents 4d46e8b + 658d583 commit c70f223

File tree

6 files changed

+29
-29
lines changed

6 files changed

+29
-29
lines changed

migrations/base/02-post-setup.sql

-12
This file was deleted.

src/database/connection.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class TenantConnection {
3535
public readonly role: string
3636

3737
protected constructor(
38-
public readonly pool: Knex,
38+
protected readonly pool: Knex,
3939
protected readonly options: TenantConnectionOptions
4040
) {
4141
this.role = options.user.payload.role || 'anon'
@@ -56,7 +56,7 @@ export class TenantConnection {
5656
let knexPool = connections.get(connectionString)
5757

5858
if (knexPool) {
59-
return new this(await knexPool, options)
59+
return new this(knexPool, options)
6060
}
6161

6262
const isExternalPool = Boolean(options.isExternalPool)
@@ -67,7 +67,6 @@ export class TenantConnection {
6767
pool: {
6868
min: 0,
6969
max: isExternalPool ? 1 : options.maxConnections || databaseMaxConnections,
70-
propagateCreateError: false,
7170
acquireTimeoutMillis: databaseConnectionTimeout,
7271
idleTimeoutMillis: isExternalPool ? 100 : databaseFreePoolAfterInactivity,
7372
reapIntervalMillis: isExternalPool ? 110 : undefined,
@@ -115,15 +114,19 @@ export class TenantConnection {
115114
}
116115
}
117116

118-
transaction(instance?: Knex): Knex.TransactionProvider {
119-
return async () => {
120-
const pool = instance || this.pool
121-
const tnx = await pool.transaction()
117+
async transaction(instance?: Knex) {
118+
const pool = instance || this.pool
119+
const tnx = await pool.transaction()
122120

123-
if (!instance) {
124-
await tnx.raw(`set search_path to ${searchPath.join(', ')}`)
125-
}
126-
return tnx
121+
if (!instance && this.options.isExternalPool) {
122+
await tnx.raw(`SELECT set_config('search_path', ?, true)`, [searchPath.join(', ')])
123+
}
124+
return tnx
125+
}
126+
127+
transactionProvider(instance?: Knex): Knex.TransactionProvider {
128+
return async () => {
129+
return this.transaction(instance)
127130
}
128131
}
129132

src/storage/database/knex.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class StorageKnexDB implements Database {
3939
transactionOptions?: TransactionOptions
4040
) {
4141
try {
42-
const tnx = await this.connection.transaction(this.options.tnx)()
42+
const tnx = await this.connection.transactionProvider(this.options.tnx)()
4343

4444
try {
4545
await this.connection.setScope(tnx)
@@ -546,7 +546,7 @@ export class StorageKnexDB implements Database {
546546
const needsNewTransaction = !tnx || differentScopes
547547

548548
if (!tnx || needsNewTransaction) {
549-
tnx = await this.connection.transaction(this.options.tnx)()
549+
tnx = await this.connection.transactionProvider(this.options.tnx)()
550550
tnx.on('query-error', (error: DatabaseError) => {
551551
throw DBError.fromDBError(error)
552552
})

src/test/object.test.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import { StorageBackendError } from '../storage'
1212
import { useMockObject, useMockQueue } from './common'
1313
import { getPostgresConnection } from '../database'
1414
import { getServiceKeyUser } from '../database/tenant'
15+
import { Knex } from 'knex'
1516

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

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

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

@@ -26,14 +28,19 @@ async function getSuperuserPostgrestClient() {
2628
tenantId,
2729
host: 'localhost',
2830
})
29-
const tnx = await conn.pool
30-
31+
tnx = await conn.transaction()
3132
return tnx
3233
}
3334

3435
useMockObject()
3536
useMockQueue()
3637

38+
afterEach(async () => {
39+
if (tnx) {
40+
await tnx.commit()
41+
}
42+
})
43+
3744
/*
3845
* GET /object/:id
3946
*/

src/test/rls.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ describe('RLS policies', () => {
127127

128128
afterAll(async () => {
129129
await db.destroy()
130-
await (storage.db as StorageKnexDB).connection.pool.destroy()
130+
await (storage.db as StorageKnexDB).connection.dispose()
131131
})
132132

133133
testSpec.tests.forEach((_test, index) => {

src/test/webhooks.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ describe('Webhooks', () => {
294294

295295
async function createObject(pg: TenantConnection, bucketId: string) {
296296
const objectName = Date.now()
297-
const tnx = pg.pool
297+
const tnx = await pg.transaction()
298298

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

319+
await tnx.commit()
320+
319321
return data as Obj
320322
}

0 commit comments

Comments
 (0)