(args: {
options: Options;
schema: Schema;
/** true for `dataconnect:sql:migrate`, false for `deploy` */
validateOnly: boolean;
schemaValidation?: SchemaValidation;
stats?: DeployStats;
})
| 149 | } |
| 150 | |
| 151 | export async function migrateSchema(args: { |
| 152 | options: Options; |
| 153 | schema: Schema; |
| 154 | /** true for `dataconnect:sql:migrate`, false for `deploy` */ |
| 155 | validateOnly: boolean; |
| 156 | schemaValidation?: SchemaValidation; |
| 157 | stats?: DeployStats; |
| 158 | }): Promise<Diff[]> { |
| 159 | const { options, schema, validateOnly, schemaValidation, stats } = args; |
| 160 | |
| 161 | // If the schema validation mode is unset, we prompt COMPATIBLE SQL diffs and then STRICT diffs. |
| 162 | let validationMode: SchemaValidation = schemaValidation ?? "COMPATIBLE"; |
| 163 | setSchemaValidationMode(schema, validationMode); |
| 164 | displayStartSchemaDiff(validationMode); |
| 165 | |
| 166 | const projectId = needProjectId(options); |
| 167 | const { serviceName, instanceId, instanceName, databaseId, schemaName } = getIdentifiers(schema); |
| 168 | await ensureServiceIsConnectedToCloudSql( |
| 169 | serviceName, |
| 170 | instanceName, |
| 171 | databaseId, |
| 172 | /* linkIfNotConnected=*/ true, |
| 173 | schemaName, |
| 174 | ); |
| 175 | |
| 176 | // Check if Cloud SQL instance is still being created. |
| 177 | const existingInstance = await cloudSqlAdminClient.getInstance(projectId, instanceId); |
| 178 | if (existingInstance.state === "PENDING_CREATE") { |
| 179 | if (stats) { |
| 180 | stats.numSchemaSkippedDueToPendingCreate++; |
| 181 | } |
| 182 | const postgresql = schema.datasources.find((d) => d.postgresql)?.postgresql; |
| 183 | if (!postgresql) { |
| 184 | throw new FirebaseError( |
| 185 | `Cannot find Postgres datasource in the schema to deploy: ${serviceName}/schemas/${MAIN_SCHEMA_ID}.\nIts datasources: ${JSON.stringify(schema.datasources)}`, |
| 186 | ); |
| 187 | } |
| 188 | postgresql.schemaValidation = "NONE"; |
| 189 | postgresql.schemaMigration = undefined; |
| 190 | await upsertSchema(schema, validateOnly); |
| 191 | postgresql.schemaValidation = undefined; |
| 192 | postgresql.schemaMigration = "MIGRATE_COMPATIBLE"; |
| 193 | await upsertSchema(schema, validateOnly, /* async= */ true); |
| 194 | logLabeledWarning( |
| 195 | "dataconnect", |
| 196 | `Skip SQL schema migration because Cloud SQL is still being created`, |
| 197 | ); |
| 198 | return []; |
| 199 | } |
| 200 | |
| 201 | // Make sure database is setup. |
| 202 | await setupSchemaIfNecessary(instanceId, databaseId, schemaName, options); |
| 203 | |
| 204 | let diffs: Diff[] = []; |
| 205 | try { |
| 206 | await upsertSchema(schema, validateOnly); |
| 207 | displayNoSchemaDiff(instanceId, databaseId, validationMode); |
| 208 | } catch (err: any) { |
no test coverage detected
searching dependent graphs…