@@ -27,13 +27,14 @@ export async function provisionCloudSql(args: {
2727 const existingInstance = await cloudSqlAdminClient . getInstance ( projectId , instanceId ) ;
2828 silent || utils . logLabeledBullet ( "dataconnect" , `Found existing instance ${ instanceId } .` ) ;
2929 connectionName = existingInstance ?. connectionName || "" ;
30- if ( ! checkInstanceConfig ( existingInstance , enableGoogleMlIntegration ) ) {
31- // TODO: Return message from checkInstanceConfig to explain exactly what changes are made
30+ const why = getUpdateReason ( existingInstance , enableGoogleMlIntegration ) ;
31+ if ( why ) {
3232 silent ||
3333 utils . logLabeledBullet (
3434 "dataconnect" ,
3535 `Instance ${ instanceId } settings not compatible with Firebase Data Connect. ` +
36- `Updating instance to enable Cloud IAM authentication and public IP. This may take a few minutes...` ,
36+ `Updating instance. This may take a few minutes...` +
37+ why ,
3738 ) ;
3839 await promiseWithSpinner (
3940 ( ) =>
@@ -77,11 +78,21 @@ export async function provisionCloudSql(args: {
7778 try {
7879 await cloudSqlAdminClient . getDatabase ( projectId , instanceId , databaseId ) ;
7980 silent || utils . logLabeledBullet ( "dataconnect" , `Found existing database ${ databaseId } .` ) ;
80- } catch ( err ) {
81- silent ||
82- utils . logLabeledBullet ( "dataconnect" , `Database ${ databaseId } not found, creating it now...` ) ;
83- await cloudSqlAdminClient . createDatabase ( projectId , instanceId , databaseId ) ;
84- silent || utils . logLabeledBullet ( "dataconnect" , `Database ${ databaseId } created.` ) ;
81+ } catch ( err : any ) {
82+ if ( err . status === 404 ) {
83+ // Create the database if not found.
84+ silent ||
85+ utils . logLabeledBullet (
86+ "dataconnect" ,
87+ `Database ${ databaseId } not found, creating it now...` ,
88+ ) ;
89+ await cloudSqlAdminClient . createDatabase ( projectId , instanceId , databaseId ) ;
90+ silent || utils . logLabeledBullet ( "dataconnect" , `Database ${ databaseId } created.` ) ;
91+ } else {
92+ // Skip it if the database is not accessible.
93+ // Possible that the CSQL instance is in the middle of something.
94+ silent || utils . logLabeledWarning ( "dataconnect" , `Database ${ databaseId } is not accessible.` ) ;
95+ }
8596 }
8697 if ( enableGoogleMlIntegration ) {
8798 await grantRolesToCloudSqlServiceAccount ( projectId , instanceId , [ GOOGLE_ML_INTEGRATION_ROLE ] ) ;
@@ -92,26 +103,24 @@ export async function provisionCloudSql(args: {
92103/**
93104 * Validate that existing CloudSQL instances have the necessary settings.
94105 */
95- export function checkInstanceConfig (
96- instance : Instance ,
97- requireGoogleMlIntegration : boolean ,
98- ) : boolean {
106+ export function getUpdateReason ( instance : Instance , requireGoogleMlIntegration : boolean ) : string {
107+ let reason = "" ;
99108 const settings = instance . settings ;
100109 // CloudSQL instances must have public IP enabled to be used with Firebase Data Connect.
101110 if ( ! settings . ipConfiguration ?. ipv4Enabled ) {
102- return false ;
111+ reason += "\n - to enable public IP." ;
103112 }
104113
105114 if ( requireGoogleMlIntegration ) {
106115 if ( ! settings . enableGoogleMlIntegration ) {
107- return false ;
116+ reason += "\n - to enable Google ML integration." ;
108117 }
109118 if (
110119 ! settings . databaseFlags ?. some (
111120 ( f ) => f . name === "cloudsql.enable_google_ml_integration" && f . value === "on" ,
112121 )
113122 ) {
114- return false ;
123+ reason += "\n - to enable Google ML integration database flag." ;
115124 }
116125 }
117126
@@ -120,6 +129,9 @@ export function checkInstanceConfig(
120129 settings . databaseFlags ?. some (
121130 ( f ) => f . name === "cloudsql.iam_authentication" && f . value === "on" ,
122131 ) ?? false ;
132+ if ( ! isIamEnabled ) {
133+ reason += "\n - to enable IAM authentication database flag." ;
134+ }
123135
124- return isIamEnabled ;
136+ return reason ;
125137}
0 commit comments