Skip to content

Commit 8cc444a

Browse files
committed
Refactors and enhances database migrations
Improves database migration process by adding status checks, rollback functionality and standardizing code formatting. Introduces migration status command to check executed and pending migrations. Adds rollback command to revert migrations. Changes code formatting to use 2 spaces for indentation.
1 parent 9262f6e commit 8cc444a

File tree

15 files changed

+693
-518
lines changed

15 files changed

+693
-518
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
root = true
22

33
[*]
4-
indent_size = 4
4+
indent_size = 2
55
indent_style = space
66
end_of_line = lf
77
charset = utf-8

playground/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"extends": "./.nuxt/tsconfig.json"
2+
"extends": "./.nuxt/tsconfig.json"
33
}

src/runtime/server/cli/migrate.ts

Lines changed: 78 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -9,95 +9,95 @@ const args = process.argv.slice(2)
99
const command = args[0]
1010

1111
const 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

2121
async 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

3636
async 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

8686
async 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(`
101101
Usage: npx nuxt-token-authentication migrate [command]
102102
103103
Commands:
@@ -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

117117
main().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

Comments
 (0)