Skip to content

Commit 7253e03

Browse files
[sqlite]: explain
1 parent fd79c4c commit 7253e03

File tree

2 files changed

+150
-80
lines changed

2 files changed

+150
-80
lines changed

drizzle-kit/src/cli/views.ts

Lines changed: 147 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -607,133 +607,200 @@ export const sqliteExplain = (
607607
const key = `${to.name}`;
608608

609609
title += `${key} table recreated:\n`;
610+
611+
const blocks: string[][] = [];
612+
610613
if (alteredColumnsBecameGenerated.length) {
611-
cause += `│ Columns become generated: ${alteredColumnsBecameGenerated.map((it) => `${it.name}`).join('\n`')}\n`;
612-
cause += `│ It is not possible to make existing column as generated\n`;
614+
blocks.push([
615+
`│ Columns become generated stored: ${alteredColumnsBecameGenerated.map((it) => `${it.name}`).join(', ')}\n`,
616+
`│ It is not possible to make existing column as generated STORED\n`,
617+
]);
613618
}
614619

615620
if (checkDiffs.length) {
616-
cause += `| Check constraints added: ${
617-
checkDiffs.filter((it) => it.$diffType === 'create').map((it) => `${it.name}`).join(',')
618-
}\n`;
619-
cause += `| Check constraints dropped: ${
620-
checkDiffs.filter((it) => it.$diffType === 'drop').map((it) => `${it.name}`).join(',')
621-
}\n`;
621+
let res: string = '';
622+
const createdChecks = checkDiffs.filter((it) => it.$diffType === 'create');
623+
const droppedChecks = checkDiffs.filter((it) => it.$diffType === 'drop');
624+
625+
if (createdChecks.length) {
626+
res += `| Check constraints added: ${createdChecks.map((it) => `${it.name}`).join(', ')}\n`;
627+
}
628+
629+
if (droppedChecks) {
630+
res += `| Check constraints dropped: ${droppedChecks.map((it) => `${it.name}`).join(', ')}\n`;
631+
}
622632

623-
cause += `| It is not possible to create/drop check constraints on existing table\n`;
633+
res += `| It is not possible to create/drop check constraints on existing table\n`;
634+
blocks.push([res]);
624635
}
625636

626637
if (checksAlters.length) {
627-
cause += `│ Check constraints altered definition: ${checksAlters.map((it) => `${it.name}`).join(',')}\n`;
628-
cause += `│ It is not possible to alter definition\n`;
638+
blocks.push([
639+
`│ Check constraints altered definition: ${checksAlters.map((it) => `${it.name}`).join(', ')}\n`,
640+
`│ It is not possible to alter definition\n`,
641+
]);
629642
}
630643

631-
if (columnAlters) {
632-
cause += `│ Columns altered:\n`;
633-
cause += `│ notNull: ${
634-
columnAlters.filter((it) => it.notNull).map((it) => `${it.name}, ${it.notNull?.from} -> ${it.notNull?.to}`)
635-
.join(', ')
636-
}\n`;
637-
cause += `│ type: ${
638-
columnAlters.filter((it) => it.type).map((it) => `${it.name}, ${it.type?.from} -> ${it.type?.to}`)
639-
.join(', ')
640-
}\n`;
641-
cause += `│ default: ${
642-
columnAlters.filter((it) => it.default).map((it) => `${it.name}, ${it.default?.from} -> ${it.default?.to}`)
643-
.join(', ')
644-
}\n`;
645-
cause += `│ autoincrement: ${
646-
columnAlters.filter((it) => it.autoincrement).map((it) =>
647-
`${it.name}, ${it.autoincrement?.from} -> ${it.autoincrement?.to}`
648-
)
649-
.join(', ')
650-
}\n`;
644+
if (columnAlters.filter((it) => it.type || it.default || it.autoincrement || it.notNull).length) {
645+
let res: string = '';
646+
const alteredNotNull = columnAlters.filter((it) => it.notNull);
647+
const alteredType = columnAlters.filter((it) => it.type);
648+
const alteredDefault = columnAlters.filter((it) => it.default);
649+
const alteredAutoincrement = columnAlters.filter((it) => it.autoincrement);
650+
651+
res += `│ Columns altered:\n`;
652+
if (alteredNotNull.length) {
653+
res += `│ notNull: ${
654+
alteredNotNull.map((it) => `${it.name}: ${it.notNull?.from} -> ${it.notNull?.to}`).join('; ')
655+
}\n`;
656+
}
657+
if (alteredType.length) {
658+
res += `│ type: ${alteredType.map((it) => `${it.name}: ${it.type?.from} -> ${it.type?.to}`).join('; ')}\n`;
659+
}
660+
if (alteredDefault.length) {
661+
res += `│ default: ${
662+
alteredDefault.map((it) => `${it.name}: ${it.default?.from} -> ${it.default?.to}`).join('; ')
663+
}\n`;
664+
}
665+
if (alteredAutoincrement.length) {
666+
res += `│ autoincrement: ${
667+
alteredAutoincrement.map((it) => `${it.name}: ${it.autoincrement?.from} -> ${it.autoincrement?.to}`).join(
668+
'; ',
669+
)
670+
}\n`;
671+
}
672+
673+
blocks.push([res]);
651674
}
652675

653676
if (uniquesDiff.length) {
654-
cause += `| Unique constraints added: ${
677+
let res: string = '';
678+
res += `| Unique constraints added: ${
655679
uniquesDiff.filter((it) => it.$diffType === 'create').map((it) => `${it.name}`).join(', ')
656680
}\n`;
657-
cause += `| Unique constraints dropped: ${
681+
res += `| Unique constraints dropped: ${
658682
uniquesDiff.filter((it) => it.$diffType === 'drop').map((it) => `${it.name}`).join(', ')
659683
}\n`;
660684

661-
cause += `| It is not possible to create/drop unique constraints on existing table\n`;
685+
res += `| It is not possible to create/drop unique constraints on existing table\n`;
686+
687+
blocks.push([res]);
662688
}
663689

664690
if (pksDiff.length) {
665-
cause += `| Primary key constraints added: ${
691+
let res: string = '';
692+
res += `| Primary key constraints added: ${
666693
pksDiff.filter((it) => it.$diffType === 'create').map((it) => `${it.name}`).join(', ')
667694
}\n`;
668-
cause += `| Primary key constraints dropped: ${
695+
res += `| Primary key constraints dropped: ${
669696
pksDiff.filter((it) => it.$diffType === 'drop').map((it) => `${it.name}`).join(', ')
670697
}\n`;
671698

672-
cause += `| It is not possible to create/drop primary key constraints on existing table\n`;
699+
res += `| It is not possible to create/drop primary key constraints on existing table\n`;
700+
blocks.push([res]);
673701
}
674702

675703
if (newStoredColumns.length) {
676-
cause += `| Stored columns added: ${newStoredColumns.map((it) => `${it.name}`).join(', ')}\n`;
677-
cause +=
678-
`| It is not possible to ALTER TABLE ADD COLUMN a STORED column. One can add a VIRTUAL column, however\n`;
704+
blocks.push([
705+
`| Stored columns added: ${newStoredColumns.map((it) => `${it.name}`).join(', ')}\n`,
706+
`| It is not possible to ALTER TABLE ADD COLUMN a STORED column. One can add a VIRTUAL column, however\n`,
707+
]);
679708
}
680709

681710
if (pksAlters.length) {
682-
cause += `│ Primary key was altered:\n`;
683-
cause += `│ columns: ${
684-
pksAlters.filter((it) => it.columns).map((it) =>
685-
`${it.name}, [${it.columns?.from.join(',')}] -> [${it.columns?.to.join(',')}]`
686-
)
687-
}\n`;
711+
blocks.push([
712+
`│ Primary key was altered:\n`,
713+
`│ columns: ${
714+
pksAlters.filter((it) => it.columns).map((it) =>
715+
`${it.name}, [${it.columns?.from.join(',')}] -> [${it.columns?.to.join(',')}]`
716+
)
717+
}\n`,
718+
]);
688719
}
689720

690721
if (uniquesAlters.length) {
691-
cause += `│ Unique constraint was altered:\n`;
692-
cause += `│ columns: ${
693-
uniquesAlters.filter((it) => it.columns).map((it) =>
694-
`${it.name}, [${it.columns?.from.join(',')}] -> [${it.columns?.to.join(',')}]`
695-
)
696-
}\n`;
722+
blocks.push([
723+
`│ Unique constraint was altered:\n`,
724+
`│ columns: ${
725+
uniquesAlters.filter((it) => it.columns).map((it) =>
726+
`${it.name}, [${it.columns?.from.join(',')}] -> [${it.columns?.to.join(',')}]`
727+
)
728+
}\n`,
729+
]);
697730
}
698731

699732
if (fksAlters.length) {
700-
cause += `│ Foreign key constraint was altered:\n`;
701-
cause += `│ columns: ${
702-
fksAlters.filter((it) => it.columns).map((it) =>
703-
`${it.name}, [${it.columns?.from.join(',')}] -> [${it.columns?.to.join(',')}]`
704-
)
705-
}\n`;
706-
cause += `│ columnTos: ${
707-
fksAlters.filter((it) => it.columnsTo).map((it) =>
708-
`${it.name}, [${it.columnsTo?.from.join(',')}] -> [${it.columnsTo?.to.join(',')}]`
709-
)
710-
}\n`;
711-
cause += `│ tableTo: ${
712-
fksAlters.filter((it) => it.tableTo).map((it) => `${it.name}, [${it.tableTo?.from}] -> [${it.tableTo?.to}]`)
713-
}\n`;
733+
let res: string = '';
734+
735+
const columnsAltered = fksAlters.filter((it) => it.columns);
736+
const columnsToAltered = fksAlters.filter((it) => it.columnsTo);
737+
const tablesToAltered = fksAlters.filter((it) => it.tableTo);
738+
739+
res += `│ Foreign key constraint was altered:\n`;
740+
if (columnsAltered) {
741+
res += `│ columns: ${
742+
columnsAltered.map((it) => `${it.name}, [${it.columns?.from.join(',')}] -> [${it.columns?.to.join(',')}]`)
743+
}\n`;
744+
}
745+
if (columnsToAltered) {
746+
res += `│ columnTos: ${
747+
columnsToAltered.map((it) =>
748+
`${it.name}, [${it.columnsTo?.from.join(',')}] -> [${it.columnsTo?.to.join(',')}]`
749+
)
750+
}\n`;
751+
}
752+
if (tablesToAltered) {
753+
res += `│ tableTo: ${
754+
tablesToAltered.map((it) => `${it.name}, [${it.tableTo?.from}] -> [${it.tableTo?.to}]`)
755+
}\n`;
756+
}
757+
758+
blocks.push([res]);
714759
}
715760

716761
if (fksDiff.length) {
717-
cause += `| Foreign key constraints added: ${
718-
fksDiff.filter((it) => it.$diffType === 'create').map((it) => `${it.name}`).join(', ')
719-
}\n`;
720-
cause += `| Unique constraints dropped: ${
721-
fksDiff.filter((it) => it.$diffType === 'drop').map((it) => `${it.name}`).join(', ')
722-
}\n`;
762+
let res: string = '';
763+
764+
const fksCreated = fksDiff.filter((it) => it.$diffType === 'create');
765+
const fksDropped = fksDiff.filter((it) => it.$diffType === 'drop');
766+
if (fksCreated) res += `| Foreign key constraints added: ${fksCreated.map((it) => `${it.name}`).join(', ')}\n`;
767+
if (fksDropped) res += `| Unique constraints dropped: ${fksDropped.map((it) => `${it.name}`).join(', ')}\n`;
768+
769+
res += `| It is not possible to create/drop foreign key constraints on existing table\n`;
723770

724-
cause += `| It is not possible to create/drop foreign key constraints on existing table\n`;
771+
blocks.push([res]);
725772
}
726773

727774
if (indexesDiff.filter((it) => it.isUnique && it.origin === 'auto').length) {
728-
cause += `| System generated index added: ${
729-
fksDiff.filter((it) => it.$diffType === 'create').map((it) => `${it.name}`).join(', ')
730-
}\n`;
731-
cause += `| System generated index dropped: ${
732-
fksDiff.filter((it) => it.$diffType === 'drop').map((it) => `${it.name}`).join(', ')
733-
}\n`;
775+
let res: string = '';
776+
const indexCreated = indexesDiff.filter((it) => it.$diffType === 'create');
777+
const indexDropped = indexesDiff.filter((it) => it.$diffType === 'drop');
778+
if (indexCreated) res += `| System generated index added: ${indexCreated.map((it) => `${it.name}`).join(', ')}\n`;
779+
if (indexDropped) {
780+
res += `| System generated index dropped: ${indexDropped.map((it) => `${it.name}`).join(', ')}\n`;
781+
}
782+
783+
res += `| It is not possible to drop/create auto generated unique indexes\n`;
734784

735-
cause += `| It is not possible to drop/create auto generated unique indexes\n`;
785+
blocks.push([res]);
736786
}
787+
788+
cause += blocks.map((it) => it.join('')).join('|-\n');
789+
}
790+
791+
if (st.type === 'recreate_column') {
792+
const {
793+
column,
794+
diffGenerated,
795+
} = st;
796+
797+
const key = `${column.name}`;
798+
799+
title += `${key} column recreated:\n`;
800+
801+
cause += `| generated: ${
802+
diffGenerated && diffGenerated.from ? diffGenerated.from.as + ' ' + diffGenerated.from.type : 'null'
803+
} -> ${diffGenerated && diffGenerated.to ? diffGenerated.to.as + ' ' + diffGenerated.to.type : 'null'}`;
737804
}
738805

739806
if (title) return { title, cause };

drizzle-kit/src/dialects/sqlite/statements.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { DiffEntities } from '../cockroach/ddl';
12
import type { Column, DiffColumn, ForeignKey, Index, SQLiteDDL, TableFull, View } from './ddl';
23

34
export interface JsonCreateTableStatement {
@@ -67,6 +68,7 @@ export interface JsonRenameColumnStatement {
6768
export interface JsonRecreateColumnStatement {
6869
type: 'recreate_column';
6970
column: Column;
71+
diffGenerated: DiffEntities['columns']['generated'];
7072
fk: ForeignKey | null;
7173
}
7274

@@ -139,6 +141,7 @@ export const prepareRecreateColumn = (
139141
if (diffColumn.generated) {
140142
return {
141143
type: 'recreate_column',
144+
diffGenerated: diffColumn.generated,
142145
column: column,
143146
fk: fk,
144147
};

0 commit comments

Comments
 (0)