Skip to content

Commit

Permalink
Merge pull request prisma#2631 from prisma/db-execute-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhoule authored Jan 27, 2022
2 parents 9c25003 + 6a95ce8 commit 8117ac1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 8 deletions.
31 changes: 31 additions & 0 deletions migration-engine/cli/tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ impl TestApi {
rt.block_on(args.create_mysql_database()).1
} else if args.tags().contains(Tags::Mssql) {
rt.block_on(args.create_mssql_database()).1
} else if args.tags().contains(Tags::Sqlite) {
args.database_url().to_owned()
} else {
unreachable!()
}
Expand All @@ -38,11 +40,40 @@ impl TestApi {
.unwrap()
}

fn run_raw(&self, args: &[&str]) -> Output {
Command::new(self.migration_engine_bin_path())
.args(args)
.env("RUST_LOG", "INFO")
.output()
.unwrap()
}

fn migration_engine_bin_path(&self) -> &'static str {
env!("CARGO_BIN_EXE_migration-engine")
}
}

#[test_connector(tags(Sqlite))]
fn test_starting_the_engine_with_empty_schema(api: TestApi) {
let tmpdir = tempfile::tempdir().unwrap();
let schema_path = tmpdir.path().join("schema.prisma");
std::fs::write(&schema_path, "").unwrap();
let output = api.run_raw(&["--datamodel", schema_path.to_string_lossy().as_ref()]);

assert!(!output.status.success(), "{:?}", output);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(stderr.contains("no datasource in the schema"), "{:?}", stderr);
}

#[test_connector(tags(Sqlite))]
fn test_starting_the_engine_with_no_schema(api: TestApi) {
let output = api.run_raw(&[]);

assert!(!output.status.success(), "{:?}", output);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(stderr.contains("Missing --datamodel"), "{:?}", stderr);
}

#[test_connector(tags(Mysql))]
fn test_connecting_with_a_working_mysql_connection_string(api: TestApi) {
let connection_string = api.connection_string();
Expand Down
16 changes: 8 additions & 8 deletions migration-engine/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,6 @@ fn parse_configuration(datamodel: &str) -> CoreResult<(Datasource, String, BitFl
.map(|validated_config| validated_config.subject)
.map_err(|err| CoreError::new_schema_parser_error(err.to_pretty_string("schema.prisma", datamodel)))?;

let url = config.datasources[0]
.load_url(|key| env::var(key).ok())
.map_err(|err| CoreError::new_schema_parser_error(err.to_pretty_string("schema.prisma", datamodel)))?;

let shadow_database_url = config.datasources[0]
.load_shadow_database_url()
.map_err(|err| CoreError::new_schema_parser_error(err.to_pretty_string("schema.prisma", datamodel)))?;

let preview_features = config.preview_features();

let source = config
Expand All @@ -117,6 +109,14 @@ fn parse_configuration(datamodel: &str) -> CoreResult<(Datasource, String, BitFl
.next()
.ok_or_else(|| CoreError::from_msg("There is no datasource in the schema.".into()))?;

let url = source
.load_url(|key| env::var(key).ok())
.map_err(|err| CoreError::new_schema_parser_error(err.to_pretty_string("schema.prisma", datamodel)))?;

let shadow_database_url = source
.load_shadow_database_url()
.map_err(|err| CoreError::new_schema_parser_error(err.to_pretty_string("schema.prisma", datamodel)))?;

Ok((source, url, preview_features, shadow_database_url))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,44 @@ fn db_execute_happy_path_with_prisma_schema(api: TestApi) {
assert_eq!(rows.next().unwrap()[0].to_string().unwrap(), "snoopy");
assert_eq!(rows.next().unwrap()[0].to_string().unwrap(), "marmaduke");
}

#[test_connector(tags(Mysql))]
fn mysql_incomplete_script_works(api: TestApi) {
let script = r#"
CREATE TABLE `dogs` ( id INTEGER AUTO_INCREMENT PRIMARY KEY, name TEXT );
INSERT INTO `dogs` (`name`) VALUES ('snoopy'), ('marmaduke') -- missing final semicolon
"#;

let url = api.connection_string().to_owned();

// Execute the command.
api.db_execute(&DbExecuteParams {
datasource_type: DbExecuteDatasourceType::Url(UrlContainer { url: url.clone() }),
script: script.to_owned(),
})
.unwrap();

// Check that the command was executed
let q = api.block_on(quaint::single::Quaint::new(&url)).unwrap();
let result = api.block_on(q.query_raw("SELECT name FROM dogs;", &[])).unwrap();
let mut rows = result.into_iter();
assert_eq!(rows.next().unwrap()[0].to_string().unwrap(), "snoopy");
assert_eq!(rows.next().unwrap()[0].to_string().unwrap(), "marmaduke");
}

#[test_connector(tags(Mysql))]
fn db_execute_error_path(api: TestApi) {
let script = r#"
CREATE TABLE "dogs" ( id INTEGER AUTO_INCREMENT PRIMARY KEY, name TEXT );
"#;

// Execute the command.
let result = api.db_execute(&DbExecuteParams {
datasource_type: DbExecuteDatasourceType::Url(UrlContainer {
url: api.connection_string().to_owned(),
}),
script: script.to_owned(),
});

assert!(result.is_err());
}

0 comments on commit 8117ac1

Please sign in to comment.