Skip to content

Commit a0f88e0

Browse files
committed
add safe option for ci usage
1 parent 6ab1bbe commit a0f88e0

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

Diff for: drizzle-kit/src/cli/commands/push.ts

+45
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export const mysqlPush = async (
3232
verbose: boolean,
3333
force: boolean,
3434
casing: CasingType | undefined,
35+
safe: boolean,
3536
) => {
3637
const { connectToMySQL } = await import('../connections');
3738
const { mysqlPushIntrospect } = await import('./mysqlIntrospect');
@@ -95,6 +96,14 @@ export const mysqlPush = async (
9596
console.log();
9697
}
9798

99+
if (safe && shouldAskForApprove) {
100+
console.log(withStyle.warning('Found data-loss statements:'));
101+
console.log(infoToPrint.join('\n'));
102+
console.log();
103+
console.log(chalk.red('Safe mode is enabled - aborting'));
104+
process.exit(1);
105+
}
106+
98107
if (!force && strict) {
99108
if (!shouldAskForApprove) {
100109
const { status, data } = await render(
@@ -171,6 +180,7 @@ export const singlestorePush = async (
171180
verbose: boolean,
172181
force: boolean,
173182
casing: CasingType | undefined,
183+
safe: boolean,
174184
) => {
175185
const { connectToSingleStore } = await import('../connections');
176186
const { singlestorePushIntrospect } = await import('./singlestoreIntrospect');
@@ -221,6 +231,14 @@ export const singlestorePush = async (
221231
console.log();
222232
}
223233

234+
if (safe && shouldAskForApprove) {
235+
console.log(withStyle.warning('Found data-loss statements:'));
236+
console.log(infoToPrint.join('\n'));
237+
console.log();
238+
console.log(chalk.red('Safe mode is enabled - aborting'));
239+
process.exit(1);
240+
}
241+
224242
if (!force && strict) {
225243
if (!shouldAskForApprove) {
226244
const { status, data } = await render(
@@ -296,6 +314,7 @@ export const pgPush = async (
296314
entities: Entities,
297315
force: boolean,
298316
casing: CasingType | undefined,
317+
safe: boolean,
299318
) => {
300319
const { preparePostgresDB } = await import('../connections');
301320
const { pgPushIntrospect } = await import('./pgIntrospect');
@@ -339,6 +358,14 @@ export const pgPush = async (
339358
console.log();
340359
}
341360

361+
if (safe && shouldAskForApprove) {
362+
console.log(withStyle.warning('Found data-loss statements:'));
363+
console.log(infoToPrint.join('\n'));
364+
console.log();
365+
console.log(chalk.red('Safe mode is enabled - aborting'));
366+
process.exit(1);
367+
}
368+
342369
if (!force && strict) {
343370
if (!shouldAskForApprove) {
344371
const { status, data } = await render(
@@ -418,6 +445,7 @@ export const sqlitePush = async (
418445
tablesFilter: string[],
419446
force: boolean,
420447
casing: CasingType | undefined,
448+
safe: boolean,
421449
) => {
422450
const { connectToSQLite } = await import('../connections');
423451
const { sqlitePushIntrospect } = await import('./sqliteIntrospect');
@@ -447,6 +475,14 @@ export const sqlitePush = async (
447475
statements.meta!,
448476
);
449477

478+
if (safe && shouldAskForApprove) {
479+
console.log(withStyle.warning('Found data-loss statements:'));
480+
console.log(infoToPrint.join('\n'));
481+
console.log();
482+
console.log(chalk.red('Safe mode is enabled - aborting'));
483+
process.exit(1);
484+
}
485+
450486
if (verbose && statementsToExecute.length > 0) {
451487
console.log();
452488
console.log(
@@ -539,6 +575,7 @@ export const libSQLPush = async (
539575
tablesFilter: string[],
540576
force: boolean,
541577
casing: CasingType | undefined,
578+
safe: boolean,
542579
) => {
543580
const { connectToLibSQL } = await import('../connections');
544581
const { sqlitePushIntrospect } = await import('./sqliteIntrospect');
@@ -568,6 +605,14 @@ export const libSQLPush = async (
568605
statements.meta!,
569606
);
570607

608+
if (safe && shouldAskForApprove) {
609+
console.log(withStyle.warning('Found data-loss statements:'));
610+
console.log(infoToPrint.join('\n'));
611+
console.log();
612+
console.log(chalk.red('Safe mode is enabled - aborting'));
613+
process.exit(1);
614+
}
615+
571616
if (verbose && statementsToExecute.length > 0) {
572617
console.log();
573618
console.log(

Diff for: drizzle-kit/src/cli/commands/utils.ts

+6
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ export const preparePushConfig = async (
291291
verbose: boolean;
292292
strict: boolean;
293293
force: boolean;
294+
safe: boolean;
294295
tablesFilter: string[];
295296
schemasFilter: string[];
296297
casing?: CasingType;
@@ -353,6 +354,7 @@ export const preparePushConfig = async (
353354
strict: config.strict ?? false,
354355
verbose: config.verbose ?? false,
355356
force: (options.force as boolean) ?? false,
357+
safe: (options.safe as boolean) ?? false,
356358
credentials: parsed.data,
357359
casing: config.casing,
358360
tablesFilter,
@@ -373,6 +375,7 @@ export const preparePushConfig = async (
373375
strict: config.strict ?? false,
374376
verbose: config.verbose ?? false,
375377
force: (options.force as boolean) ?? false,
378+
safe: (options.safe as boolean) ?? false,
376379
credentials: parsed.data,
377380
casing: config.casing,
378381
tablesFilter,
@@ -393,6 +396,7 @@ export const preparePushConfig = async (
393396
strict: config.strict ?? false,
394397
verbose: config.verbose ?? false,
395398
force: (options.force as boolean) ?? false,
399+
safe: (options.safe as boolean) ?? false,
396400
credentials: parsed.data,
397401
tablesFilter,
398402
schemasFilter,
@@ -411,6 +415,7 @@ export const preparePushConfig = async (
411415
strict: config.strict ?? false,
412416
verbose: config.verbose ?? false,
413417
force: (options.force as boolean) ?? false,
418+
safe: (options.safe as boolean) ?? false,
414419
credentials: parsed.data,
415420
casing: config.casing,
416421
tablesFilter,
@@ -430,6 +435,7 @@ export const preparePushConfig = async (
430435
strict: config.strict ?? false,
431436
verbose: config.verbose ?? false,
432437
force: (options.force as boolean) ?? false,
438+
safe: (options.safe as boolean) ?? false,
433439
credentials: parsed.data,
434440
casing: config.casing,
435441
tablesFilter,

Diff for: drizzle-kit/src/cli/schema.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ export const push = command({
257257
.desc('Print all statements for each push')
258258
.default(false),
259259
strict: boolean().desc('Always ask for confirmation').default(false),
260+
safe: boolean().desc('Only run non-data-loss statements, aborting if not possible').default(false),
260261
force: boolean()
261262
.desc(
262263
'Auto-approve all data loss statements. Note: Data loss statements may truncate your tables and data',
@@ -267,7 +268,7 @@ export const push = command({
267268
const from = assertCollisions(
268269
'push',
269270
opts,
270-
['force', 'verbose', 'strict'],
271+
['force', 'verbose', 'strict', 'safe'],
271272
[
272273
'schema',
273274
'dialect',
@@ -305,6 +306,7 @@ export const push = command({
305306
force,
306307
casing,
307308
entities,
309+
safe,
308310
} = config;
309311

310312
try {
@@ -318,6 +320,7 @@ export const push = command({
318320
verbose,
319321
force,
320322
casing,
323+
safe,
321324
);
322325
} else if (dialect === 'postgresql') {
323326
if ('driver' in credentials) {
@@ -352,6 +355,7 @@ export const push = command({
352355
entities,
353356
force,
354357
casing,
358+
safe,
355359
);
356360
} else if (dialect === 'sqlite') {
357361
const { sqlitePush } = await import('./commands/push');
@@ -363,6 +367,7 @@ export const push = command({
363367
tablesFilter,
364368
force,
365369
casing,
370+
safe,
366371
);
367372
} else if (dialect === 'turso') {
368373
const { libSQLPush } = await import('./commands/push');
@@ -374,6 +379,7 @@ export const push = command({
374379
tablesFilter,
375380
force,
376381
casing,
382+
safe,
377383
);
378384
} else if (dialect === 'singlestore') {
379385
const { singlestorePush } = await import('./commands/push');
@@ -385,6 +391,7 @@ export const push = command({
385391
verbose,
386392
force,
387393
casing,
394+
safe,
388395
);
389396
} else if (dialect === 'gel') {
390397
console.log(

Diff for: drizzle-kit/tests/cli-push.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ test('push #1', async (t) => {
2222
url: 'postgresql://postgres:[email protected]:5432/db',
2323
},
2424
force: false,
25+
safe: false,
2526
schemaPath: './schema.ts',
2627
schemasFilter: ['public'],
2728
tablesFilter: [],
@@ -42,6 +43,7 @@ test('push #2', async (t) => {
4243
url: 'turso.dev',
4344
},
4445
force: false,
46+
safe: false,
4547
schemaPath: './schema.ts',
4648
schemasFilter: ['public'],
4749
tablesFilter: [],
@@ -63,6 +65,7 @@ test('push #3', async (t) => {
6365
token: 'token',
6466
},
6567
force: false,
68+
safe: false,
6669
schemaPath: './schema.ts',
6770
schemasFilter: ['public'],
6871
tablesFilter: [],
@@ -85,6 +88,7 @@ test('push #4', async (t) => {
8588
user: 'postgresql',
8689
},
8790
force: false,
91+
safe: false,
8892
schemaPath: './schema.ts',
8993
schemasFilter: ['public'],
9094
tablesFilter: [],
@@ -114,6 +118,7 @@ test('push #5', async (t) => {
114118
strict: false,
115119
entities: undefined,
116120
force: false,
121+
safe: false,
117122
verbose: false,
118123
casing: undefined,
119124
});

0 commit comments

Comments
 (0)