MCPcopy Create free account
hub / github.com/firebase/firebase-tools / handleIncompatibleSchemaError

Function handleIncompatibleSchemaError

src/dataconnect/schemaMigration.ts:454–540  ·  view source on GitHub ↗
(args: {
  incompatibleSchemaError: IncompatibleSqlSchemaError;
  options: Options;
  instanceId: string;
  databaseId: string;
  schemaName: string;
  choice: "all" | "safe" | "none";
})

Source from the content-addressed store, hash-verified

452}
453
454async function handleIncompatibleSchemaError(args: {
455 incompatibleSchemaError: IncompatibleSqlSchemaError;
456 options: Options;
457 instanceId: string;
458 databaseId: string;
459 schemaName: string;
460 choice: "all" | "safe" | "none";
461}): Promise<Diff[]> {
462 const { incompatibleSchemaError, options, instanceId, databaseId, schemaName, choice } = args;
463 const commandsToExecute = incompatibleSchemaError.diffs.filter((d) => {
464 switch (choice) {
465 case "all":
466 return true;
467 case "safe":
468 return !d.destructive;
469 case "none":
470 return false;
471 }
472 });
473 if (commandsToExecute.length) {
474 const commandsToExecuteBySuperUser = commandsToExecute.filter(requireSuperUser);
475 const commandsToExecuteByOwner = commandsToExecute.filter((sql) => !requireSuperUser(sql));
476
477 const userIsCSQLAdmin = await iamUserIsCSQLAdmin(options);
478
479 if (!userIsCSQLAdmin && commandsToExecuteBySuperUser.length) {
480 throw new FirebaseError(`Some SQL commands required for this migration require Admin permissions.\n
481 Please ask a user with 'roles/cloudsql.admin' to apply the following commands.\n
482 ${diffsToString(commandsToExecuteBySuperUser)}`);
483 }
484
485 const schemaInfo = await getSchemaMetadata(instanceId, databaseId, schemaName, options);
486 if (schemaInfo.setupStatus !== SchemaSetupStatus.GreenField) {
487 throw new FirebaseError(
488 `Brownfield database are protected from SQL changes by SQL Connect.\n` +
489 `You can use the SQL diff generated by 'firebase dataconnect:sql:diff' to assist you in applying the required changes to your CloudSQL database. Connector deployment will succeed when there is no required diff changes.\n` +
490 `If you would like SQL Connect to manage your database schema, run 'firebase dataconnect:sql:setup'`,
491 );
492 }
493
494 // Test if iam user has access to the roles required for this migration
495 if (
496 !(await checkSQLRoleIsGranted(
497 options,
498 instanceId,
499 databaseId,
500 firebaseowner(databaseId, schemaName),
501 (await getIAMUser(options)).user,
502 ))
503 ) {
504 if (!userIsCSQLAdmin) {
505 throw new FirebaseError(
506 `Command aborted. Only users granted firebaseowner SQL role can run migrations.`,
507 );
508 }
509 const account = (await requireAuth(options))!;
510 logLabeledBullet("dataconnect", `Granting firebaseowner role to myself ${account}...`);
511 await grantRoleTo(options, instanceId, databaseId, "owner", account, schemaName);

Callers 1

migrateSchemaFunction · 0.85

Calls 12

iamUserIsCSQLAdminFunction · 0.90
getSchemaMetadataFunction · 0.90
checkSQLRoleIsGrantedFunction · 0.90
firebaseownerFunction · 0.90
getIAMUserFunction · 0.90
requireAuthFunction · 0.90
logLabeledBulletFunction · 0.90
grantRoleToFunction · 0.90
executeSqlCmdsAsIamUserFunction · 0.90
requireSuperUserFunction · 0.85
diffsToStringFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…