@@ -18,6 +18,8 @@ type Flags = {
1818} ;
1919
2020const SIGNAL_CLEANUP_TIMEOUT_MS = 10 * 1000 ;
21+ const CONNECTION_STRING_RETRY_COUNT = 2 ;
22+ const CONNECTION_STRING_RETRY_DELAY_MS = 100 ;
2123const SCRATCH_SCALE_TO_ZERO = {
2224 enabled : true ,
2325 inactivityPeriodMinutes : 10
@@ -57,6 +59,10 @@ async function withTimeout<T>(promise: Promise<T>, ms: number) {
5759 }
5860}
5961
62+ async function sleep ( ms : number ) {
63+ await new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
64+ }
65+
6066function getErrorMessage ( error : unknown ) {
6167 if ( error instanceof Error ) return error . message ;
6268 if ( typeof error === 'object' && error !== null && 'message' in error ) return String ( error . message ) ;
@@ -202,6 +208,28 @@ async function deleteScratchBranchByName(
202208 }
203209}
204210
211+ async function resolveScratchBranchConnectionString (
212+ context : LocalContext ,
213+ organizationId : string ,
214+ projectId : string ,
215+ branch : ScratchBranch
216+ ) {
217+ if ( branch . connectionString ) return branch ;
218+
219+ let resolvedBranch = branch ;
220+ for ( let retry = 0 ; retry < CONNECTION_STRING_RETRY_COUNT ; retry ++ ) {
221+ await sleep ( CONNECTION_STRING_RETRY_DELAY_MS ) ;
222+
223+ resolvedBranch = await context . api . branches . describeBranch ( {
224+ pathParams : { organizationID : organizationId , projectID : projectId , branchID : branch . id }
225+ } ) ;
226+
227+ if ( resolvedBranch . connectionString ) return resolvedBranch ;
228+ }
229+
230+ return resolvedBranch ;
231+ }
232+
205233export async function implementation ( this : LocalContext , flags : Flags , ...command : string [ ] ) {
206234 const hasQuery = Boolean ( flags . execute ) ;
207235 const hasBinary = command . length > 0 ;
@@ -337,9 +365,11 @@ export async function implementation(this: LocalContext, flags: Flags, ...comman
337365
338366 this . process . stderr . write ( chalk . green ( `Created scratch branch ${ createdScratchBranch . name } \n` ) ) ;
339367
340- invariant ( createdScratchBranch . connectionString , 'Scratch branch should have a connection string.' ) ;
368+ scratchBranch = await resolveScratchBranchConnectionString ( this , organizationId , projectId , createdScratchBranch ) ;
369+
370+ invariant ( scratchBranch . connectionString , 'Scratch branch should have a connection string.' ) ;
341371
342- const connectionString = buildConnectionString ( createdScratchBranch . connectionString , {
372+ const connectionString = buildConnectionString ( scratchBranch . connectionString , {
343373 database,
344374 endpointType : 'rw'
345375 } ) ;
0 commit comments