Skip to content

Commit 58b1a83

Browse files
authored
Merge pull request diesel-rs#714 from Eijebong/dont_panic_on_empty_migration
Don't panic when running empty migrations.
2 parents 1c0b29b + d1a7a79 commit 58b1a83

5 files changed

Lines changed: 50 additions & 0 deletions

File tree

diesel/src/migrations/migration.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ fn run_sql_from_file(conn: &SimpleConnection, path: &Path) -> Result<(), RunMigr
104104
let mut sql = String::new();
105105
let mut file = try!(File::open(path));
106106
try!(file.read_to_string(&mut sql));
107+
108+
if sql.is_empty() {
109+
return Err(RunMigrationsError::EmptyMigration);
110+
}
111+
107112
try!(conn.batch_execute(&sql));
108113
Ok(())
109114
}

diesel/src/migrations/migration_error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@ impl From<io::Error> for MigrationError {
6565
pub enum RunMigrationsError {
6666
MigrationError(MigrationError),
6767
QueryError(result::Error),
68+
EmptyMigration,
6869
}
6970

7071
impl Error for RunMigrationsError {
7172
fn description(&self) -> &str {
7273
match *self {
7374
RunMigrationsError::MigrationError(ref error) => error.description(),
7475
RunMigrationsError::QueryError(ref error) => error.description(),
76+
RunMigrationsError::EmptyMigration => "Attempted to run an empty migration.",
7577
}
7678
}
7779
}

diesel/src/pg/connection/result.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ impl PgResult {
2424
internal_result: internal_result,
2525
})
2626
},
27+
PGRES_EMPTY_QUERY => {
28+
let error_message = "Received an empty query".to_string();
29+
Err(Error::DatabaseError(DatabaseErrorKind::__Unknown, Box::new(error_message)))
30+
},
2731
_ => {
2832
let error_kind = match get_result_field(internal_result.as_ptr(), ResultField::SqlState) {
2933
Some(error_codes::UNIQUE_VIOLATION) => DatabaseErrorKind::UniqueViolation,

diesel_cli/tests/migration_run.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,25 @@ fn migration_run_inserts_run_on_timestamps() {
8686
assert!(valid_run_on_timestamp(&db),
8787
"Running a migration did not insert an updated run_on value");
8888
}
89+
90+
#[test]
91+
fn empty_migrations_are_not_valid() {
92+
let p = project("migration_run_empty")
93+
.folder("migrations")
94+
.build();
95+
96+
p.command("setup").run();
97+
98+
p.create_migration(
99+
"12345_empty_migration",
100+
"",
101+
""
102+
);
103+
104+
let result = p.command("migration")
105+
.arg("run")
106+
.run();
107+
108+
assert!(!result.is_success());
109+
assert!(result.stdout().contains("empty migration"));
110+
}

diesel_tests/tests/internal_details.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,20 @@ fn query_which_cannot_be_transmitted_gives_proper_error_message() {
3232
Err(_) => panic!("We got back the wrong kind of error. This test is invalid."),
3333
}
3434
}
35+
36+
#[test]
37+
#[cfg(feature="postgres")]
38+
fn empty_query_gives_proper_error_instead_of_panicking() {
39+
use diesel::result::Error::DatabaseError;
40+
use diesel::result::DatabaseErrorKind::__Unknown;
41+
use diesel::expression::dsl::sql;
42+
43+
let connection = connection();
44+
let query = sql::<Integer>("");
45+
46+
match query.execute(&connection) {
47+
Ok(_) => panic!("We successfully executed an empty query"),
48+
Err(DatabaseError(__Unknown, info)) => assert_ne!("", info.message()),
49+
Err(_) => panic!("We got back the wrong kind of error. This test is invalid."),
50+
}
51+
}

0 commit comments

Comments
 (0)