( options: Options, schema: Schema, schemaValidation?: SchemaValidation, )
| 69 | } |
| 70 | |
| 71 | export async function diffSchema( |
| 72 | options: Options, |
| 73 | schema: Schema, |
| 74 | schemaValidation?: SchemaValidation, |
| 75 | ): Promise<Diff[]> { |
| 76 | // If the schema validation mode is unset, we diff in strict mode first. |
| 77 | let validationMode: SchemaValidation = schemaValidation ?? "STRICT"; |
| 78 | setSchemaValidationMode(schema, validationMode); |
| 79 | displayStartSchemaDiff(validationMode); |
| 80 | |
| 81 | const { serviceName, instanceName, databaseId, instanceId, schemaName } = getIdentifiers(schema); |
| 82 | await ensureServiceIsConnectedToCloudSql( |
| 83 | serviceName, |
| 84 | instanceName, |
| 85 | databaseId, |
| 86 | /* linkIfNotConnected=*/ false, |
| 87 | schemaName, |
| 88 | ); |
| 89 | |
| 90 | let incompatible: IncompatibleSqlSchemaError | undefined = undefined; |
| 91 | try { |
| 92 | await upsertSchema(schema, /** validateOnly=*/ true); |
| 93 | displayNoSchemaDiff(instanceId, databaseId, validationMode); |
| 94 | } catch (err: any) { |
| 95 | if (err?.status !== 400) { |
| 96 | throw err; |
| 97 | } |
| 98 | incompatible = errors.getIncompatibleSchemaError(err); |
| 99 | const invalidConnectors = errors.getInvalidConnectors(err); |
| 100 | if (!incompatible && !invalidConnectors.length) { |
| 101 | // If we got a different type of error, throw it |
| 102 | const gqlErrs = errors.getGQLErrors(err); |
| 103 | if (gqlErrs) { |
| 104 | throw new FirebaseError(`There are errors in your schema files:\n${gqlErrs}`); |
| 105 | } |
| 106 | throw err; |
| 107 | } |
| 108 | // Display failed precondition errors nicely. |
| 109 | if (invalidConnectors.length) { |
| 110 | displayInvalidConnectors(invalidConnectors); |
| 111 | } |
| 112 | } |
| 113 | if (!incompatible) { |
| 114 | return []; |
| 115 | } |
| 116 | // If the schema validation mode is unset, we diff in strict mode first, then diff in compatible if there are any diffs. |
| 117 | // It should display both COMPATIBLE and STRICT mode diffs in this order. |
| 118 | if (schemaValidation) { |
| 119 | displaySchemaChanges(incompatible, validationMode); |
| 120 | return incompatible.diffs; |
| 121 | } |
| 122 | const strictIncompatible = incompatible; |
| 123 | let compatibleIncompatible: IncompatibleSqlSchemaError | undefined = undefined; |
| 124 | validationMode = "COMPATIBLE"; |
| 125 | setSchemaValidationMode(schema, validationMode); |
| 126 | try { |
| 127 | displayStartSchemaDiff(validationMode); |
| 128 | await upsertSchema(schema, /** validateOnly=*/ true); |
no test coverage detected
searching dependent graphs…