Skip to content

Commit

Permalink
fix(action): database
Browse files Browse the repository at this point in the history
  • Loading branch information
batosai committed Jan 16, 2025
1 parent e1db4a5 commit 9424c4e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
3 changes: 2 additions & 1 deletion bin/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assert } from '@japa/assert'
import { expectTypeOf } from '@japa/expect-type'
import { processCLIArgs, configure, run } from '@japa/runner'
import { createApp } from '../tests/helpers/app.js'
import { createApp, initializeDatabase } from '../tests/helpers/app.js'
import { fileSystem } from '@japa/file-system'
import app from '@adonisjs/core/services/app'
import { ApplicationService } from '@adonisjs/core/types'
Expand All @@ -15,6 +15,7 @@ configure({
setup: [
async () => {
testApp = await createApp()
await initializeDatabase(testApp)
},
],
teardown: [
Expand Down
23 changes: 23 additions & 0 deletions tests/fixtures/migrations/create_users_table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @jrmc/adonis-attachment
*
* @license MIT
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
*/
import { BaseSchema } from '@adonisjs/lucid/schema'
export default class extends BaseSchema {
protected tableName = 'users'
async up() {
this.schema.createTable(this.tableName, (table) => {
table.increments('id')
table.string('name')
table.json('avatar')
table.json('avatar_2')
table.timestamp('created_at')
table.timestamp('updated_at')
})
}
async down() {
this.schema.dropTable(this.tableName)
}
}
24 changes: 17 additions & 7 deletions tests/helpers/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
* @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
*/

import type { ApplicationService } from '@adonisjs/core/types'
import type { InferConverters } from '../../src/types/config.js'

import { copyFile, mkdir } from 'node:fs/promises'
import { IgnitorFactory } from '@adonisjs/core/factories'
import { defineConfig as defineLucidConfig } from '@adonisjs/lucid'
import { defineConfig } from '../../src/define_config.js'
import { defineConfig as defineDriveConfig, services } from '@adonisjs/drive'

import { BASE_URL } from './index.js'
import type { InferConverters } from '../../src/types/config.js'

const IMPORTER = (filePath: string) => {
if (filePath.startsWith('./') || filePath.startsWith('../')) {
Expand Down Expand Up @@ -83,7 +86,7 @@ export async function createApp(options = {}) {
sqlite: {
client: 'better-sqlite3',
connection: {
filename: new URL('./db.sqlite', BASE_URL).pathname,
filename: new URL('../db.sqlite', BASE_URL).pathname,
},
},
},
Expand All @@ -100,14 +103,21 @@ export async function createApp(options = {}) {
await app.init()
await app.boot()

await mkdir(app.migrationsPath(), { recursive: true })

await copyFile(
new URL('../fixtures/migrations/create_users_table.ts', import.meta.url),
app.migrationsPath('create_users_table.ts')
)

return app
}

// export async function initializeDatabase(app: ApplicationService) {
// const ace = await app.container.make('ace')
// await ace.exec('migration:fresh', [])
// await seedDatabase()
// }
export async function initializeDatabase(app: ApplicationService) {
const ace = await app.container.make('ace')
await ace.exec('migration:fresh', [])
// await seedDatabase()
}

// async function seedDatabase() {
// const { default: User } = await import('./fixtures/models/user.js')
Expand Down

0 comments on commit 9424c4e

Please sign in to comment.