@@ -9,13 +9,13 @@ import type { JsonStatement } from '../../dialects/mysql/statements';
99import type { DB } from '../../utils' ;
1010import { prepareFilenames } from '../../utils/utils-node' ;
1111import { connectToMySQL } from '../connections' ;
12+ import { highlightSQL } from '../highlighter' ;
1213import { resolver } from '../prompts' ;
1314import { Select } from '../selector-ui' ;
1415import type { EntitiesFilterConfig } from '../validations/cli' ;
1516import type { CasingType } from '../validations/common' ;
1617import type { MysqlCredentials } from '../validations/mysql' ;
17- import { withStyle } from '../validations/outputs' ;
18- import { mysqlExplain , ProgressView } from '../views' ;
18+ import { explain , ProgressView } from '../views' ;
1919import { introspect } from './pull-mysql' ;
2020
2121export const handle = async (
@@ -25,7 +25,7 @@ export const handle = async (
2525 force : boolean ,
2626 casing : CasingType | undefined ,
2727 filters : EntitiesFilterConfig ,
28- explain : boolean ,
28+ explainFlag : boolean ,
2929) => {
3030 const { prepareFromSchemaFiles, fromDrizzleSchema } = await import ( '../../dialects/mysql/drizzle' ) ;
3131
@@ -64,62 +64,27 @@ export const handle = async (
6464 render ( `[${ chalk . blue ( 'i' ) } ] No changes detected` ) ;
6565 }
6666
67- if ( explain ) {
68- const messages : string [ ] = [ `\n\nThe following migration was generated:\n` ] ;
69- for ( const { jsonStatement, sqlStatements : sql } of groupedStatements ) {
70- const msg = mysqlExplain ( jsonStatement , sql ) ;
71- if ( msg ) messages . push ( msg ) ;
72- // Logic below should show all statements depending on flags like 'verbose' etc.
73- // else messages.push(...sql);
74- }
75- console . log ( withStyle . info ( messages . join ( '\n' ) ) ) ;
76- process . exit ( 0 ) ;
77- }
67+ const hints = await suggestions ( db , filteredStatements ) ;
68+ const explainMessage = explain ( 'mysql' , groupedStatements , explainFlag , hints ) ;
7869
79- const { hints, truncates } = await suggestions ( db , filteredStatements ) ;
80-
81- const combinedStatements = [ ...truncates , ...sqlStatements ] ;
82- if ( verbose ) {
83- console . log ( ) ;
84- console . log (
85- withStyle . warning ( 'You are about to execute current statements:' ) ,
86- ) ;
87- console . log ( ) ;
88- console . log ( combinedStatements . map ( ( s ) => chalk . blue ( s ) ) . join ( '\n' ) ) ;
89- console . log ( ) ;
90- }
70+ if ( explainMessage ) console . log ( explainMessage ) ;
71+ if ( explainFlag ) return ;
9172
9273 if ( ! force && hints . length > 0 ) {
93- const { data } = await render (
94- new Select ( [ 'No, abort' , `Yes, I want to execute all statements` ] ) ,
95- ) ;
74+ const { data } = await render ( new Select ( [ 'No, abort' , 'Yes, I want to execute all statements' ] ) ) ;
75+
9676 if ( data ?. index === 0 ) {
9777 render ( `[${ chalk . red ( 'x' ) } ] All changes were aborted` ) ;
9878 process . exit ( 0 ) ;
9979 }
10080 }
10181
102- if ( ! force && hints . length > 0 ) {
103- console . log ( withStyle . warning ( 'Found data-loss statements:' ) ) ;
104- console . log ( truncates . join ( '\n' ) ) ;
105- console . log ( ) ;
106- console . log (
107- chalk . red . bold (
108- 'THIS ACTION WILL CAUSE DATA LOSS AND CANNOT BE REVERTED\n' ,
109- ) ,
110- ) ;
82+ const lossStatements = hints . map ( ( x ) => x . statement ) . filter ( ( x ) => typeof x !== 'undefined' ) ;
11183
112- console . log ( chalk . white ( 'Do you still want to push changes?' ) ) ;
84+ for ( const statement of [ ...lossStatements , ...sqlStatements ] ) {
85+ if ( verbose ) console . log ( highlightSQL ( statement ) ) ;
11386
114- const { data } = await render ( new Select ( [ 'No, abort' , `Yes, execute` ] ) ) ;
115- if ( data ?. index === 0 ) {
116- render ( `[${ chalk . red ( 'x' ) } ] All changes were aborted` ) ;
117- process . exit ( 0 ) ;
118- }
119- }
120-
121- for ( const st of combinedStatements ) {
122- await db . query ( st ) ;
87+ await db . query ( statement ) ;
12388 }
12489
12590 if ( filteredStatements . length > 0 ) {
@@ -228,10 +193,7 @@ export const handle = async (
228193// };
229194
230195export const suggestions = async ( _db : DB , _statements : JsonStatement [ ] ) => {
231- const hints : string [ ] = [ ] ;
232- const truncates : string [ ] = [ ] ;
233-
234- return { hints, truncates } ;
196+ return [ ] as { hint : string ; statement ?: string | undefined } [ ] ;
235197
236198 // TODO: update and implement
237199
0 commit comments