Skip to content

Commit 3dc215b

Browse files
committed
improve db init ux - dont throw error on CONFLICT (db already connected)
- if db already connected, we just say its connected and continue to log the status - improve status by fetching cli-db-status extension endpoint
1 parent f4a9e6a commit 3dc215b

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

src/commands/database/init.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,27 +118,48 @@ export const init = async (_options: OptionValues, command: BaseCommand) => {
118118
code?: string
119119
message?: string
120120
}
121-
throw new Error(`Failed to initialize DB: ${error.message ?? 'Unknown error occurred'}`)
121+
if (error.code === 'CONFLICT') {
122+
log(`Database already connected to this site. Skipping initialization.`)
123+
} else {
124+
throw new Error(`Failed to initialize DB: ${error.message ?? 'Unknown error occurred'}`)
125+
}
122126
}
123127

124-
const res = (await req.json()) as {
125-
code?: string
126-
message?: string
127-
}
128+
let status
128129

129-
if (res.code !== 'DATABASE_INITIALIZED') {
130-
throw new Error(`Failed to initialize DB: ${res.message ?? 'Unknown error'}`)
130+
try {
131+
const statusEndpoint = new URL('/api/cli-db-status', hostSiteUrl).toString()
132+
const statusRes = await fetch(statusEndpoint, {
133+
headers,
134+
})
135+
if (!statusRes.ok) {
136+
throw new Error(`Failed to get database status`, { cause: statusRes })
137+
}
138+
status = (await statusRes.json()) as {
139+
siteConfiguration?: {
140+
connectedDatabase?: {
141+
isConnected: boolean
142+
}
143+
}
144+
existingManagedEnvs?: string[]
145+
}
146+
} catch (e) {
147+
console.error('Failed to get database status', e)
131148
}
132149

133150
log(
134151
prettyjson.render({
135152
'Current team': account.name,
136153
'Current site': siteInfo.name,
137154
[`${extension.name} extension`]: 'installed on team',
138-
['Database status']: 'connected to site',
155+
['Database status']: status?.siteConfiguration?.connectedDatabase?.isConnected
156+
? 'connected to site'
157+
: 'not connected',
139158
['Environment variables']: '',
140-
[' NETLIFY_DATABASE_URL']: 'saved',
141-
[' NETLIFY_DATABASE_URL_UNPOOLED']: 'saved',
159+
[' NETLIFY_DATABASE_URL']: status?.existingManagedEnvs?.includes('NETLIFY_DATABASE_URL') ? 'saved' : 'not set',
160+
[' NETLIFY_DATABASE_URL_UNPOOLED']: status?.existingManagedEnvs?.includes('NETLIFY_DATABASE_URL_UNPOOLED')
161+
? 'saved'
162+
: 'not set',
142163
}),
143164
)
144165
return

0 commit comments

Comments
 (0)