Skip to content

Commit 6e4e345

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

File tree

2 files changed

+53
-36
lines changed

2 files changed

+53
-36
lines changed

drizzle-kit/src/cli/commands/generate-sqlite.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export const handle = async (config: GenerateConfig) => {
2020
schemaPath,
2121
casing,
2222
);
23-
2423
if (config.custom) {
2524
writeResult({
2625
snapshot: custom,
@@ -36,7 +35,6 @@ export const handle = async (config: GenerateConfig) => {
3635
});
3736
return;
3837
}
39-
4038
const { sqlStatements, warnings, renames } = await ddlDiff(
4139
ddlPrev,
4240
ddlCur,
@@ -45,6 +43,12 @@ export const handle = async (config: GenerateConfig) => {
4543
'default',
4644
);
4745

46+
// for (const { jsonStatement } of groupedStatements) {
47+
// const msg = sqliteExplain(jsonStatement);
48+
// console.log(msg?.title);
49+
// console.log(msg?.cause);
50+
// }
51+
4852
for (const w of warnings) {
4953
warning(w);
5054
}

drizzle-kit/src/cli/views.ts

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,8 @@ export const sqliteExplain = (
636636

637637
if (checksAlters.length) {
638638
blocks.push([
639-
`│ Check constraints altered definition: ${checksAlters.map((it) => `${it.name}`).join(', ')}\n`,
639+
`│ Check constraints altered definition:\n`,
640+
`│ ${checksAlters.map((it) => `${it.name}: ${it.$left.value} -> ${it.$right.value}`).join(',\n')}\n`,
640641
`│ It is not possible to alter definition\n`,
641642
]);
642643
}
@@ -650,23 +651,27 @@ export const sqliteExplain = (
650651

651652
res += `│ Columns altered:\n`;
652653
if (alteredNotNull.length) {
653-
res += `│ notNull: ${
654-
alteredNotNull.map((it) => `${it.name}: ${it.notNull?.from} -> ${it.notNull?.to}`).join('; ')
654+
res += `${
655+
alteredNotNull.map((it) => `│ ${it.name} => notNull: ${it.notNull?.from} -> ${it.notNull?.to}`).join(
656+
'\n',
657+
)
655658
}\n`;
656659
}
657660
if (alteredType.length) {
658-
res += `│ type: ${alteredType.map((it) => `${it.name}: ${it.type?.from} -> ${it.type?.to}`).join('; ')}\n`;
661+
res += `${alteredType.map((it) => `${it.name} => type: ${it.type?.from} -> ${it.type?.to}`).join('\n')}\n`;
659662
}
660663
if (alteredDefault.length) {
661-
res += `│ default: ${
662-
alteredDefault.map((it) => `${it.name}: ${it.default?.from} -> ${it.default?.to}`).join('; ')
664+
res += `${
665+
alteredDefault.map((it) => `│ ${it.name} => default: ${it.default?.from} -> ${it.default?.to}`).join(
666+
'\n',
667+
)
663668
}\n`;
664669
}
665670
if (alteredAutoincrement.length) {
666-
res += `│ autoincrement: ${
667-
alteredAutoincrement.map((it) => `${it.name}: ${it.autoincrement?.from} -> ${it.autoincrement?.to}`).join(
668-
'; ',
669-
)
671+
res += `${
672+
alteredAutoincrement.map((it) =>
673+
`│ ${it.name} => autoincrement: ${it.autoincrement?.from} -> ${it.autoincrement?.to}`
674+
).join('\n')
670675
}\n`;
671676
}
672677

@@ -675,28 +680,32 @@ export const sqliteExplain = (
675680

676681
if (uniquesDiff.length) {
677682
let res: string = '';
678-
res += `| Unique constraints added: ${
679-
uniquesDiff.filter((it) => it.$diffType === 'create').map((it) => `${it.name}`).join(', ')
680-
}\n`;
681-
res += `| Unique constraints dropped: ${
682-
uniquesDiff.filter((it) => it.$diffType === 'drop').map((it) => `${it.name}`).join(', ')
683-
}\n`;
684683

685-
res += `| It is not possible to create/drop unique constraints on existing table\n`;
684+
const uniquesCreated = uniquesDiff.filter((it) => it.$diffType === 'create');
685+
const uniquesDropped = uniquesDiff.filter((it) => it.$diffType === 'drop');
686+
if (uniquesCreated.length) {
687+
res += `│ Unique constraints added: ${uniquesCreated.map((it) => `${it.name}`).join(', ')}\n`;
688+
}
689+
if (uniquesDropped.length) {
690+
res += `│ Unique constraints dropped: ${uniquesDropped.map((it) => `${it.name}`).join(', ')}\n`;
691+
}
692+
693+
res += `│ It is not possible to create/drop unique constraints on existing table\n`;
686694

687695
blocks.push([res]);
688696
}
689697

690698
if (pksDiff.length) {
691699
let res: string = '';
692-
res += `| Primary key constraints added: ${
693-
pksDiff.filter((it) => it.$diffType === 'create').map((it) => `${it.name}`).join(', ')
694-
}\n`;
695-
res += `| Primary key constraints dropped: ${
696-
pksDiff.filter((it) => it.$diffType === 'drop').map((it) => `${it.name}`).join(', ')
697-
}\n`;
698-
699-
res += `| It is not possible to create/drop primary key constraints on existing table\n`;
700+
const pksCreated = pksDiff.filter((it) => it.$diffType === 'create');
701+
const pksDropped = pksDiff.filter((it) => it.$diffType === 'drop');
702+
703+
if (pksCreated.length) {
704+
res += `│ Primary key constraints added: ${pksCreated.map((it) => `${it.name}`).join(', ')}\n`;
705+
}
706+
if (pksDropped) res += `│ Primary key constraints dropped: ${pksDropped.map((it) => `${it.name}`).join(', ')}\n`;
707+
708+
res += `│ It is not possible to create/drop primary key constraints on existing table\n`;
700709
blocks.push([res]);
701710
}
702711

@@ -712,7 +721,7 @@ export const sqliteExplain = (
712721
`│ Primary key was altered:\n`,
713722
`│ columns: ${
714723
pksAlters.filter((it) => it.columns).map((it) =>
715-
`${it.name}, [${it.columns?.from.join(',')}] -> [${it.columns?.to.join(',')}]`
724+
`${it.name}: [${it.columns?.from.join(',')}] -> [${it.columns?.to.join(',')}]\n`
716725
)
717726
}\n`,
718727
]);
@@ -723,7 +732,7 @@ export const sqliteExplain = (
723732
`│ Unique constraint was altered:\n`,
724733
`│ columns: ${
725734
uniquesAlters.filter((it) => it.columns).map((it) =>
726-
`${it.name}, [${it.columns?.from.join(',')}] -> [${it.columns?.to.join(',')}]`
735+
`${it.name}, [${it.columns?.from.join(',')}] -> [${it.columns?.to.join(',')}]\n`
727736
)
728737
}\n`,
729738
]);
@@ -738,20 +747,22 @@ export const sqliteExplain = (
738747

739748
res += `│ Foreign key constraint was altered:\n`;
740749
if (columnsAltered) {
741-
res += `│ columns: ${
742-
columnsAltered.map((it) => `${it.name}, [${it.columns?.from.join(',')}] -> [${it.columns?.to.join(',')}]`)
750+
res += `${
751+
columnsAltered.map((it) =>
752+
`│ ${it.name} => columns: [${it.columns?.from.join(',')}] -> [${it.columns?.to.join(',')}]`
753+
)
743754
}\n`;
744755
}
745756
if (columnsToAltered) {
746-
res += `│ columnTos: ${
757+
res += ` ${
747758
columnsToAltered.map((it) =>
748-
`${it.name}, [${it.columnsTo?.from.join(',')}] -> [${it.columnsTo?.to.join(',')}]`
759+
`${it.name} => columnsTo: [${it.columnsTo?.from.join(',')}] -> [${it.columnsTo?.to.join(',')}]`
749760
)
750761
}\n`;
751762
}
752763
if (tablesToAltered) {
753-
res += `│ tableTo: ${
754-
tablesToAltered.map((it) => `${it.name}, [${it.tableTo?.from}] -> [${it.tableTo?.to}]`)
764+
res += `${
765+
tablesToAltered.map((it) => `${it.name} => tableTo: [${it.tableTo?.from}] -> [${it.tableTo?.to}]`)
755766
}\n`;
756767
}
757768

@@ -785,7 +796,9 @@ export const sqliteExplain = (
785796
blocks.push([res]);
786797
}
787798

788-
cause += blocks.map((it) => it.join('')).join('|-\n');
799+
if (blocks.filter((it) => Boolean(it))) {
800+
cause += blocks.map((it) => it.join('')).join('|-\n');
801+
}
789802
}
790803

791804
if (st.type === 'recreate_column') {

0 commit comments

Comments
 (0)