-
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: release connection in provider shutdown (#977)
- Loading branch information
1 parent
2dbff86
commit 8b839d3
Showing
2 changed files
with
92 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { test } from '@japa/runner' | ||
import { IgnitorFactory } from '@adonisjs/core/factories' | ||
|
||
import { defineConfig } from '../src/define_config.js' | ||
import { Database } from '../src/database/main.js' | ||
|
||
const BASE_URL = new URL('./tmp/', import.meta.url) | ||
const IMPORTER = (filePath: string) => { | ||
if (filePath.startsWith('./') || filePath.startsWith('../')) { | ||
return import(new URL(filePath, BASE_URL).href) | ||
} | ||
return import(filePath) | ||
} | ||
|
||
test.group('Database Provider', () => { | ||
test('register database provider', async ({ assert }) => { | ||
const ignitor = new IgnitorFactory() | ||
.merge({ | ||
rcFileContents: { | ||
providers: [() => import('../providers/database_provider.js')], | ||
}, | ||
}) | ||
.withCoreConfig() | ||
.withCoreProviders() | ||
.merge({ | ||
config: { | ||
database: defineConfig({ | ||
connection: 'sqlite', | ||
connections: { | ||
sqlite: { | ||
client: 'sqlite', | ||
connection: { filename: new URL('./tmp/database.sqlite', import.meta.url).href }, | ||
migrations: { naturalSort: true, paths: ['database/migrations'] }, | ||
}, | ||
}, | ||
}), | ||
}, | ||
}) | ||
.create(BASE_URL, { importer: IMPORTER }) | ||
|
||
const app = ignitor.createApp('web') | ||
await app.init() | ||
await app.boot() | ||
|
||
assert.instanceOf(await app.container.make('lucid.db'), Database) | ||
}) | ||
|
||
test('release connection when app is terminating', async ({ assert }) => { | ||
const ignitor = new IgnitorFactory() | ||
.merge({ | ||
rcFileContents: { | ||
providers: [() => import('../providers/database_provider.js')], | ||
}, | ||
}) | ||
.withCoreConfig() | ||
.withCoreProviders() | ||
.merge({ | ||
config: { | ||
database: defineConfig({ | ||
connection: 'sqlite', | ||
connections: { | ||
sqlite: { | ||
client: 'sqlite', | ||
connection: { filename: new URL('./tmp/database.sqlite', import.meta.url).href }, | ||
migrations: { naturalSort: true, paths: ['database/migrations'] }, | ||
}, | ||
}, | ||
}), | ||
}, | ||
}) | ||
.create(BASE_URL, { importer: IMPORTER }) | ||
|
||
const app = ignitor.createApp('web') | ||
await app.init() | ||
await app.boot() | ||
|
||
const db = await app.container.make('lucid.db') | ||
|
||
await db.from('users').catch(() => {}) | ||
await app.terminate() | ||
|
||
assert.isFalse(db.manager.isConnected('sqlite')) | ||
}) | ||
}) |