Skip to content

Commit 2d84d66

Browse files
chore: wip
1 parent 890d466 commit 2d84d66

File tree

12 files changed

+146
-56
lines changed

12 files changed

+146
-56
lines changed

storage/framework/core/actions/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export {
2424
makeLanguage,
2525
makeNotification,
2626
makePage,
27+
makeQueueTable,
2728
makeStack,
2829
make as runMake,
2930
} from './make'

storage/framework/core/actions/src/make.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import type { MakeOptions } from '@stacksjs/types'
22
import process from 'node:process'
33
import { italic } from '@stacksjs/cli'
4+
import { Action } from '@stacksjs/enums'
45
import { handleError } from '@stacksjs/error-handling'
56
import { log } from '@stacksjs/logging'
67
import { frameworkPath, path as p, resolve } from '@stacksjs/path'
78
import { createFolder, doesFolderExist, writeTextFile } from '@stacksjs/storage'
9+
import { runAction } from './helpers'
810

911
export async function invoke(options: MakeOptions): Promise<void> {
1012
if (options.component)
@@ -309,6 +311,10 @@ export async function up(db: Kysely<any>): Promise<void> {
309311
}
310312
}
311313

314+
export async function makeQueueTable(): Promise<void> {
315+
await runAction(Action.QueueTable)
316+
}
317+
312318
export async function createModel(options: MakeOptions): Promise<void> {
313319
const optionName = options.name
314320

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import process from 'node:process'
2+
import { createJobsMigration } from '@stacksjs/database'
3+
import { log } from '@stacksjs/logging'
4+
5+
// first, reset the database, if it exists
6+
const result = await createJobsMigration()
7+
8+
if (result?.isErr()) {
9+
console.error(result.error)
10+
log.error('generateMigrations failed', result.error)
11+
process.exit(1)
12+
}
13+
14+
process.exit(0)

storage/framework/core/buddy/src/commands/make.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
makeFunction,
1111
makeLanguage,
1212
makePage,
13+
makeQueueTable,
1314
makeStack,
1415
} from '@stacksjs/actions'
1516
import { intro, italic, outro, runCommand } from '@stacksjs/cli'
@@ -31,6 +32,7 @@ export function make(buddy: CLI): void {
3132
factory: 'Create a new factory',
3233
notification: 'Create a new notification',
3334
name: 'The name of the action',
35+
queue: 'Make queue migration',
3436
stack: 'Create a new stack',
3537
certificate: 'Create a new SSL Certificate',
3638
select: 'What are you trying to make?',
@@ -50,6 +52,7 @@ export function make(buddy: CLI): void {
5052
.option('-m, --migration [migration]', descriptions.migration, { default: false })
5153
.option('-f, --factory [factory]', descriptions.factory, { default: false })
5254
.option('-n, --notification [notification]', descriptions.notification, { default: false })
55+
.option('-qt, --queue-table', descriptions.queue, { default: false })
5356
.option('-s, --stack [stack]', descriptions.stack, { default: false })
5457
.option('--verbose', descriptions.verbose, { default: false })
5558
.action(async (options: MakeOptions) => {
@@ -341,6 +344,16 @@ export function make(buddy: CLI): void {
341344
log.success('Certificate installed')
342345
})
343346

347+
buddy
348+
.command('make:queue-table', descriptions.migration)
349+
.option('-p, --project [project]', descriptions.project, { default: false })
350+
.option('--verbose', descriptions.verbose, { default: false })
351+
.action(async (options: MakeOptions) => {
352+
log.debug('Running `buddy make queue:table` ...', options)
353+
354+
await makeQueueTable()
355+
})
356+
344357
buddy.on('make:*', () => {
345358
console.error('Invalid command: %s\nSee --help for a list of available commands.', buddy.args.join(' '))
346359
process.exit(1)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { log } from '@stacksjs/cli'
2+
import { database } from '@stacksjs/config'
3+
import { path } from '@stacksjs/path'
4+
import { hasTableBeenMigrated } from '../drivers'
5+
6+
const driver = database.default || ''
7+
8+
export async function createErrorsTable(): Promise<void> {
9+
if (['sqlite', 'mysql'].includes(driver)) {
10+
const hasBeenMigrated = await hasTableBeenMigrated('errors')
11+
12+
if (hasBeenMigrated)
13+
return
14+
15+
let migrationContent = `import type { Database } from '@stacksjs/database'\nimport { sql } from '@stacksjs/database'\n\n`
16+
migrationContent += `export async function up(db: Database<any>) {\n`
17+
migrationContent += ` await db.schema\n`
18+
migrationContent += ` .createTable('errors')\n`
19+
migrationContent += ` .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())\n`
20+
migrationContent += ` .addColumn('type', 'varchar(255)', col => col.notNull())\n` // The type of error
21+
migrationContent += ` .addColumn('message', 'text', col => col.notNull())\n` // The error message
22+
migrationContent += ` .addColumn('stack', 'text')\n` // Optional stack trace
23+
migrationContent += ` .addColumn('status', 'integer', col => col.notNull().defaultTo(0))\n` // Status code
24+
migrationContent += ` .addColumn('user_id', 'integer')\n` // Optional user ID if applicable
25+
migrationContent += ` .addColumn('additional_info', 'text')\n` // Optional field for extra info
26+
migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))\n` // When the error was logged
27+
migrationContent += ` .addColumn('updated_at', 'timestamp')\n` // When the error was last updated
28+
migrationContent += ` .execute()\n`
29+
migrationContent += `}\n`
30+
31+
const timestamp = new Date().getTime().toString()
32+
const migrationFileName = `${timestamp}-create-errors-table.ts`
33+
34+
const migrationFilePath = path.userMigrationsPath(migrationFileName)
35+
36+
await Bun.write(migrationFilePath, migrationContent) // Ensure the write operation is awaited
37+
38+
log.success('Created errors table')
39+
}
40+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './errors'
2+
export * from './jobs'
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type { MigrationResult } from '../migrations'
2+
import { log } from '@stacksjs/cli'
3+
import { database } from '@stacksjs/config'
4+
import { err, handleError, ok, type Result } from '@stacksjs/error-handling'
5+
import { path } from '@stacksjs/path'
6+
import { hasTableBeenMigrated } from '../drivers'
7+
8+
const driver = database.default || ''
9+
10+
export async function createJobsMigration(): Promise<Result<MigrationResult[] | string, Error>> {
11+
try {
12+
if (['sqlite', 'mysql'].includes(driver)) {
13+
const hasBeenMigrated = await hasTableBeenMigrated('jobs')
14+
15+
if (!hasBeenMigrated) {
16+
let migrationContent = `import type { Database } from '@stacksjs/database'\nimport { sql } from '@stacksjs/database'\n\n`
17+
migrationContent += `export async function up(db: Database<any>) {\n`
18+
migrationContent += ` await db.schema\n`
19+
migrationContent += ` .createTable('jobs')\n`
20+
migrationContent += ` .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())\n`
21+
migrationContent += ` .addColumn('queue', 'varchar(255)', col => col.notNull())\n`
22+
migrationContent += ` .addColumn('payload', 'text', col => col.notNull())\n`
23+
migrationContent += ` .addColumn('attempts', 'integer', col => col.notNull().defaultTo(0))\n`
24+
migrationContent += ` .addColumn('reserved_at', 'timestamp')\n`
25+
migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))\n` // When the error was logged
26+
migrationContent += ` .addColumn('updated_at', 'timestamp')\n`
27+
migrationContent += ` .execute()\n`
28+
migrationContent += `}\n`
29+
30+
const timestamp = new Date().getTime().toString()
31+
const migrationFileName = `${timestamp}-create-jobs-table.ts`
32+
33+
const migrationFilePath = path.userMigrationsPath(migrationFileName)
34+
35+
await Bun.write(migrationFilePath, migrationContent) // Ensure the write operation is awaited
36+
37+
log.success('Created jobs table')
38+
}
39+
}
40+
41+
return ok('Database migration completed with no new migrations.')
42+
}
43+
catch (error) {
44+
return err(handleError('Error creating migration', error))
45+
}
46+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './custom'
12
export * from './drivers'
23
export * from './migrations'
34
export * from './schema'

storage/framework/core/database/src/migrations.ts

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { type Err, err, handleError, type Ok, ok, type Result } from '@stacksjs/
55
import { path } from '@stacksjs/path'
66
import { fs, globSync } from '@stacksjs/storage'
77
import { FileMigrationProvider, type MigrationResult, Migrator } from 'kysely'
8-
import { generateMysqlMigration, generatePostgresMigration, generateSqliteMigration, hasTableBeenMigrated, resetMysqlDatabase, resetPostgresDatabase, resetSqliteDatabase } from './drivers'
8+
import { createErrorsTable } from './custom/errors'
9+
import { generateMysqlMigration, generatePostgresMigration, generateSqliteMigration, resetMysqlDatabase, resetPostgresDatabase, resetSqliteDatabase } from './drivers'
910
import { db } from './utils'
1011

1112
const driver = database.default || ''
@@ -99,40 +100,6 @@ export async function generateMigrations(): Promise<Ok<string, never> | Err<stri
99100
}
100101
}
101102

