@@ -2,9 +2,9 @@ use std::fmt;
22
33use bon:: bon;
44use sqlparser:: ast:: {
5- AlterColumnOperation , AlterTableOperation , AlterType , AlterTypeAddValuePosition ,
6- AlterTypeOperation , ColumnOption , ColumnOptionDef , CreateTable , GeneratedAs , ObjectName ,
7- ObjectNamePart , ObjectType , Statement , UserDefinedTypeRepresentation ,
5+ AlterColumnOperation , AlterTable , AlterTableOperation , AlterType , AlterTypeAddValuePosition ,
6+ AlterTypeOperation , ColumnOption , ColumnOptionDef , CreateExtension , CreateTable , DropExtension ,
7+ GeneratedAs , ObjectName , ObjectNamePart , ObjectType , Statement , UserDefinedTypeRepresentation ,
88} ;
99use thiserror:: Error ;
1010
@@ -76,7 +76,7 @@ impl Migrate for Vec<Statement> {
7676 Statement :: CreateTable ( ca) => other
7777 . iter ( )
7878 . find ( |sb| match sb {
79- Statement :: AlterTable { name, .. } => * name == ca. name ,
79+ Statement :: AlterTable ( AlterTable { name, .. } ) => * name == ca. name ,
8080 Statement :: Drop {
8181 object_type, names, ..
8282 } => {
@@ -114,10 +114,12 @@ impl Migrate for Vec<Statement> {
114114 _ => false ,
115115 } )
116116 . map_or ( Some ( Ok ( orig) ) , |sb| sa. migrate ( sb) . transpose ( ) ) ,
117- Statement :: CreateExtension { name, .. } => other
117+ Statement :: CreateExtension ( CreateExtension { name, .. } ) => other
118118 . iter ( )
119119 . find ( |sb| match sb {
120- Statement :: DropExtension { names, .. } => names. contains ( name) ,
120+ Statement :: DropExtension ( DropExtension { names, .. } ) => {
121+ names. contains ( name)
122+ }
121123 _ => false ,
122124 } )
123125 . map_or ( Some ( Ok ( orig) ) , |sb| sa. migrate ( sb) . transpose ( ) ) ,
@@ -152,9 +154,9 @@ impl Migrate for Statement {
152154 fn migrate ( self , other : & Self ) -> Result < Option < Self > , MigrateError > {
153155 match self {
154156 Self :: CreateTable ( ca) => match other {
155- Self :: AlterTable {
157+ Self :: AlterTable ( AlterTable {
156158 name, operations, ..
157- } => {
159+ } ) => {
158160 if * name == ca. name {
159161 Ok ( Some ( Self :: CreateTable ( migrate_alter_table (
160162 ca, operations,
@@ -264,8 +266,13 @@ fn migrate_alter_table(
264266 AlterTableOperation :: AddColumn { column_def, .. } => {
265267 t. columns . push ( column_def. clone ( ) ) ;
266268 }
267- AlterTableOperation :: DropColumn { column_name, .. } => {
268- t. columns . retain ( |c| c. name . value != * column_name. value ) ;
269+ AlterTableOperation :: DropColumn { column_names, .. } => {
270+ t. columns . retain ( |c| {
271+ column_names
272+ . iter ( )
273+ . find ( |name| c. name . value == name. value )
274+ . is_none ( )
275+ } ) ;
269276 }
270277 AlterTableOperation :: AlterColumn { column_name, op } => {
271278 t. columns . iter_mut ( ) . for_each ( |c| {
@@ -297,7 +304,8 @@ fn migrate_alter_table(
297304 }
298305 AlterColumnOperation :: SetDataType {
299306 data_type,
300- using : _, // not applicable since we're not running the query
307+ using : _, // not applicable since we're not running the query
308+ had_set : _, // this doesn't change the meaning
301309 } => {
302310 c. data_type = data_type. clone ( ) ;
303311 }
@@ -339,9 +347,9 @@ fn migrate_alter_table(
339347
340348fn migrate_alter_type (
341349 name : ObjectName ,
342- representation : UserDefinedTypeRepresentation ,
350+ representation : Option < UserDefinedTypeRepresentation > ,
343351 other : & AlterType ,
344- ) -> Result < ( ObjectName , UserDefinedTypeRepresentation ) , MigrateError > {
352+ ) -> Result < ( ObjectName , Option < UserDefinedTypeRepresentation > ) , MigrateError > {
345353 match & other. operation {
346354 AlterTypeOperation :: Rename ( r) => {
347355 let mut parts = name. 0 ;
@@ -352,7 +360,7 @@ fn migrate_alter_type(
352360 Ok ( ( name, representation) )
353361 }
354362 AlterTypeOperation :: AddValue ( a) => match representation {
355- UserDefinedTypeRepresentation :: Enum { mut labels } => {
363+ Some ( UserDefinedTypeRepresentation :: Enum { mut labels } ) => {
356364 match & a. position {
357365 Some ( AlterTypeAddValuePosition :: Before ( before_name) ) => {
358366 let index = labels
@@ -379,9 +387,9 @@ fn migrate_alter_type(
379387 None => labels. push ( a. value . clone ( ) ) ,
380388 }
381389
382- Ok ( ( name, UserDefinedTypeRepresentation :: Enum { labels } ) )
390+ Ok ( ( name, Some ( UserDefinedTypeRepresentation :: Enum { labels } ) ) )
383391 }
384- UserDefinedTypeRepresentation :: Composite { .. } => Err ( MigrateError :: builder ( )
392+ Some ( _ ) | None => Err ( MigrateError :: builder ( )
385393 . kind ( MigrateErrorKind :: AlterTypeInvalidOp ( Box :: new (
386394 other. operation . clone ( ) ,
387395 ) ) )
@@ -393,15 +401,15 @@ fn migrate_alter_type(
393401 . build ( ) ) ,
394402 } ,
395403 AlterTypeOperation :: RenameValue ( rv) => match representation {
396- UserDefinedTypeRepresentation :: Enum { labels } => {
404+ Some ( UserDefinedTypeRepresentation :: Enum { labels } ) => {
397405 let labels = labels
398406 . into_iter ( )
399407 . map ( |l| if l == rv. from { rv. to . clone ( ) } else { l } )
400408 . collect :: < Vec < _ > > ( ) ;
401409
402- Ok ( ( name, UserDefinedTypeRepresentation :: Enum { labels } ) )
410+ Ok ( ( name, Some ( UserDefinedTypeRepresentation :: Enum { labels } ) ) )
403411 }
404- UserDefinedTypeRepresentation :: Composite { .. } => Err ( MigrateError :: builder ( )
412+ Some ( _ ) | None => Err ( MigrateError :: builder ( )
405413 . kind ( MigrateErrorKind :: AlterTypeInvalidOp ( Box :: new (
406414 other. operation . clone ( ) ,
407415 ) ) )
0 commit comments