@@ -9,95 +9,95 @@ const args = process.argv.slice(2)
99const command = args [ 0 ]
1010
1111const options = {
12- ...defaultOptions ,
13- connector : {
14- name : 'sqlite' as const ,
15- options : {
16- path : './data/db.sqlite3' ,
17- } ,
12+ ...defaultOptions ,
13+ connector : {
14+ name : 'sqlite' as const ,
15+ options : {
16+ path : './data/db.sqlite3' ,
1817 } ,
18+ } ,
1919}
2020
2121async function runMigrations ( ) {
22- try {
23- const db = createDatabase ( sqlite ( options . connector ! . options ) )
24- const migrationManager = new MigrationManager ( db , options )
25-
26- console . log ( 'Running database migrations...' )
27- await migrationManager . migrate ( )
28- console . log ( 'Migrations completed successfully!' )
29- }
30- catch ( error ) {
31- console . error ( 'Migration failed:' , error )
32- process . exit ( 1 )
33- }
22+ try {
23+ const db = createDatabase ( sqlite ( options . connector ! . options ) )
24+ const migrationManager = new MigrationManager ( db , options )
25+
26+ console . log ( 'Running database migrations...' )
27+ await migrationManager . migrate ( )
28+ console . log ( 'Migrations completed successfully!' )
29+ }
30+ catch ( error ) {
31+ console . error ( 'Migration failed:' , error )
32+ process . exit ( 1 )
33+ }
3434}
3535
3636async function showStatus ( ) {
37- try {
38- const db = createDatabase ( sqlite ( options . connector ! . options ) )
39- const migrationManager = new MigrationManager ( db , options )
40-
41- const status = await migrationManager . status ( )
42-
43- console . log ( '\nMigration Status:' )
44- console . log ( '================' )
45-
46- if ( status . executed . length > 0 ) {
47- console . log ( '\nExecuted migrations:' )
48- status . executed . forEach ( ( migration ) => {
49- console . log ( ` ✓ ${ migration . name } (v${ migration . version } ) - ${ migration . executed_at } ` )
50- } )
51- }
52-
53- if ( status . pending . length > 0 ) {
54- console . log ( '\nPending migrations:' )
55- status . pending . forEach ( ( migration ) => {
56- console . log ( ` ⏳ ${ migration . name } (v${ migration . version } )` )
57- } )
58- }
59- else {
60- console . log ( '\nNo pending migrations.' )
61- }
62- }
63- catch ( error ) {
64- console . error ( 'Failed to get migration status:' , error )
65- process . exit ( 1 )
66- }
67- }
37+ try {
38+ const db = createDatabase ( sqlite ( options . connector ! . options ) )
39+ const migrationManager = new MigrationManager ( db , options )
6840
69- async function rollbackMigrations ( ) {
70- const steps = Number . parseInt ( args [ 1 ] ) || 1
41+ const status = await migrationManager . status ( )
7142
72- try {
73- const db = createDatabase ( sqlite ( options . connector ! . options ) )
74- const migrationManager = new MigrationManager ( db , options )
43+ console . log ( '\nMigration Status:' )
44+ console . log ( '================' )
7545
76- console . log ( `Rolling back ${ steps } migration(s)...` )
77- await migrationManager . rollback ( steps )
78- console . log ( 'Rollback completed successfully!' )
46+ if ( status . executed . length > 0 ) {
47+ console . log ( '\nExecuted migrations:' )
48+ status . executed . forEach ( ( migration ) => {
49+ console . log ( ` ✓ ${ migration . name } (v${ migration . version } ) - ${ migration . executed_at } ` )
50+ } )
7951 }
80- catch ( error ) {
81- console . error ( 'Rollback failed:' , error )
82- process . exit ( 1 )
52+
53+ if ( status . pending . length > 0 ) {
54+ console . log ( '\nPending migrations:' )
55+ status . pending . forEach ( ( migration ) => {
56+ console . log ( ` ⏳ ${ migration . name } (v${ migration . version } )` )
57+ } )
8358 }
59+ else {
60+ console . log ( '\nNo pending migrations.' )
61+ }
62+ }
63+ catch ( error ) {
64+ console . error ( 'Failed to get migration status:' , error )
65+ process . exit ( 1 )
66+ }
67+ }
68+
69+ async function rollbackMigrations ( ) {
70+ const steps = Number . parseInt ( args [ 1 ] ) || 1
71+
72+ try {
73+ const db = createDatabase ( sqlite ( options . connector ! . options ) )
74+ const migrationManager = new MigrationManager ( db , options )
75+
76+ console . log ( `Rolling back ${ steps } migration(s)...` )
77+ await migrationManager . rollback ( steps )
78+ console . log ( 'Rollback completed successfully!' )
79+ }
80+ catch ( error ) {
81+ console . error ( 'Rollback failed:' , error )
82+ process . exit ( 1 )
83+ }
8484}
8585
8686async function main ( ) {
87- switch ( command ) {
88- case 'migrate' :
89- case 'up' :
90- await runMigrations ( )
91- break
92- case 'status' :
93- await showStatus ( )
94- break
95- case 'rollback' :
96- case 'down' :
97- await rollbackMigrations ( )
98- break
99- default :
100- console . log ( `
87+ switch ( command ) {
88+ case 'migrate' :
89+ case 'up' :
90+ await runMigrations ( )
91+ break
92+ case 'status' :
93+ await showStatus ( )
94+ break
95+ case 'rollback' :
96+ case 'down' :
97+ await rollbackMigrations ( )
98+ break
99+ default :
100+ console . log ( `
101101Usage: npx nuxt-token-authentication migrate [command]
102102
103103Commands:
@@ -110,11 +110,11 @@ Examples:
110110 npx nuxt-token-authentication migrate status
111111 npx nuxt-token-authentication migrate rollback 2
112112 ` )
113- process . exit ( 1 )
114- }
113+ process . exit ( 1 )
114+ }
115115}
116116
117117main ( ) . catch ( ( error ) => {
118- console . error ( 'CLI error:' , error )
119- process . exit ( 1 )
118+ console . error ( 'CLI error:' , error )
119+ process . exit ( 1 )
120120} )
0 commit comments