102-
async function createErrorsTable(): Promise<void> {
103-
if (['sqlite', 'mysql'].includes(driver)) {
104-
const hasBeenMigrated = await hasTableBeenMigrated('errors')
105-
106-
if (hasBeenMigrated)
107-
return
108-
109-
let migrationContent = `import type { Database } from '@stacksjs/database'\nimport { sql } from '@stacksjs/database'\n\n`
110-
migrationContent += `export async function up(db: Database<any>) {\n`
111-
migrationContent += ` await db.schema\n`
112-
migrationContent += ` .createTable('errors')\n`
113-
migrationContent += ` .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())\n`
114-
migrationContent += ` .addColumn('type', 'varchar(255)', col => col.notNull())\n` // The type of error
115-
migrationContent += ` .addColumn('message', 'text', col => col.notNull())\n` // The error message
116-
migrationContent += ` .addColumn('stack', 'text')\n` // Optional stack trace
117-
migrationContent += ` .addColumn('status', 'integer', col => col.notNull().defaultTo(0))\n` // Status code
118-
migrationContent += ` .addColumn('user_id', 'integer')\n` // Optional user ID if applicable
119-
migrationContent += ` .addColumn('additional_info', 'text')\n` // Optional field for extra info
120-
migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))\n` // When the error was logged
121-
migrationContent += ` .addColumn('updated_at', 'timestamp')\n` // When the error was last updated
122-
migrationContent += ` .execute()\n`
123-
migrationContent += `}\n`
124-
125-
const timestamp = new Date().getTime().toString()
126-
const migrationFileName = `${timestamp}-create-errors-table.ts`
127-
128-
const migrationFilePath = path.userMigrationsPath(migrationFileName)
129-
130-
await Bun.write(migrationFilePath, migrationContent) // Ensure the write operation is awaited
131-
132-
log.success('Created errors table')
133-
}
134-
}
135-
136103
export async function generateMigration(modelPath: string): Promise<void> {
137104
if (driver === 'sqlite')
138105
await generateSqliteMigration(modelPath)

storage/framework/core/enums/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export enum Action {
8484
Lint = 'lint/index',
8585
LintFix = 'lint/fix',
8686
Prepublish = 'prepublish',
87+
QueueTable = 'queue/table',
8788
Release = 'release', // ✅
8889
RouteList = 'route/list', // ✅
8990
StripeSetup = 'saas/setup',

0 commit comments

Comments
 (0)