@@ -99,15 +99,19 @@ function formatOptionChanges(
9999}
100100
101101export const explain = (
102- dialect : 'mysql' | 'postgres ' ,
103- grouped : { jsonStatement : StatementPostgres ; sqlStatements : string [ ] } [ ] ,
102+ dialect : 'postgres' | ' mysql' | 'sqlite' | 'singlestore' | 'mssql' | 'common' | 'gel' | 'cockroach ',
103+ grouped : { jsonStatement : StatementPostgres | StatementSqlite ; sqlStatements : string [ ] } [ ] ,
104104 explain : boolean ,
105105 hints : { hint : string ; statement ?: string } [ ] ,
106106) => {
107107 const res = [ ] ;
108108 const explains = [ ] ;
109109 for ( const { jsonStatement, sqlStatements } of grouped ) {
110- const res = dialect === 'postgres' ? psqlExplain ( jsonStatement as StatementPostgres ) : null ;
110+ const res = dialect === 'postgres'
111+ ? psqlExplain ( jsonStatement as StatementPostgres )
112+ : dialect === 'sqlite'
113+ ? sqliteExplain ( jsonStatement as StatementSqlite )
114+ : null ;
111115
112116 if ( res ) {
113117 let msg = `┌─── ${ res . title } \n` ;
@@ -618,27 +622,22 @@ export const sqliteExplain = (
618622 }
619623
620624 if ( checkDiffs . length ) {
621- let res : string = '' ;
622625 const createdChecks = checkDiffs . filter ( ( it ) => it . $diffType === 'create' ) ;
623626 const droppedChecks = checkDiffs . filter ( ( it ) => it . $diffType === 'drop' ) ;
624627
625628 if ( createdChecks . length ) {
626- res += `| Check constraints added: ${ createdChecks . map ( ( it ) => `${ it . name } ` ) . join ( ', ' ) } \n` ;
629+ blocks . push ( [ `| Check constraints added: ${ createdChecks . map ( ( it ) => `${ it . name } ` ) . join ( ', ' ) } \n` ] ) ;
627630 }
628631
629- if ( droppedChecks ) {
630- res += `| Check constraints dropped: ${ droppedChecks . map ( ( it ) => `${ it . name } ` ) . join ( ', ' ) } \n` ;
632+ if ( droppedChecks . length ) {
633+ blocks . push ( [ `| Check constraints dropped: ${ droppedChecks . map ( ( it ) => `${ it . name } ` ) . join ( ', ' ) } \n` ] ) ;
631634 }
632-
633- res += `| It is not possible to create/drop check constraints on existing table\n` ;
634- blocks . push ( [ res ] ) ;
635635 }
636636
637637 if ( checksAlters . length ) {
638638 blocks . push ( [
639639 `│ Check constraints altered definition:\n` ,
640640 `│ ${ checksAlters . map ( ( it ) => `${ it . name } : ${ it . $left . value } -> ${ it . $right . value } ` ) . join ( ',\n' ) } \n` ,
641- `│ It is not possible to alter definition\n` ,
642641 ] ) ;
643642 }
644643
@@ -679,49 +678,40 @@ export const sqliteExplain = (
679678 }
680679
681680 if ( uniquesDiff . length ) {
682- let res : string = '' ;
683-
684681 const uniquesCreated = uniquesDiff . filter ( ( it ) => it . $diffType === 'create' ) ;
685682 const uniquesDropped = uniquesDiff . filter ( ( it ) => it . $diffType === 'drop' ) ;
686683 if ( uniquesCreated . length ) {
687- res += `│ Unique constraints added: ${ uniquesCreated . map ( ( it ) => `${ it . name } ` ) . join ( ', ' ) } \n` ;
684+ blocks . push ( [ `│ Unique constraints added: ${ uniquesCreated . map ( ( it ) => `${ it . name } ` ) . join ( ', ' ) } \n` ] ) ;
688685 }
689686 if ( uniquesDropped . length ) {
690- res += `│ Unique constraints dropped: ${ uniquesDropped . map ( ( it ) => `${ it . name } ` ) . join ( ', ' ) } \n` ;
687+ blocks . push ( [ `│ Unique constraints dropped: ${ uniquesDropped . map ( ( it ) => `${ it . name } ` ) . join ( ', ' ) } \n` ] ) ;
691688 }
692-
693- res += `│ It is not possible to create/drop unique constraints on existing table\n` ;
694-
695- blocks . push ( [ res ] ) ;
696689 }
697690
698691 if ( pksDiff . length ) {
699- let res : string = '' ;
700692 const pksCreated = pksDiff . filter ( ( it ) => it . $diffType === 'create' ) ;
701693 const pksDropped = pksDiff . filter ( ( it ) => it . $diffType === 'drop' ) ;
702694
703695 if ( pksCreated . length ) {
704- res += `│ Primary key constraints added: ${ pksCreated . map ( ( it ) => `${ it . name } ` ) . join ( ', ' ) } \n` ;
696+ blocks . push ( [ `│ Primary key constraints added: ${ pksCreated . map ( ( it ) => `${ it . name } ` ) . join ( ', ' ) } \n` ] ) ;
697+ }
698+ if ( pksDropped . length ) {
699+ blocks . push ( [ `│ Primary key constraints dropped: ${ pksDropped . map ( ( it ) => `${ it . name } ` ) . join ( ', ' ) } \n` ] ) ;
705700 }
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` ;
709- blocks . push ( [ res ] ) ;
710701 }
711702
712703 if ( newStoredColumns . length ) {
713704 blocks . push ( [
714705 `| Stored columns added: ${ newStoredColumns . map ( ( it ) => `${ it . name } ` ) . join ( ', ' ) } \n` ,
715- `| It is not possible to ALTER TABLE ADD COLUMN a STORED column. One can add a VIRTUAL column, however\n` ,
716706 ] ) ;
717707 }
718708
719709 if ( pksAlters . length ) {
720710 blocks . push ( [
721711 `│ Primary key was altered:\n` ,
722- `│ columns: ${
712+ `${
723713 pksAlters . filter ( ( it ) => it . columns ) . map ( ( it ) =>
724- `${ it . name } : [${ it . columns ?. from . join ( ',' ) } ] -> [${ it . columns ?. to . join ( ',' ) } ]\n`
714+ `[${ it . columns ?. from . join ( ',' ) } ] -> [${ it . columns ?. to . join ( ',' ) } ]\n`
725715 )
726716 } \n`,
727717 ] ) ;
@@ -730,9 +720,9 @@ export const sqliteExplain = (
730720 if ( uniquesAlters . length ) {
731721 blocks . push ( [
732722 `│ Unique constraint was altered:\n` ,
733- `│ columns: ${
723+ `${
734724 uniquesAlters . filter ( ( it ) => it . columns ) . map ( ( it ) =>
735- `${ it . name } , [${ it . columns ?. from . join ( ',' ) } ] -> [${ it . columns ?. to . join ( ',' ) } ]\n`
725+ `│ name: ${ it . name } => columns: [${ it . columns ?. from . join ( ',' ) } ] -> [${ it . columns ?. to . join ( ',' ) } ]\n`
736726 )
737727 } \n`,
738728 ] ) ;
@@ -749,56 +739,49 @@ export const sqliteExplain = (
749739 if ( columnsAltered ) {
750740 res += `${
751741 columnsAltered . map ( ( it ) =>
752- `│ ${ it . name } => columns: [${ it . columns ?. from . join ( ',' ) } ] -> [${ it . columns ?. to . join ( ',' ) } ]`
742+ `│ name: ${ it . name } => columns: [${ it . columns ?. from . join ( ',' ) } ] -> [${ it . columns ?. to . join ( ',' ) } ]`
753743 )
754744 } \n`;
755745 }
756746 if ( columnsToAltered ) {
757747 res += ` ${
758748 columnsToAltered . map ( ( it ) =>
759- `│ ${ it . name } => columnsTo: [${ it . columnsTo ?. from . join ( ',' ) } ] -> [${ it . columnsTo ?. to . join ( ',' ) } ]`
749+ `│ name: ${ it . name } => columnsTo: [${ it . columnsTo ?. from . join ( ',' ) } ] -> [${ it . columnsTo ?. to . join ( ',' ) } ]`
760750 )
761751 } \n`;
762752 }
763753 if ( tablesToAltered ) {
764754 res += `${
765- tablesToAltered . map ( ( it ) => `│ ${ it . name } => tableTo: [${ it . tableTo ?. from } ] -> [${ it . tableTo ?. to } ]` )
755+ tablesToAltered . map ( ( it ) => `│ name: ${ it . name } => tableTo: [${ it . tableTo ?. from } ] -> [${ it . tableTo ?. to } ]` )
766756 } \n`;
767757 }
768758
769759 blocks . push ( [ res ] ) ;
770760 }
771761
772762 if ( fksDiff . length ) {
773- let res : string = '' ;
774-
775763 const fksCreated = fksDiff . filter ( ( it ) => it . $diffType === 'create' ) ;
776764 const fksDropped = fksDiff . filter ( ( it ) => it . $diffType === 'drop' ) ;
777- if ( fksCreated ) res += `| Foreign key constraints added: ${ fksCreated . map ( ( it ) => ` ${ it . name } ` ) . join ( ', ' ) } \n` ;
778- if ( fksDropped ) res += `| Unique constraints dropped : ${ fksDropped . map ( ( it ) => `${ it . name } ` ) . join ( ', ' ) } \n` ;
779-
780- res += `| It is not possible to create/drop foreign key constraints on existing table\n` ;
781-
782- blocks . push ( [ res ] ) ;
765+ if ( fksCreated . length ) {
766+ blocks . push ( [ `| Foreign key constraints added : ${ fksCreated . map ( ( it ) => `${ it . name } ` ) . join ( ', ' ) } \n` ] ) ;
767+ }
768+ if ( fksDropped . length ) {
769+ blocks . push ( [ `| Foreign key constraints dropped: ${ fksDropped . map ( ( it ) => ` ${ it . name } ` ) . join ( ', ' ) } \n` ] ) ;
770+ }
783771 }
784772
785773 if ( indexesDiff . filter ( ( it ) => it . isUnique && it . origin === 'auto' ) . length ) {
786- let res : string = '' ;
787774 const indexCreated = indexesDiff . filter ( ( it ) => it . $diffType === 'create' ) ;
788775 const indexDropped = indexesDiff . filter ( ( it ) => it . $diffType === 'drop' ) ;
789- if ( indexCreated ) res += `| System generated index added: ${ indexCreated . map ( ( it ) => `${ it . name } ` ) . join ( ', ' ) } \n` ;
790- if ( indexDropped ) {
791- res += `| System generated index dropped: ${ indexDropped . map ( ( it ) => `${ it . name } ` ) . join ( ', ' ) } \n` ;
776+ if ( indexCreated . length ) {
777+ blocks . push ( [ `| System generated index added: ${ indexCreated . map ( ( it ) => `${ it . name } ` ) . join ( ', ' ) } \n` ] ) ;
778+ }
779+ if ( indexDropped . length ) {
780+ blocks . push ( [ `| System generated index dropped: ${ indexDropped . map ( ( it ) => `${ it . name } ` ) . join ( ', ' ) } \n` ] ) ;
792781 }
793-
794- res += `| It is not possible to drop/create auto generated unique indexes\n` ;
795-
796- blocks . push ( [ res ] ) ;
797782 }
798783
799- if ( blocks . filter ( ( it ) => Boolean ( it ) ) ) {
800- cause += blocks . map ( ( it ) => it . join ( '' ) ) . join ( '|-\n' ) ;
801- }
784+ cause += blocks . map ( ( it ) => it . join ( '' ) ) . join ( '├─\n' ) ;
802785 }
803786
804787 if ( st . type === 'recreate_column' ) {
0 commit comments