Skip to content

Commit c66dbcd

Browse files
committed
refactor: pass entire context when calling run_migrate function
1 parent b7069c1 commit c66dbcd

2 files changed

Lines changed: 16 additions & 51 deletions

File tree

vectorctl-migration/src/cli.rs

Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
use std::{any::Any, path::PathBuf};
1+
use std::path::PathBuf;
22

33
use clap::Parser;
4-
#[cfg(feature = "sea-backend")]
5-
use sea_orm::{ConnectOptions, Database, DbConn, DbErr};
64
use thiserror::Error;
7-
use vectorctl_backend::generic::{VectorBackendError, VectorTrait};
5+
use vectorctl_backend::generic::VectorBackendError;
86
use vectorctl_cli::commands::{MigrateError, MigrateSubcommands, create_new_revision, init};
97

10-
use crate::{
11-
context::Backend,
12-
migrator::{MigrationError, MigratorTrait},
13-
};
8+
use crate::migrator::{MigrationError, MigratorTrait};
149

1510
#[derive(Error, Debug)]
1611
pub enum CliError {
@@ -22,9 +17,6 @@ pub enum CliError {
2217
Context(#[from] crate::context::ContextError),
2318
#[error(transparent)]
2419
Backend(#[from] VectorBackendError),
25-
#[cfg(feature = "sea-backend")]
26-
#[error(transparent)]
27-
Db(#[from] DbErr),
2820
}
2921

3022
#[derive(Parser)]
@@ -38,7 +30,7 @@ pub struct Cli {
3830
help = "vector database URL",
3931
default_value = "http://localhost:6334"
4032
)]
41-
database_url: Option<String>,
33+
pub database_url: String,
4234

4335
#[arg(
4436
global = true,
@@ -56,7 +48,7 @@ pub struct Cli {
5648
help = "database api key",
5749
env = "DATABASE_API_KEY"
5850
)]
59-
api_key: Option<String>,
51+
pub api_key: Option<String>,
6052

6153
#[cfg(feature = "sea-backend")]
6254
#[arg(
@@ -65,48 +57,20 @@ pub struct Cli {
6557
env = "SQL_DATABASE_URL",
6658
help = "sql database URL"
6759
)]
68-
sql_database_url: Option<String>,
60+
pub sql_database_url: Option<String>,
6961

7062
#[command(subcommand)]
71-
command: Option<MigrateSubcommands>,
63+
pub command: Option<MigrateSubcommands>,
7264
}
7365

74-
pub async fn run_migrate<M>(
75-
_: M,
76-
resources: Option<Vec<Box<dyn Any + 'static + Send + Sync>>>,
77-
) -> Result<(), CliError>
66+
pub async fn run_migrate<M>(_: M, context: &crate::context::Context) -> Result<(), CliError>
7867
where
7968
M: MigratorTrait,
8069
{
8170
let cli = Cli::parse();
8271

8372
let migration_dir = cli.migration_dir;
8473

85-
let database_url = cli
86-
.database_url
87-
.expect("Environment variable 'DATABASE_URL' not set");
88-
89-
#[cfg(feature = "qdrant-backend")]
90-
let api_key = cli.api_key;
91-
92-
#[cfg(not(feature = "qdrant-backend"))]
93-
let api_key = None;
94-
95-
let mut context = crate::context::Context::new(Backend::new(&database_url, api_key)?);
96-
97-
#[cfg(feature = "sea-backend")]
98-
if let Some(database_url) = cli.sql_database_url {
99-
let db_conn = {
100-
let connect_opts = ConnectOptions::from(database_url);
101-
Database::connect(connect_opts).await?
102-
};
103-
context.insert_resource::<DbConn>(db_conn);
104-
}
105-
106-
if let Some(resources) = resources {
107-
context.insert_resources(resources);
108-
};
109-
11074
match cli.command {
11175
Some(MigrateSubcommands::Init {
11276
package_name,
@@ -128,12 +92,12 @@ where
12892
)
12993
.await?
13094
}
131-
Some(MigrateSubcommands::Up { to }) => M::up(&context, to).await?,
132-
Some(MigrateSubcommands::Down { to }) => M::down(&context, to).await?,
133-
Some(MigrateSubcommands::Refresh) => M::refresh(&context).await?,
134-
Some(MigrateSubcommands::Reset) => M::reset(&context).await?,
135-
Some(MigrateSubcommands::Status) => M::status(&context).await?,
136-
None => M::up(&context, None).await?,
95+
Some(MigrateSubcommands::Up { to }) => M::up(context, to).await?,
96+
Some(MigrateSubcommands::Down { to }) => M::down(context, to).await?,
97+
Some(MigrateSubcommands::Refresh) => M::refresh(context).await?,
98+
Some(MigrateSubcommands::Reset) => M::reset(context).await?,
99+
Some(MigrateSubcommands::Status) => M::status(context).await?,
100+
None => M::up(context, None).await?,
137101
}
138102
Ok(())
139103
}

vectorctl-migration/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ mod revision;
55

66
use std::fmt::Debug;
77

8-
pub use cli::{CliError as CliMigrationError, run_migrate};
8+
pub use clap::Parser;
9+
pub use cli::{Cli, CliError as CliMigrationError, run_migrate};
910
pub use context::{Backend, Context, ContextError, Resource};
1011
pub use migrator::{MigrationError, MigratorTrait};
1112

0 commit comments

Comments
 (0)