@@ -8,7 +8,7 @@ use sqlx::{PgPool, Row};
88static MIGRATOR : Migrator = sqlx:: migrate!( ) ;
99
1010#[ derive( Debug , thiserror:: Error ) ]
11- pub enum DbError {
11+ pub enum MigrateError {
1212 #[ error( "Config error: {0}" ) ]
1313 Config ( #[ from] ConfigError ) ,
1414
@@ -18,54 +18,62 @@ pub enum DbError {
1818 #[ error( "Migration error: {0}" ) ]
1919 Migrate ( #[ from] sqlx:: migrate:: MigrateError ) ,
2020
21- #[ error( "Alias '{0}' is not a database alias. Use --db or configure a db alias." ) ]
21+ #[ error( "Alias '{0}' is not a database alias. Use --db when creating the alias." ) ]
2222 NotDbAlias ( String ) ,
2323
2424 #[ error( "No alias specified and no default alias configured" ) ]
2525 NoAlias ,
2626}
2727
2828#[ derive( Subcommand ) ]
29- pub enum DbCommand {
30- /// Run pending migrations
31- Migrate ,
29+ pub enum MigrateCommand {
30+ /// Run all pending migrations
31+ #[ command( after_help = "Example:\n ow local migrate run" ) ]
32+ Run ,
3233
33- /// Show migration status
34+ /// Show which migrations are applied or pending
35+ #[ command( after_help = "Example:\n ow local migrate status" ) ]
3436 Status ,
3537
36- /// Mark all migrations as applied without running them (for existing databases)
38+ /// Mark all migrations as applied without running them
39+ #[ command(
40+ after_help = "Use this for existing databases that already have the schema.\n \n \
41+ Example:\n ow local migrate baseline"
42+ ) ]
3743 Baseline ,
3844}
3945
40- impl DbCommand {
41- pub async fn run ( self , alias : Option < String > ) -> Result < ( ) , DbError > {
46+ impl MigrateCommand {
47+ pub async fn run ( self , alias : Option < String > ) -> Result < ( ) , MigrateError > {
4248 let database_url = resolve_database_url ( alias) ?;
4349 let pool = connect ( & database_url) . await ?;
4450
4551 match self {
46- Self :: Migrate => cmd_migrate ( & pool) . await ,
52+ Self :: Run => cmd_run ( & pool) . await ,
4753 Self :: Status => cmd_status ( & pool) . await ,
4854 Self :: Baseline => cmd_baseline ( & pool) . await ,
4955 }
5056 }
5157}
5258
53- fn resolve_database_url ( alias : Option < String > ) -> Result < String , DbError > {
59+ fn resolve_database_url ( alias : Option < String > ) -> Result < String , MigrateError > {
5460 let config = Config :: load ( ) ?;
5561
56- let alias_name = alias. or ( config. default . clone ( ) ) . ok_or ( DbError :: NoAlias ) ?;
62+ let alias_name = alias
63+ . or ( config. default . clone ( ) )
64+ . ok_or ( MigrateError :: NoAlias ) ?;
5765
5866 let alias_config = config
5967 . get_alias ( & alias_name)
6068 . ok_or_else ( || ConfigError :: AliasNotFound ( alias_name. clone ( ) ) ) ?;
6169
6270 match alias_config {
6371 AliasConfig :: Db { database_url, .. } => Ok ( database_url. clone ( ) ) ,
64- AliasConfig :: Api { .. } => Err ( DbError :: NotDbAlias ( alias_name) ) ,
72+ AliasConfig :: Api { .. } => Err ( MigrateError :: NotDbAlias ( alias_name) ) ,
6573 }
6674}
6775
68- async fn connect ( database_url : & str ) -> Result < PgPool , DbError > {
76+ async fn connect ( database_url : & str ) -> Result < PgPool , MigrateError > {
6977 let pool = PgPoolOptions :: new ( )
7078 . max_connections ( 1 )
7179 . connect ( database_url)
@@ -74,7 +82,7 @@ async fn connect(database_url: &str) -> Result<PgPool, DbError> {
7482 Ok ( pool)
7583}
7684
77- async fn cmd_migrate ( pool : & PgPool ) -> Result < ( ) , DbError > {
85+ async fn cmd_run ( pool : & PgPool ) -> Result < ( ) , MigrateError > {
7886 println ! ( "Running migrations...\n " ) ;
7987
8088 MIGRATOR . run ( pool) . await ?;
@@ -84,7 +92,7 @@ async fn cmd_migrate(pool: &PgPool) -> Result<(), DbError> {
8492 Ok ( ( ) )
8593}
8694
87- async fn cmd_status ( pool : & PgPool ) -> Result < ( ) , DbError > {
95+ async fn cmd_status ( pool : & PgPool ) -> Result < ( ) , MigrateError > {
8896 // Get applied migrations from DB
8997 let applied: Vec < ( i64 , Vec < u8 > ) > =
9098 sqlx:: query ( "SELECT version, checksum FROM _sqlx_migrations ORDER BY version" )
@@ -135,14 +143,14 @@ async fn cmd_status(pool: &PgPool) -> Result<(), DbError> {
135143 println ! (
136144 "{} pending migration(s). Run '{}' to apply." ,
137145 pending_count. to_string( ) . yellow( ) ,
138- "ow db migrate" . cyan( )
146+ "ow migrate run " . cyan( )
139147 ) ;
140148 }
141149
142150 Ok ( ( ) )
143151}
144152
145- async fn cmd_baseline ( pool : & PgPool ) -> Result < ( ) , DbError > {
153+ async fn cmd_baseline ( pool : & PgPool ) -> Result < ( ) , MigrateError > {
146154 // Create _sqlx_migrations table if it doesn't exist
147155 sqlx:: query (
148156 r#"
@@ -170,7 +178,7 @@ async fn cmd_baseline(pool: &PgPool) -> Result<(), DbError> {
170178 "Warning:" . yellow( ) . bold( ) ,
171179 count
172180 ) ;
173- println ! ( "Use '{}' to check status." , "ow db status" . cyan( ) ) ;
181+ println ! ( "Use '{}' to check status." , "ow migrate status" . cyan( ) ) ;
174182 return Ok ( ( ) ) ;
175183 }
176184
0 commit comments