Skip to content

Commit 0280eca

Browse files
committed
feat: add migration
1 parent 16aefa9 commit 0280eca

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export default defineNuxtModule<RuntimeModuleOptions>({
143143
}
144144

145145
return combinedWhitelist
146-
})()
146+
})(),
147147
permissions: runtimeConfigOptions.auth?.permissions || defaultOptions.auth.permissions
148148
},
149149
apiBasePath: runtimeConfigOptions.apiBasePath || defaultOptions.apiBasePath
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { useDb } from './db'
2+
import type { ModuleOptions } from 'nuxt-users/utils'
3+
4+
export const addGoogleOauthFields = async (options: ModuleOptions) => {
5+
const connectorName = options.connector!.name
6+
const db = await useDb(options)
7+
const tableName = options.tables.users
8+
9+
console.log(`[Nuxt Users] DB:Add Google OAuth fields to ${connectorName} Users Table in ${tableName}...`)
10+
11+
if (connectorName === 'sqlite' || connectorName === 'mysql' || connectorName === 'postgresql') {
12+
// Try to add the columns, ignore errors if they already exist
13+
try {
14+
await db.sql`ALTER TABLE {${tableName}} ADD COLUMN google_id TEXT UNIQUE`
15+
console.log('[Nuxt Users] Added google_id column ✅')
16+
}
17+
catch (error) {
18+
// Column might already exist, ignore the error
19+
console.log('[Nuxt Users] google_id column might already exist')
20+
}
21+
22+
try {
23+
await db.sql`ALTER TABLE {${tableName}} ADD COLUMN profile_picture TEXT`
24+
console.log('[Nuxt Users] Added profile_picture column ✅')
25+
}
26+
catch (error) {
27+
// Column might already exist, ignore the error
28+
console.log('[Nuxt Users] profile_picture column might already exist')
29+
}
30+
}
31+
32+
console.log(`[Nuxt Users] DB:Add Google OAuth fields to ${connectorName} Users Table ✅`)
33+
}

src/runtime/server/utils/migrate.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { createPersonalAccessTokensTable } from './create-personal-access-tokens
55
import { createPasswordResetTokensTable } from './create-password-reset-tokens-table'
66
import { createMigrationsTable } from './create-migrations-table'
77
import { addActiveToUsers } from './add-active-to-users'
8+
import { addGoogleOauthFields } from './add-google-oauth-fields'
89

910
interface Migration {
1011
name: string
@@ -31,6 +32,10 @@ const migrations: Migration[] = [
3132
{
3233
name: 'add_active_to_users',
3334
run: addActiveToUsers
35+
},
36+
{
37+
name: 'add_google_oauth_fields',
38+
run: addGoogleOauthFields
3439
}
3540
]
3641

0 commit comments

Comments
 (0)