@@ -3,6 +3,7 @@ use crate::schema::{
33} ;
44use schemars:: JsonSchema ;
55use serde:: { Deserialize , Serialize } ;
6+ use std:: fmt;
67
78#[ derive( Debug , Clone , PartialEq , Eq , Serialize , Deserialize , JsonSchema ) ]
89#[ serde( rename_all = "snake_case" ) ]
@@ -69,3 +70,88 @@ pub enum MigrationAction {
6970 sql : String ,
7071 } ,
7172}
73+
74+ impl fmt:: Display for MigrationAction {
75+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
76+ match self {
77+ MigrationAction :: CreateTable { table, .. } => {
78+ write ! ( f, "CreateTable: {}" , table)
79+ }
80+ MigrationAction :: DeleteTable { table } => {
81+ write ! ( f, "DeleteTable: {}" , table)
82+ }
83+ MigrationAction :: AddColumn { table, column, .. } => {
84+ write ! ( f, "AddColumn: {}.{}" , table, column. name)
85+ }
86+ MigrationAction :: RenameColumn { table, from, to } => {
87+ write ! ( f, "RenameColumn: {}.{} -> {}" , table, from, to)
88+ }
89+ MigrationAction :: DeleteColumn { table, column } => {
90+ write ! ( f, "DeleteColumn: {}.{}" , table, column)
91+ }
92+ MigrationAction :: ModifyColumnType { table, column, .. } => {
93+ write ! ( f, "ModifyColumnType: {}.{}" , table, column)
94+ }
95+ MigrationAction :: AddIndex { table, index } => {
96+ write ! ( f, "AddIndex: {}.{}" , table, index. name)
97+ }
98+ MigrationAction :: RemoveIndex { name, .. } => {
99+ write ! ( f, "RemoveIndex: {}" , name)
100+ }
101+ MigrationAction :: AddConstraint { table, constraint } => {
102+ let constraint_name = match constraint {
103+ TableConstraint :: PrimaryKey { .. } => "PRIMARY KEY" ,
104+ TableConstraint :: Unique { name, .. } => {
105+ if let Some ( n) = name {
106+ return write ! ( f, "AddConstraint: {}.{} (UNIQUE)" , table, n) ;
107+ }
108+ "UNIQUE"
109+ }
110+ TableConstraint :: ForeignKey { name, .. } => {
111+ if let Some ( n) = name {
112+ return write ! ( f, "AddConstraint: {}.{} (FOREIGN KEY)" , table, n) ;
113+ }
114+ "FOREIGN KEY"
115+ }
116+ TableConstraint :: Check { name, .. } => {
117+ return write ! ( f, "AddConstraint: {}.{} (CHECK)" , table, name) ;
118+ }
119+ } ;
120+ write ! ( f, "AddConstraint: {}.{}" , table, constraint_name)
121+ }
122+ MigrationAction :: RemoveConstraint { table, constraint } => {
123+ let constraint_name = match constraint {
124+ TableConstraint :: PrimaryKey { .. } => "PRIMARY KEY" ,
125+ TableConstraint :: Unique { name, .. } => {
126+ if let Some ( n) = name {
127+ return write ! ( f, "RemoveConstraint: {}.{} (UNIQUE)" , table, n) ;
128+ }
129+ "UNIQUE"
130+ }
131+ TableConstraint :: ForeignKey { name, .. } => {
132+ if let Some ( n) = name {
133+ return write ! ( f, "RemoveConstraint: {}.{} (FOREIGN KEY)" , table, n) ;
134+ }
135+ "FOREIGN KEY"
136+ }
137+ TableConstraint :: Check { name, .. } => {
138+ return write ! ( f, "RemoveConstraint: {}.{} (CHECK)" , table, name) ;
139+ }
140+ } ;
141+ write ! ( f, "RemoveConstraint: {}.{}" , table, constraint_name)
142+ }
143+ MigrationAction :: RenameTable { from, to } => {
144+ write ! ( f, "RenameTable: {} -> {}" , from, to)
145+ }
146+ MigrationAction :: RawSql { sql } => {
147+ // Truncate SQL if too long for display
148+ let display_sql = if sql. len ( ) > 50 {
149+ format ! ( "{}..." , & sql[ ..47 ] )
150+ } else {
151+ sql. clone ( )
152+ } ;
153+ write ! ( f, "RawSql: {}" , display_sql)
154+ }
155+ }
156+ }
157+ }
0 commit comments