@@ -118,27 +118,48 @@ export const init = async (_options: OptionValues, command: BaseCommand) => {
118
118
code ?: string
119
119
message ?: string
120
120
}
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
+ }
122
126
}
123
127
124
- const res = ( await req . json ( ) ) as {
125
- code ?: string
126
- message ?: string
127
- }
128
+ let status
128
129
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 )
131
148
}
132
149
133
150
log (
134
151
prettyjson . render ( {
135
152
'Current team' : account . name ,
136
153
'Current site' : siteInfo . name ,
137
154
[ `${ 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' ,
139
158
[ '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' ,
142
163
} ) ,
143
164
)
144
165
return
0 commit comments