1
- // import { generateMigrationFile } from '@stacksjs/database'
2
- import User from '../../../../../../app/Models/User'
3
- import { fieldAssociation , modelEntity } from './fields'
4
-
5
- const file = Bun . file ( 'user-migration.ts' )
6
- const writer = file . writer ( )
7
-
8
- const driver = 'sqlite'
9
-
10
- writer . write ( 'import { db, sql, Database } from \'@stacksjs/database\'\n' )
11
- writer . write ( '\n' )
12
- writer . write ( 'export async function up(db: Database<any>) {\n' )
13
-
14
- writer . write ( ' await db.schema\n' )
15
- writer . write ( ` .createTable('${ User . table } ')\n` )
16
-
17
- writer . write ( ' .addColumn(\'id\', \'integer\', (col) => col.primaryKey())\n' )
18
-
19
- for ( let modelIndex = 0 ; modelIndex < modelEntity . length ; modelIndex ++ ) {
20
- const modelElement = modelEntity [ modelIndex ]
21
-
22
- if ( ! modelElement )
23
- continue
24
-
25
- let entity = ''
26
- let characteristic = ''
27
-
28
- const isNullablePresent = modelElement . fieldArray . some ( item => item . entity === 'nullable' )
29
-
30
- if ( modelElement . default )
31
- characteristic += `.defaultTo('${ modelElement . default . toString ( ) } ')`
32
-
33
- if ( modelElement . unique )
34
- characteristic += '.unique()'
35
-
36
- if ( ! isNullablePresent )
37
- characteristic += '.notNull()'
38
-
39
- for ( let fieldIndex = 0 ; fieldIndex < modelElement . fieldArray . length ; fieldIndex ++ ) {
40
- const fieldArrayElement = modelElement . fieldArray [ fieldIndex ]
41
-
42
- if ( ! fieldArrayElement )
43
- continue
44
-
45
- if ( ! fieldAssociation [ driver ] )
46
- continue
47
-
48
- if ( fieldAssociation [ driver ] [ fieldArrayElement . entity ] )
49
- entity += `${ fieldAssociation [ driver ] [ fieldArrayElement . entity ] } `
1
+ import { Attributes } from "@stacksjs/types"
2
+
3
+ export async function createTableMigration ( modelPath : string ) {
4
+
5
+ log . debug ( 'createTableMigration modelPath:' , modelPath )
6
+
7
+ const model = await import ( modelPath )
8
+ const tableName = model . default . table
9
+
10
+ const fields = model . default . attributes
11
+ const useTimestamps = model . default ?. traits ?. useTimestamps ?? model . default ?. traits ?. timestampable
12
+ const useSoftDeletes = model . default ?. traits ?. useSoftDeletes ?? model . default ?. traits ?. softDeletable
13
+
14
+ let migrationContent = `import type { Database } from '@stacksjs/database'\n`
15
+ migrationContent += `import { 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('${ tableName } ')\n`
19
+
20
+ for ( const [ fieldName , options ] of Object . entries ( fields ) ) {
21
+ const fieldOptions = options as Attributes
22
+ const columnType = mapFieldTypeToColumnType ( fieldOptions . validator ?. rule )
23
+ migrationContent += ` .addColumn('${ fieldName } ', '${ columnType } '`
24
+
25
+ // Check if there are configurations that require the lambda function
26
+ if ( fieldOptions . unique || ( fieldOptions . validator ?. rule ?. required ) ) {
27
+ migrationContent += `, col => col`
28
+ if ( fieldOptions . unique )
29
+ migrationContent += `.unique()`
30
+ if ( fieldOptions . validator ?. rule ?. required )
31
+ migrationContent += `.notNull()`
32
+ migrationContent += ``
33
+ }
34
+
35
+ migrationContent += `)\n`
36
+ }
50
37
51
- // fieldEntity.forEach((entityField) => {
52
- // if (fieldArrayElement.entity === entityField)
53
- // entity += `(${fieldArrayElement.charValue}) `
54
- // })
38
+ // Append created_at and updated_at columns if useTimestamps is true
39
+ if ( useTimestamps ) {
40
+ migrationContent += ` .addColumn('created_at', 'timestamp', col => col.notNull().defaultTo(sql.raw('CURRENT_TIMESTAMP')))\n `
41
+ migrationContent += ` .addColumn('updated_at', 'timestamp')\n`
55
42
}
56
43
57
- writer . write ( ` .addColumn('${ modelElement . field } ', '${ entity } ', (col) => col${ characteristic } )\n` )
58
- }
44
+ // Append deleted_at column if useSoftDeletes is true
45
+ if ( useSoftDeletes )
46
+ migrationContent += ` .addColumn('deleted_at', 'timestamp')\n`
47
+
48
+ migrationContent += ` .execute()\n`
49
+ migrationContent += `}\n`
59
50
60
- writer . write ( ' .addColumn(\'created_at\', \'timestamp\', (col) => col.defaultTo(sql`CURRENT_TIMESTAMP`).notNull())\n' )
61
- writer . write ( ' .execute()\n' )
62
- writer . write ( '}\n\n' )
51
+ const timestamp = new Date ( ) . getTime ( ) . toString ( )
52
+ const migrationFileName = ` ${ timestamp } -create- ${ tableName } -table.ts`
53
+ const migrationFilePath = path . userMigrationsPath ( migrationFileName )
63
54
64
- writer . write ( 'process.exit(0)\n' )
55
+ // Assuming fs.writeFileSync is available or use an equivalent method
56
+ Bun . write ( migrationFilePath , migrationContent )
65
57
66
- await writer . end ( )
58
+ log . success ( `Created migration: ${ migrationFileName } ` )
59
+ }
0 commit comments