11use anyhow:: Result ;
22use colored:: Colorize ;
33use vespertide_planner:: plan_next_migration;
4- use vespertide_query:: build_plan_queries;
4+ use vespertide_query:: { DatabaseBackend , build_plan_queries} ;
55
66use crate :: utils:: { load_config, load_migrations, load_models} ;
77
@@ -60,11 +60,9 @@ fn emit_sql(plan: &vespertide_core::MigrationPlan) -> Result<()> {
6060 println ! (
6161 "{}. {}" ,
6262 ( i + 1 ) . to_string( ) . bright_magenta( ) . bold( ) ,
63- q. sql . trim( ) . bright_white( )
63+ q. build ( DatabaseBackend :: Postgres ) . trim( ) . bright_white( )
6464 ) ;
65- if !q. binds . is_empty ( ) {
66- println ! ( " {} {:?}" , "binds:" . bright_cyan( ) , q. binds) ;
67- }
65+ println ! ( " {} {:?}" , "binds:" . bright_cyan( ) , q. binds( ) ) ;
6866 }
6967
7068 Ok ( ( ) )
@@ -137,31 +135,28 @@ mod tests {
137135 let tmp = tempdir ( ) . unwrap ( ) ;
138136 let _guard = CwdGuard :: new ( & tmp. path ( ) . to_path_buf ( ) ) ;
139137
140- let cfg = write_config ( ) ;
138+ let _cfg = write_config ( ) ;
141139 write_model ( "users" ) ;
142- fs:: create_dir_all ( cfg. migrations_dir ( ) ) . unwrap ( ) ;
143140
141+ // No migrations yet -> plan will create table
144142 let result = cmd_sql ( ) ;
145143 assert ! ( result. is_ok( ) ) ;
146144 }
147145
148146 #[ test]
149- fn emit_sql_no_actions_early_return ( ) {
147+ #[ serial]
148+ fn cmd_sql_no_changes ( ) {
149+ let tmp = tempdir ( ) . unwrap ( ) ;
150+ let _guard = CwdGuard :: new ( & tmp. path ( ) . to_path_buf ( ) ) ;
151+
152+ let cfg = write_config ( ) ;
153+ write_model ( "users" ) ;
154+
155+ // Create initial migration to establish baseline
150156 let plan = MigrationPlan {
151157 comment : None ,
152158 created_at : None ,
153159 version : 1 ,
154- actions : vec ! [ ] ,
155- } ;
156- assert ! ( emit_sql( & plan) . is_ok( ) ) ;
157- }
158-
159- #[ test]
160- fn emit_sql_with_metadata ( ) {
161- let plan = MigrationPlan {
162- comment : Some ( "init" . into ( ) ) ,
163- created_at : Some ( "2024-01-01T00:00:00Z" . into ( ) ) ,
164- version : 1 ,
165160 actions : vec ! [ MigrationAction :: CreateTable {
166161 table: "users" . into( ) ,
167162 columns: vec![ ColumnDef {
@@ -181,6 +176,27 @@ mod tests {
181176 } ] ,
182177 } ] ,
183178 } ;
184- assert ! ( emit_sql( & plan) . is_ok( ) ) ;
179+ fs:: create_dir_all ( cfg. migrations_dir ( ) ) . unwrap ( ) ;
180+ let path = cfg. migrations_dir ( ) . join ( "0001_init.json" ) ;
181+ fs:: write ( path, serde_json:: to_string_pretty ( & plan) . unwrap ( ) ) . unwrap ( ) ;
182+
183+ let result = cmd_sql ( ) ;
184+ assert ! ( result. is_ok( ) ) ;
185+ }
186+
187+ #[ test]
188+ #[ serial]
189+ fn emit_sql_prints_created_at_and_comment ( ) {
190+ let plan = MigrationPlan {
191+ comment : Some ( "with comment" . into ( ) ) ,
192+ created_at : Some ( "2024-01-02T00:00:00Z" . into ( ) ) ,
193+ version : 1 ,
194+ actions : vec ! [ MigrationAction :: RawSql {
195+ sql: "SELECT 1;" . into( ) ,
196+ } ] ,
197+ } ;
198+
199+ let result = emit_sql ( & plan) ;
200+ assert ! ( result. is_ok( ) ) ;
185201 }
186202}
0 commit comments