Skip to content

Commit 3a42386

Browse files
committed
Add custom connection entrypoint to migration CLI
1 parent 9c27b5a commit 3a42386

1 file changed

Lines changed: 88 additions & 30 deletions

File tree

sea-orm-migration/src/cli.rs

Lines changed: 88 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,39 @@ where
3333
dotenv().ok();
3434
let cli = Cli::parse();
3535

36-
let url = cli
37-
.database_url
38-
.expect("Environment variable 'DATABASE_URL' not set");
39-
let schema = cli.database_schema.unwrap_or_else(|| "public".to_owned());
40-
41-
let connect_options = ConnectOptions::new(url)
42-
.set_schema_search_path(schema)
43-
.to_owned();
36+
run_cli_with_connection_inner(migrator, cli, async move |cli| {
37+
let url = cli
38+
.database_url
39+
.clone()
40+
.expect("Environment variable 'DATABASE_URL' not set");
41+
let schema = cli
42+
.database_schema
43+
.clone()
44+
.unwrap_or_else(|| "public".to_owned());
45+
46+
let connect_options = ConnectOptions::new(url)
47+
.set_schema_search_path(schema)
48+
.to_owned();
49+
50+
make_connection(connect_options).await
51+
})
52+
.await
53+
.unwrap_or_else(handle_error);
54+
}
4455

45-
let db = make_connection(connect_options)
46-
.await
47-
.expect("Fail to acquire database connection");
56+
/// Same as [`run_cli`] where you provide a fully customized way to create the
57+
/// [`DbConn`].
58+
///
59+
/// This allows loading database configuration from sources other than CLI
60+
/// arguments or environment variables.
61+
pub async fn run_cli_with_custom_connection(
62+
migrator: impl MigratorTraitSelf,
63+
make_connection: impl AsyncFnOnce() -> Result<DbConn, DbErr>,
64+
) {
65+
dotenv().ok();
66+
let cli = Cli::parse();
4867

49-
run_migrate(migrator, &db, cli.command, cli.verbose)
68+
run_cli_with_connection_inner(migrator, cli, async |_| make_connection().await)
5069
.await
5170
.unwrap_or_else(handle_error);
5271
}
@@ -60,6 +79,63 @@ pub async fn run_migrate<M>(
6079
where
6180
M: MigratorTraitSelf,
6281
{
82+
setup_tracing(verbose);
83+
run_migrate_inner(migrator, db, command).await
84+
}
85+
86+
async fn run_cli_with_connection_inner(
87+
migrator: impl MigratorTraitSelf,
88+
cli: Cli,
89+
make_connection: impl AsyncFnOnce(&Cli) -> Result<DbConn, DbErr>,
90+
) -> Result<(), Box<dyn Error>> {
91+
setup_tracing(cli.verbose);
92+
93+
match cli.command {
94+
Some(MigrateSubcommands::Init) => run_migrate_init(MIGRATION_DIR)?,
95+
Some(MigrateSubcommands::Generate {
96+
migration_name,
97+
universal_time: _,
98+
local_time,
99+
}) => run_migrate_generate(MIGRATION_DIR, &migration_name, !local_time)?,
100+
command => {
101+
let db = make_connection(&cli)
102+
.await
103+
.expect("Fail to acquire database connection");
104+
run_migrate_inner(migrator, &db, command).await?;
105+
}
106+
};
107+
108+
Ok(())
109+
}
110+
111+
async fn run_migrate_inner<M>(
112+
migrator: M,
113+
db: &DbConn,
114+
command: Option<MigrateSubcommands>,
115+
) -> Result<(), Box<dyn Error>>
116+
where
117+
M: MigratorTraitSelf,
118+
{
119+
match command {
120+
Some(MigrateSubcommands::Fresh) => migrator.fresh(db).await?,
121+
Some(MigrateSubcommands::Refresh) => migrator.refresh(db).await?,
122+
Some(MigrateSubcommands::Reset) => migrator.reset(db).await?,
123+
Some(MigrateSubcommands::Status) => migrator.status(db).await?,
124+
Some(MigrateSubcommands::Up { num }) => migrator.up(db, num).await?,
125+
Some(MigrateSubcommands::Down { num }) => migrator.down(db, Some(num)).await?,
126+
Some(MigrateSubcommands::Init) => run_migrate_init(MIGRATION_DIR)?,
127+
Some(MigrateSubcommands::Generate {
128+
migration_name,
129+
universal_time: _,
130+
local_time,
131+
}) => run_migrate_generate(MIGRATION_DIR, &migration_name, !local_time)?,
132+
_ => migrator.up(db, None).await?,
133+
};
134+
135+
Ok(())
136+
}
137+
138+
fn setup_tracing(verbose: bool) {
63139
let filter = match verbose {
64140
true => "debug",
65141
false => "sea_orm_migration=info",
@@ -83,24 +159,6 @@ where
83159
.with(fmt_layer)
84160
.init()
85161
};
86-
87-
match command {
88-
Some(MigrateSubcommands::Fresh) => migrator.fresh(db).await?,
89-
Some(MigrateSubcommands::Refresh) => migrator.refresh(db).await?,
90-
Some(MigrateSubcommands::Reset) => migrator.reset(db).await?,
91-
Some(MigrateSubcommands::Status) => migrator.status(db).await?,
92-
Some(MigrateSubcommands::Up { num }) => migrator.up(db, num).await?,
93-
Some(MigrateSubcommands::Down { num }) => migrator.down(db, Some(num)).await?,
94-
Some(MigrateSubcommands::Init) => run_migrate_init(MIGRATION_DIR)?,
95-
Some(MigrateSubcommands::Generate {
96-
migration_name,
97-
universal_time: _,
98-
local_time,
99-
}) => run_migrate_generate(MIGRATION_DIR, &migration_name, !local_time)?,
100-
_ => migrator.up(db, None).await?,
101-
};
102-
103-
Ok(())
104162
}
105163

106164
#[derive(Parser)]

0 commit comments

Comments
 (0)