Skip to content

Commit

Permalink
fix: diffing two migration directories on sqlite
Browse files Browse the repository at this point in the history
The pattern match was not exhaustive where it should have been.

closes prisma/prisma#13633
  • Loading branch information
tomhoule committed Jun 17, 2022
1 parent e7e0337 commit 6047dd7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
13 changes: 10 additions & 3 deletions migration-engine/core/src/commands/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async fn json_rpc_diff_target_to_connector(
}
DiffTarget::Migrations(PathContainer { path }) => {
let provider = migration_connector::migrations_directory::read_provider_from_lock_file(path);
match (provider, shadow_database_url) {
match (provider.as_deref(), shadow_database_url) {
(Some(provider), Some(shadow_database_url)) => {
let mut connector = crate::connector_for_provider(&provider)?;
let directories = migration_connector::migrations_directory::list_migrations(Path::new(path))?;
Expand All @@ -117,14 +117,21 @@ async fn json_rpc_diff_target_to_connector(
.await?;
Ok(Some((connector, schema)))
}
(provider, None) if provider.as_deref() != Some("sqlite") => Err(ConnectorError::from_msg(
(Some("sqlite"), None) => {
let mut connector = crate::connector_for_provider("sqlite")?;
let directories = migration_connector::migrations_directory::list_migrations(Path::new(path))?;
let schema = connector
.database_schema_from_diff_target(McDiff::Migrations(&directories), None)
.await?;
Ok(Some((connector, schema)))
}
(Some(_), None) => Err(ConnectorError::from_msg(
"You must pass the --shadow-database-url if you want to diff a migrations directory.".to_owned(),
)),
(None, _) => Err(ConnectorError::from_msg(
"Could not determine the connector from the migrations directory (missing migrations_lock.toml)."
.to_owned(),
)),
_ => unreachable!("no provider, no shadow database url for migrations target"),
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions migration-engine/migration-engine-tests/tests/migrations/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,40 @@ fn diffing_mongo_schemas_to_script_returns_a_nice_error() {
expected.assert_eq(&diff_error(params));
}

#[test]
fn diff_sqlite_migration_directories() {
let base_dir = tempfile::tempdir().unwrap();
let base_dir_2 = tempfile::tempdir().unwrap();
let base_dir_str = base_dir.path().to_str().unwrap();
let base_dir_str_2 = base_dir_2.path().to_str().unwrap();

let migrations_lock_path = base_dir.path().join("migration_lock.toml");
std::fs::write(
&migrations_lock_path,
&"provider = \"sqlite\"",
).unwrap();
let migrations_lock_path = base_dir_2.path().join("migration_lock.toml");
std::fs::write(
&migrations_lock_path,
&"provider = \"sqlite\"",
).unwrap();

let params = DiffParams {
exit_code: None,
from: DiffTarget::Migrations(PathContainer {
path: base_dir_str.to_owned(),
}),
script: true,
shadow_database_url: None,
to: DiffTarget::Migrations(PathContainer {
path: base_dir_str_2.to_owned(),
}),
};

tok(migration_core::migration_api(None, None).unwrap().diff(params)).unwrap();
// it's ok!
}

#[test]
fn diffing_mongo_schemas_works() {
let tempdir = tempfile::tempdir().unwrap();
Expand Down

0 comments on commit 6047dd7

Please sign in to comment.