Skip to content

Commit ddb40f5

Browse files
authored
sql:migrate --force shouldn't prompt (#7208)
1 parent 649da13 commit ddb40f5

File tree

4 files changed

+33
-33
lines changed

4 files changed

+33
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fixes an issue where dataconnect:sql:migrate still prompts for confirmation even with `--force`. (#7208)

src/commands/dataconnect-sql-migrate.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export const command = new Command("dataconnect:sql:migrate [serviceId]")
3434
const diffs = await migrateSchema({
3535
options,
3636
schema: serviceInfo.schema,
37-
allowNonInteractiveMigration: true,
3837
validateOnly: true,
3938
});
4039
if (diffs.length) {

src/dataconnect/schemaMigration.ts

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ export async function diffSchema(schema: Schema): Promise<Diff[]> {
5050
export async function migrateSchema(args: {
5151
options: Options;
5252
schema: Schema;
53-
allowNonInteractiveMigration: boolean;
53+
/** true for `dataconnect:sql:migrate`, false for `deploy` */
5454
validateOnly: boolean;
5555
}): Promise<Diff[]> {
56-
const { options, schema, allowNonInteractiveMigration, validateOnly } = args;
56+
const { options, schema, validateOnly } = args;
5757

5858
const { serviceName, instanceId, instanceName, databaseId } = getIdentifiers(schema);
5959
await ensureServiceIsConnectedToCloudSql(
@@ -77,26 +77,20 @@ export async function migrateSchema(args: {
7777
throw err;
7878
}
7979

80+
const migrationMode = await promptForSchemaMigration(
81+
options,
82+
databaseId,
83+
incompatible,
84+
validateOnly,
85+
);
86+
8087
const shouldDeleteInvalidConnectors = await promptForInvalidConnectorError(
8188
options,
8289
serviceName,
8390
invalidConnectors,
8491
validateOnly,
8592
);
8693

87-
const migrationMode = incompatible
88-
? await promptForSchemaMigration(
89-
options,
90-
databaseId,
91-
incompatible,
92-
allowNonInteractiveMigration,
93-
)
94-
: "none";
95-
// First, error out if we aren't making all changes
96-
if (migrationMode === "none" && incompatible) {
97-
throw new FirebaseError("Command aborted.");
98-
}
99-
10094
let diffs: Diff[] = [];
10195
if (incompatible) {
10296
diffs = await handleIncompatibleSchemaError({
@@ -205,44 +199,51 @@ async function handleIncompatibleSchemaError(args: {
205199
async function promptForSchemaMigration(
206200
options: Options,
207201
databaseName: string,
208-
err: IncompatibleSqlSchemaError,
209-
allowNonInteractiveMigration: boolean,
210-
): Promise<"none" | "safe" | "all"> {
202+
err: IncompatibleSqlSchemaError | undefined,
203+
validateOnly: boolean,
204+
): Promise<"none" | "all"> {
205+
if (!err) {
206+
return "none";
207+
}
211208
displaySchemaChanges(err);
212209
if (!options.nonInteractive) {
213-
// Always prompt in interactive mode. Destructive migrations are too potentially dangerous to not prompt for with --force
210+
if (validateOnly && options.force) {
211+
// `firebase dataconnect:sql:migrate --force` performs all migrations
212+
return "all";
213+
}
214+
// `firebase deploy` and `firebase dataconnect:sql:migrate` always prompt for any SQL migration changes.
215+
// Destructive migrations are too potentially dangerous to not prompt for with --force
214216
const choices = err.destructive
215217
? [
216218
{ name: "Execute all changes (including destructive changes)", value: "all" },
217219
{ name: "Abort changes", value: "none" },
218220
]
219221
: [
220-
{ name: "Execute changes", value: "safe" },
222+
{ name: "Execute changes", value: "all" },
221223
{ name: "Abort changes", value: "none" },
222224
];
223225
return await promptOnce({
224226
message: `Would you like to execute these changes against ${databaseName}?`,
225227
type: "list",
226228
choices,
227229
});
228-
} else if (!allowNonInteractiveMigration) {
229-
// `deploy --nonInteractive` performs no migrations
230-
logger.error(
231-
"Your database schema is incompatible with your Data Connect schema. Run `firebase dataconnect:sql:migrate` to migrate your database schema",
230+
}
231+
if (!validateOnly) {
232+
// `firebase deploy --nonInteractive` performs no migrations
233+
throw new FirebaseError(
234+
"Command aborted. Your database schema is incompatible with your Data Connect schema. Run `firebase dataconnect:sql:migrate` to migrate your database schema",
232235
);
233-
return "none";
234236
} else if (options.force) {
235-
// `dataconnect:sql:migrate --nonInteractive --force` performs all migrations
237+
// `dataconnect:sql:migrate --nonInteractive --force` performs all migrations.
236238
return "all";
237239
} else if (!err.destructive) {
238-
// `dataconnect:sql:migrate --nonInteractive` performs only safe migrations
239-
return "safe";
240+
// `dataconnect:sql:migrate --nonInteractive` performs only non-destructive migrations.
241+
return "all";
240242
} else {
241243
// `dataconnect:sql:migrate --nonInteractive` errors out if there are destructive migrations
242-
logger.error(
243-
"This schema migration includes potentially destructive changes. If you'd like to execute it anyway, rerun this command with --force",
244+
throw new FirebaseError(
245+
"Command aborted. This schema migration includes potentially destructive changes. If you'd like to execute it anyway, rerun this command with --force",
244246
);
245-
return "none";
246247
}
247248
}
248249

src/deploy/dataconnect/release.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export default async function (
4444
await migrateSchema({
4545
options,
4646
schema: s,
47-
allowNonInteractiveMigration: false,
4847
validateOnly: false,
4948
});
5049
}

0 commit comments

Comments
 (0)