Skip to content

Commit 85d0ecf

Browse files
committed
Test that running migrations adds valid timestamps
This adds a test case which checks that the __diesel_schema_migrations table is initially empty, and that after running a migration, the table is non-empty and the timestamp in the "run_on" has a valid value which can be compared to an older timestamp.
1 parent fcb8ee3 commit 85d0ecf

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

diesel_cli/tests/migration_run.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
use support::{database, project};
2+
use diesel::{select, LoadDsl};
3+
use diesel::expression::sql;
4+
use diesel::types::Bool;
25

36
#[test]
47
fn migration_run_runs_pending_migrations() {
@@ -25,3 +28,55 @@ fn migration_run_runs_pending_migrations() {
2528
"Unexpected stdout {}", result.stdout());
2629
assert!(db.table_exists("users"));
2730
}
31+
32+
#[test]
33+
fn migration_run_inserts_run_on_timestamps() {
34+
let p = project("migration_run_on_timestamps")
35+
.folder("migrations")
36+
.build();
37+
let db = database(&p.database_url());
38+
39+
// Make sure the project is setup.
40+
p.command("setup").run();
41+
42+
p.create_migration("12345_create_users_table",
43+
"CREATE TABLE users ( id INTEGER )",
44+
"DROP TABLE users");
45+
46+
let migrations_done: bool = select(sql::<Bool>(
47+
"EXISTS (SELECT * FROM __diesel_schema_migrations)"))
48+
.get_result(&db.conn())
49+
.unwrap();
50+
assert!(!migrations_done, "Migrations table should be empty");
51+
52+
let result = p.command("migration")
53+
.arg("run")
54+
.run();
55+
56+
assert!(result.is_success(), "Result was unsuccessful {:?}", result);
57+
assert!(db.table_exists("users"));
58+
59+
// By running a query that compares timestamps, we are also checking
60+
// that the auto-inserted values for the "run_on" column are valid.
61+
62+
#[cfg(feature = "sqlite")]
63+
fn valid_run_on_timestamp(db: &database::Database) -> bool {
64+
select(sql::<Bool>("EXISTS (SELECT run_on < DATETIME('now', '-1 hour') \
65+
FROM __diesel_schema_migrations)"))
66+
.get_result(&db.conn())
67+
.unwrap()
68+
}
69+
70+
#[cfg(feature = "postgres")]
71+
fn valid_run_on_timestamp(db: &database::Database) -> bool {
72+
select(sql::<Bool>("EXISTS (SELECT \
73+
run_on < (CAST('now' AS TIMESTAMP) - \
74+
CAST('1 hour' AS INTERVAL)) \
75+
FROM __diesel_schema_migrations)"))
76+
.get_result(&db.conn())
77+
.unwrap()
78+
}
79+
80+
assert!(valid_run_on_timestamp(&db),
81+
"Running a migration did not insert an updated run_on value");
82+
}

0 commit comments

Comments
 (0)