Skip to content

Commit

Permalink
tests: complete sqlx migrate tests for onchange migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
C茅dric Prezelin authored and C茅dric Prezelin committed Apr 26, 2024
1 parent 6098033 commit b581309
Show file tree
Hide file tree
Showing 14 changed files with 329 additions and 71 deletions.
74 changes: 57 additions & 17 deletions sqlx-cli/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use assert_cmd::{assert::Assert, Command};

use sqlx::{migrate::Migrate, Connection, SqliteConnection};
use sqlx::{
migrate::{AppliedMigration, Migrate},
Connection, SqliteConnection,
};
use std::{
env::temp_dir,
fs::remove_file,
Expand All @@ -9,17 +12,12 @@ use std::{

pub struct TestDatabase {
file_path: PathBuf,
migrations: String,
}

impl TestDatabase {
pub fn new(name: &str, migrations: &str) -> Self {
let migrations_path = Path::new("tests").join(migrations);
pub fn new(name: &str) -> Self {
let file_path = Path::new(&temp_dir()).join(format!("test-{}.db", name));
let ret = Self {
file_path,
migrations: String::from(migrations_path.to_str().unwrap()),
};
let ret = Self { file_path };
Command::cargo_bin("cargo-sqlx")
.unwrap()
.args([
Expand All @@ -34,11 +32,45 @@ impl TestDatabase {
ret
}

pub fn connection_string(&self) -> String {
format!("sqlite://{}", self.file_path.display())
pub fn run_migration(&self, source: &str, version: Option<i64>, dry_run: bool) -> Assert {
let source_path = Path::new("tests").join(source);
let source = String::from(source_path.to_str().unwrap());

let ver = match version {
Some(v) => v.to_string(),
None => String::from(""),
};
Command::cargo_bin("cargo-sqlx")
.unwrap()
.args(
[
vec![
"sqlx",
"migrate",
"run",
"--database-url",
&self.connection_string(),
"--source",
&source,
],
match version {
Some(_) => vec!["--target-version", &ver],
None => vec![],
},
match dry_run {
true => vec!["--dry-run"],
false => vec![],
},
]
.concat(),
)
.assert()
}

pub fn run_migration(&self, revert: bool, version: Option<i64>, dry_run: bool) -> Assert {
pub fn revert_migration(&self, source: &str, version: Option<i64>, dry_run: bool) -> Assert {
let source_path = Path::new("tests").join(source);
let source = String::from(source_path.to_str().unwrap());

let ver = match version {
Some(v) => v.to_string(),
None => String::from(""),
Expand All @@ -50,14 +82,11 @@ impl TestDatabase {
vec![
"sqlx",
"migrate",
match revert {
true => "revert",
false => "run",
},
"revert",
"--database-url",
&self.connection_string(),
"--source",
&self.migrations,
&source,
],
match version {
Some(_) => vec!["--target-version", &ver],
Expand All @@ -73,7 +102,14 @@ impl TestDatabase {
.assert()
}

pub async fn applied_migrations(&self) -> Vec<i64> {
pub async fn applied_migrations(&self) -> Vec<AppliedMigration> {
let mut conn = SqliteConnection::connect(&self.connection_string())
.await
.unwrap();
conn.list_applied_migrations().await.unwrap()
}

pub async fn applied_migrations_versions(&self) -> Vec<i64> {
let mut conn = SqliteConnection::connect(&self.connection_string())
.await
.unwrap();
Expand All @@ -84,6 +120,10 @@ impl TestDatabase {
.map(|m| m.version)
.collect()
}

fn connection_string(&self) -> String {
format!("sqlite://{}", self.file_path.display())
}
}

impl Drop for TestDatabase {
Expand Down

0 comments on commit b581309

Please sign in to comment.