Skip to content

Commit cb0e9b7

Browse files
committed
Merge branch 'dev'
2 parents 7f08eff + b454db2 commit cb0e9b7

File tree

5 files changed

+53
-37
lines changed

5 files changed

+53
-37
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ members = [
77

88
[package]
99
name = "rorm-cli"
10-
version = "0.6.0"
10+
version = "0.7.0"
1111
edition = "2021"
1212
authors = ["myOmikron <[email protected]>"]
1313
repository = "https://github.com/rorm-orm/rorm-cli"

changelog.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Since 0.5.0
1+
Since 0.6.0
22
-----------

src/migrate/mod.rs

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::cmp::Ordering;
22
use std::path::Path;
33

44
use anyhow::{anyhow, Context};
5-
use rorm_declaration::config::DatabaseDriver;
5+
use rorm_declaration::config::{DatabaseConfig, DatabaseDriver};
66
use rorm_declaration::imr::{Annotation, DbType};
77
use rorm_declaration::migration::Migration;
88
use rorm_sql::create_table::CreateTable;
@@ -96,41 +96,24 @@ pub async fn apply_migration(
9696
Ok(())
9797
}
9898

99-
/**
100-
Applies migrations on the given database
101-
*/
102-
pub async fn run_migrate(options: MigrateOptions) -> anyhow::Result<()> {
103-
let db_conf_path = Path::new(options.database_config.as_str());
104-
105-
if !&db_conf_path.exists() {
106-
println!(
107-
"Couldn't find the database configuration file, created {} and exiting",
108-
options.database_config.as_str()
109-
);
110-
create_db_config(db_conf_path)?;
111-
return Ok(());
112-
}
113-
114-
let db_conf = deserialize_db_conf(db_conf_path)?;
115-
116-
if let DatabaseDriver::SQLite { filename } = &db_conf.driver {
117-
if filename.is_empty() {
118-
println!("Invalid configuration: Filename for sqlite is empty");
119-
return Ok(());
120-
}
121-
}
122-
123-
let p = Path::new(options.migration_dir.as_str());
99+
/// Applies migrations on the given database with a given driver
100+
pub async fn run_migrate_custom(
101+
db_conf: DatabaseConfig,
102+
migration_dir: String,
103+
log_sql: bool,
104+
apply_until: Option<u16>,
105+
) -> anyhow::Result<()> {
106+
let p = Path::new(migration_dir.as_str());
124107
if !p.exists() || p.is_file() {
125108
println!(
126109
"Couldn't find the migration directory in {} \n\n\
127110
You can specify an alternative path with --migration-dir <PATH>",
128-
options.migration_dir.as_str()
111+
migration_dir.as_str()
129112
);
130113
return Ok(());
131114
}
132115

133-
let existing_migrations = get_existing_migrations(options.migration_dir.as_str())
116+
let existing_migrations = get_existing_migrations(migration_dir.as_str())
134117
.with_context(|| "Couldn't retrieve existing migrations")?;
135118

136119
if existing_migrations.is_empty() {
@@ -214,7 +197,7 @@ pub async fn run_migrate(options: MigrateOptions) -> anyhow::Result<()> {
214197
.with_context(|| "Could not create transaction")?;
215198

216199
for (query_string, bind_params) in statements {
217-
if options.log_queries {
200+
if log_sql {
218201
println!("{}", query_string.as_str());
219202
}
220203

@@ -237,7 +220,7 @@ pub async fn run_migrate(options: MigrateOptions) -> anyhow::Result<()> {
237220
"SELECT migration_id FROM {} ORDER BY id DESC LIMIT 1;",
238221
&last_migration_table_name
239222
),
240-
options.log_queries
223+
log_sql
241224
)
242225
.as_str(),
243226
)
@@ -257,11 +240,11 @@ pub async fn run_migrate(options: MigrateOptions) -> anyhow::Result<()> {
257240
migration,
258241
&pool,
259242
last_migration_table_name,
260-
options.log_queries,
243+
log_sql,
261244
)
262245
.await?;
263246

264-
if let Some(apply_until) = options.apply_until {
247+
if let Some(apply_until) = apply_until {
265248
if migration.id == apply_until {
266249
println!(
267250
"Applied all migrations until (inclusive) migration {apply_until:04}"
@@ -283,7 +266,7 @@ pub async fn run_migrate(options: MigrateOptions) -> anyhow::Result<()> {
283266
migration,
284267
&pool,
285268
last_migration_table_name,
286-
options.log_queries,
269+
log_sql,
287270
)
288271
.await?;
289272
continue;
@@ -297,7 +280,7 @@ pub async fn run_migrate(options: MigrateOptions) -> anyhow::Result<()> {
297280
}
298281
}
299282

300-
if let Some(apply_until) = options.apply_until {
283+
if let Some(apply_until) = apply_until {
301284
match migration.id.cmp(&apply_until) {
302285
Ordering::Equal => {
303286
if apply {
@@ -331,3 +314,34 @@ To correct, empty the {last_migration_table_name} table or reset the whole datab
331314

332315
Ok(())
333316
}
317+
318+
/// Applies migrations on the given database
319+
pub async fn run_migrate(options: MigrateOptions) -> anyhow::Result<()> {
320+
let db_conf_path = Path::new(options.database_config.as_str());
321+
322+
if !&db_conf_path.exists() {
323+
println!(
324+
"Couldn't find the database configuration file, created {} and exiting",
325+
options.database_config.as_str()
326+
);
327+
create_db_config(db_conf_path)?;
328+
return Ok(());
329+
}
330+
331+
let db_conf = deserialize_db_conf(db_conf_path)?;
332+
333+
if let DatabaseDriver::SQLite { filename } = &db_conf.driver {
334+
if filename.is_empty() {
335+
println!("Invalid configuration: Filename for sqlite is empty");
336+
return Ok(());
337+
}
338+
}
339+
340+
run_migrate_custom(
341+
db_conf,
342+
options.migration_dir,
343+
options.log_queries,
344+
options.apply_until,
345+
)
346+
.await
347+
}

src/utils/bind.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ where
4040
NullType::NaiveTime => query.bind(None::<NaiveTime>),
4141
NullType::NaiveDate => query.bind(None::<NaiveDate>),
4242
NullType::NaiveDateTime => query.bind(None::<NaiveDateTime>),
43+
NullType::Choice => query,
4344
},
4445
value::Value::Ident(_) => query,
4546
value::Value::Column { .. } => query,
47+
value::Value::Choice(_) => query,
4648
}
4749
}

0 commit comments

Comments
 (0)