Skip to content

Commit

Permalink
Replace more instances of Connection::execute
Browse files Browse the repository at this point in the history
  • Loading branch information
weiznich committed Mar 3, 2022
1 parent ad1ce71 commit 7d07f64
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 64 deletions.
4 changes: 2 additions & 2 deletions diesel/src/pg/expression/array_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::sql_types::{Array, SqlType};
/// # fn main() {
/// # use schema::users::dsl::*;
/// # let connection = &mut establish_connection();
/// # connection.execute("INSERT INTO users (name) VALUES ('Jim')").unwrap();
/// # diesel::sql_query("INSERT INTO users (name) VALUES ('Jim')").execute(connection).unwrap();
/// let sean = (1, "Sean".to_string());
/// let jim = (3, "Jim".to_string());
/// let data = users.filter(name.eq(any(vec!["Sean", "Jim"])));
Expand Down Expand Up @@ -48,7 +48,7 @@ where
/// # fn main() {
/// # use schema::users::dsl::*;
/// # let connection = &mut establish_connection();
/// # connection.execute("INSERT INTO users (name) VALUES ('Jim')").unwrap();
/// # diesel::sql_query("INSERT INTO users (name) VALUES ('Jim')").execute(connection).unwrap();
/// let tess = (2, "Tess".to_string());
/// let data = users.filter(name.ne(all(vec!["Sean", "Jim"])));
/// assert_eq!(Ok(vec![tess]), data.load(connection));
Expand Down
95 changes: 51 additions & 44 deletions diesel_cli/src/infer_schema_internals/information_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,11 @@ mod tests {
fn skip_views() {
let mut connection = connection();

connection
.execute("CREATE TABLE a_regular_table (id SERIAL PRIMARY KEY)")
diesel::sql_query("CREATE TABLE a_regular_table (id SERIAL PRIMARY KEY)")
.execute(&mut connection)
.unwrap();
connection
.execute("CREATE VIEW a_view AS SELECT 42")
diesel::sql_query("CREATE VIEW a_view AS SELECT 42")
.execute(&mut connection)
.unwrap();

let table_names = load_table_names(&mut connection, None).unwrap();
Expand All @@ -421,10 +421,9 @@ mod tests {
fn load_table_names_loads_from_public_schema_if_none_given() {
let mut connection = connection();

connection
.execute(
diesel::sql_query(
"CREATE TABLE load_table_names_loads_from_public_schema_if_none_given (id SERIAL PRIMARY KEY)",
)
).execute(&mut connection)
.unwrap();

let table_names = load_table_names(&mut connection, None).unwrap();
Expand All @@ -440,16 +439,18 @@ mod tests {
fn load_table_names_loads_from_custom_schema() {
let mut connection = connection();

connection.execute("CREATE SCHEMA test_schema").unwrap();
connection
.execute("CREATE TABLE test_schema.table_1 (id SERIAL PRIMARY KEY)")
diesel::sql_query("CREATE SCHEMA test_schema")
.execute(&mut connection)
.unwrap();
diesel::sql_query("CREATE TABLE test_schema.table_1 (id SERIAL PRIMARY KEY)")
.execute(&mut connection)
.unwrap();

let table_names = load_table_names(&mut connection, Some("test_schema")).unwrap();
assert_eq!(vec![TableName::new("table_1", "test_schema")], table_names);

connection
.execute("CREATE TABLE test_schema.table_2 (id SERIAL PRIMARY KEY)")
diesel::sql_query("CREATE TABLE test_schema.table_2 (id SERIAL PRIMARY KEY)")
.execute(&mut connection)
.unwrap();

let table_names = load_table_names(&mut connection, Some("test_schema")).unwrap();
Expand All @@ -459,11 +460,11 @@ mod tests {
];
assert_eq!(expected, table_names);

connection
.execute("CREATE SCHEMA other_test_schema")
diesel::sql_query("CREATE SCHEMA other_test_schema")
.execute(&mut connection)
.unwrap();
connection
.execute("CREATE TABLE other_test_schema.table_1 (id SERIAL PRIMARY KEY)")
diesel::sql_query("CREATE TABLE other_test_schema.table_1 (id SERIAL PRIMARY KEY)")
.execute(&mut connection)
.unwrap();

let table_names = load_table_names(&mut connection, Some("test_schema")).unwrap();
Expand All @@ -482,15 +483,17 @@ mod tests {
#[test]
fn load_table_names_output_is_ordered() {
let mut connection = connection();
connection.execute("CREATE SCHEMA test_schema").unwrap();
connection
.execute("CREATE TABLE test_schema.ccc (id SERIAL PRIMARY KEY)")
diesel::sql_query("CREATE SCHEMA test_schema")
.execute(&mut connection)
.unwrap();
connection
.execute("CREATE TABLE test_schema.aaa (id SERIAL PRIMARY KEY)")
diesel::sql_query("CREATE TABLE test_schema.ccc (id SERIAL PRIMARY KEY)")
.execute(&mut connection)
.unwrap();
connection
.execute("CREATE TABLE test_schema.bbb (id SERIAL PRIMARY KEY)")
diesel::sql_query("CREATE TABLE test_schema.aaa (id SERIAL PRIMARY KEY)")
.execute(&mut connection)
.unwrap();
diesel::sql_query("CREATE TABLE test_schema.bbb (id SERIAL PRIMARY KEY)")
.execute(&mut connection)
.unwrap();

let table_names = load_table_names(&mut connection, Some("test_schema"))
Expand All @@ -508,14 +511,17 @@ mod tests {
fn get_primary_keys_only_includes_primary_key() {
let mut connection = connection();

connection.execute("CREATE SCHEMA test_schema").unwrap();
connection
.execute("CREATE TABLE test_schema.table_1 (id SERIAL PRIMARY KEY, not_id INTEGER)")
diesel::sql_query("CREATE SCHEMA test_schema")
.execute(&mut connection)
.unwrap();
connection
.execute(
diesel::sql_query(
"CREATE TABLE test_schema.table_1 (id SERIAL PRIMARY KEY, not_id INTEGER)",
)
.execute(&mut connection)
.unwrap();
diesel::sql_query(
"CREATE TABLE test_schema.table_2 (id INTEGER, id2 INTEGER, not_id INTEGER, PRIMARY KEY (id, id2))",
)
).execute(&mut connection)
.unwrap();

let table_1 = TableName::new("table_1", "test_schema");
Expand All @@ -534,14 +540,15 @@ mod tests {
fn get_table_data_loads_column_information() {
let mut connection = connection();

connection.execute("CREATE SCHEMA test_schema").unwrap();
connection
.execute(
diesel::sql_query("CREATE SCHEMA test_schema")
.execute(&mut connection)
.unwrap();
diesel::sql_query(
"CREATE TABLE test_schema.table_1 (id SERIAL PRIMARY KEY, text_col VARCHAR, not_null TEXT NOT NULL)",
)
).execute(&mut connection)
.unwrap();
connection
.execute("CREATE TABLE test_schema.table_2 (array_col VARCHAR[] NOT NULL)")
diesel::sql_query("CREATE TABLE test_schema.table_2 (array_col VARCHAR[] NOT NULL)")
.execute(&mut connection)
.unwrap();

let table_1 = TableName::new("table_1", "test_schema");
Expand All @@ -565,19 +572,19 @@ mod tests {
fn get_foreign_keys_loads_foreign_keys() {
let mut connection = connection();

connection.execute("CREATE SCHEMA test_schema").unwrap();
connection
.execute("CREATE TABLE test_schema.table_1 (id SERIAL PRIMARY KEY)")
diesel::sql_query("CREATE SCHEMA test_schema")
.execute(&mut connection)
.unwrap();
connection
.execute(
diesel::sql_query("CREATE TABLE test_schema.table_1 (id SERIAL PRIMARY KEY)")
.execute(&mut connection)
.unwrap();
diesel::sql_query(
"CREATE TABLE test_schema.table_2 (id SERIAL PRIMARY KEY, fk_one INTEGER NOT NULL REFERENCES test_schema.table_1)",
)
).execute(&mut connection)
.unwrap();
connection
.execute(
diesel::sql_query(
"CREATE TABLE test_schema.table_3 (id SERIAL PRIMARY KEY, fk_two INTEGER NOT NULL REFERENCES test_schema.table_2)",
)
).execute(&mut connection)
.unwrap();

let table_1 = TableName::new("table_1", "test_schema");
Expand Down
40 changes: 26 additions & 14 deletions diesel_cli/src/infer_schema_internals/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ fn load_table_names_returns_nothing_when_no_tables_exist() {
#[test]
fn load_table_names_includes_tables_that_exist() {
let mut conn = SqliteConnection::establish(":memory:").unwrap();
conn.execute("CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT)")
diesel::sql_query("CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT)")
.execute(&mut conn)
.unwrap();
let table_names = load_table_names(&mut conn, None).unwrap();
assert!(table_names.contains(&TableName::from_name("users")));
Expand All @@ -279,7 +280,8 @@ fn load_table_names_includes_tables_that_exist() {
#[test]
fn load_table_names_excludes_diesel_metadata_tables() {
let mut conn = SqliteConnection::establish(":memory:").unwrap();
conn.execute("CREATE TABLE __diesel_metadata (id INTEGER PRIMARY KEY AUTOINCREMENT)")
diesel::sql_query("CREATE TABLE __diesel_metadata (id INTEGER PRIMARY KEY AUTOINCREMENT)")
.execute(&mut conn)
.unwrap();
let table_names = load_table_names(&mut conn, None).unwrap();
assert!(!table_names.contains(&TableName::from_name("__diesel_metadata")));
Expand All @@ -288,9 +290,11 @@ fn load_table_names_excludes_diesel_metadata_tables() {
#[test]
fn load_table_names_excludes_sqlite_metadata_tables() {
let mut conn = SqliteConnection::establish(":memory:").unwrap();
conn.execute("CREATE TABLE __diesel_metadata (id INTEGER PRIMARY KEY AUTOINCREMENT)")
diesel::sql_query("CREATE TABLE __diesel_metadata (id INTEGER PRIMARY KEY AUTOINCREMENT)")
.execute(&mut conn)
.unwrap();
conn.execute("CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT)")
diesel::sql_query("CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT)")
.execute(&mut conn)
.unwrap();
let table_names = load_table_names(&mut conn, None);
assert_eq!(vec![TableName::from_name("users")], table_names.unwrap());
Expand All @@ -299,9 +303,12 @@ fn load_table_names_excludes_sqlite_metadata_tables() {
#[test]
fn load_table_names_excludes_views() {
let mut conn = SqliteConnection::establish(":memory:").unwrap();
conn.execute("CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT)")
diesel::sql_query("CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT)")
.execute(&mut conn)
.unwrap();
diesel::sql_query("CREATE VIEW answer AS SELECT 42")
.execute(&mut conn)
.unwrap();
conn.execute("CREATE VIEW answer AS SELECT 42").unwrap();
let table_names = load_table_names(&mut conn, None);
assert_eq!(vec![TableName::from_name("users")], table_names.unwrap());
}
Expand All @@ -324,11 +331,14 @@ fn load_table_names_returns_error_when_given_schema_name() {
#[test]
fn load_table_names_output_is_ordered() {
let mut conn = SqliteConnection::establish(":memory:").unwrap();
conn.execute("CREATE TABLE bbb (id INTEGER PRIMARY KEY AUTOINCREMENT)")
diesel::sql_query("CREATE TABLE bbb (id INTEGER PRIMARY KEY AUTOINCREMENT)")
.execute(&mut conn)
.unwrap();
conn.execute("CREATE TABLE aaa (id INTEGER PRIMARY KEY AUTOINCREMENT)")
diesel::sql_query("CREATE TABLE aaa (id INTEGER PRIMARY KEY AUTOINCREMENT)")
.execute(&mut conn)
.unwrap();
conn.execute("CREATE TABLE ccc (id INTEGER PRIMARY KEY AUTOINCREMENT)")
diesel::sql_query("CREATE TABLE ccc (id INTEGER PRIMARY KEY AUTOINCREMENT)")
.execute(&mut conn)
.unwrap();

let table_names = load_table_names(&mut conn, None)
Expand All @@ -343,12 +353,14 @@ fn load_table_names_output_is_ordered() {
fn load_foreign_key_constraints_loads_foreign_keys() {
let mut connection = SqliteConnection::establish(":memory:").unwrap();

connection.execute("CREATE TABLE table_1 (id)").unwrap();
connection
.execute("CREATE TABLE table_2 (id, fk_one REFERENCES table_1(id))")
diesel::sql_query("CREATE TABLE table_1 (id)")
.execute(&mut connection)
.unwrap();
diesel::sql_query("CREATE TABLE table_2 (id, fk_one REFERENCES table_1(id))")
.execute(&mut connection)
.unwrap();
connection
.execute("CREATE TABLE table_3 (id, fk_two REFERENCES table_2(id))")
diesel::sql_query("CREATE TABLE table_3 (id, fk_two REFERENCES table_2(id))")
.execute(&mut connection)
.unwrap();

let table_1 = TableName::from_name("table_1");
Expand Down
6 changes: 4 additions & 2 deletions diesel_cli/tests/support/mysql_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ impl Database {
pub fn create(self) -> Self {
let (database, mysql_url) = self.split_url();
let mut conn = MysqlConnection::establish(&mysql_url).unwrap();
conn.execute(&format!("CREATE DATABASE `{}`", database))
diesel::sql_query(&format!("CREATE DATABASE `{}`", database))
.execute(&mut conn)
.unwrap();
self
}
Expand Down Expand Up @@ -64,7 +65,8 @@ impl Drop for Database {
"Couldn't connect to database"
);
try_drop!(
conn.execute(&format!("DROP DATABASE IF EXISTS `{}`", database)),
diesel::sql_query(&format!("DROP DATABASE IF EXISTS `{}`", database))
.execute(&mut conn),
"Couldn't drop database"
);
}
Expand Down
6 changes: 4 additions & 2 deletions diesel_cli/tests/support/postgres_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ impl Database {
pub fn create(self) -> Self {
let (database, postgres_url) = self.split_url();
let mut conn = PgConnection::establish(&postgres_url).unwrap();
conn.execute(&format!(r#"CREATE DATABASE "{}""#, database))
diesel::sql_query(&format!(r#"CREATE DATABASE "{}""#, database))
.execute(&mut conn)
.unwrap();
self
}
Expand Down Expand Up @@ -63,7 +64,8 @@ impl Drop for Database {
"Couldn't connect to database"
);
try_drop!(
conn.execute(&format!(r#"DROP DATABASE IF EXISTS "{}""#, database)),
diesel::sql_query(&format!(r#"DROP DATABASE IF EXISTS "{}""#, database))
.execute(&mut conn),
"Couldn't drop database"
);
}
Expand Down

0 comments on commit 7d07f64

Please sign in to comment.