Skip to content

Commit ea4652f

Browse files
chore: wip
1 parent 98d0655 commit ea4652f

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { Database } from '@stacksjs/database'
2+
import { sql } from '@stacksjs/database'
3+
4+
export async function up(db: Database<any>) {
5+
await db.schema
6+
.createTable('jobs')
7+
.addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
8+
.addColumn('queue', 'varchar(255)', col => col.notNull())
9+
.addColumn('payload', 'text', col => col.notNull())
10+
.addColumn('attempts', 'integer', col => col.notNull().defaultTo(0))
11+
.addColumn('reserved_at', 'timestamp')
12+
.addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))
13+
.addColumn('updated_at', 'timestamp')
14+
.execute()
15+
}

storage/framework/core/database/src/custom/jobs.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { log } from '@stacksjs/cli'
33
import { database } from '@stacksjs/config'
44
import { err, handleError, ok, type Result } from '@stacksjs/error-handling'
55
import { path } from '@stacksjs/path'
6-
import { hasTableBeenMigrated } from '../drivers'
6+
import { hasMigrationBeenCreated } from '../drivers'
77

88
const driver = database.default || ''
99

1010
export async function createJobsMigration(): Promise<Result<MigrationResult[] | string, Error>> {
1111
try {
1212
if (['sqlite', 'mysql'].includes(driver)) {
13-
const hasBeenMigrated = await hasTableBeenMigrated('jobs')
13+
const hasBeenMigrated = await hasMigrationBeenCreated('jobs')
1414

1515
if (!hasBeenMigrated) {
1616
let migrationContent = `import type { Database } from '@stacksjs/database'\nimport { sql } from '@stacksjs/database'\n\n`
@@ -35,6 +35,8 @@ export async function createJobsMigration(): Promise<Result<MigrationResult[] |
3535
await Bun.write(migrationFilePath, migrationContent) // Ensure the write operation is awaited
3636

3737
log.success('Created jobs migration')
38+
} else {
39+
log.success('Jobs migration already created')
3840
}
3941
}
4042

storage/framework/core/database/src/drivers/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ export async function hasTableBeenMigrated(tableName: string): Promise<boolean>
4444
return results.some(migration => migration.name.includes(tableName))
4545
}
4646

47+
export async function hasMigrationBeenCreated(tableName: string): Promise<boolean> {
48+
log.debug(`hasTableBeenMigrated for table: ${tableName}`)
49+
50+
const migrations = globSync([path.userMigrationsPath('*.ts')], { absolute: true })
51+
52+
return migrations.some(path => path.includes("create-jobs-"))
53+
}
54+
4755
export async function getExecutedMigrations(): Promise<{ name: string }[]> {
4856
try {
4957
return await db.selectFrom('migrations').select('name').execute()

0 commit comments

Comments
 (0)