From cbe5cf8ca0cb0ab3d2a11c27509b9d7c79b7e564 Mon Sep 17 00:00:00 2001
From: Felix Ableitner <me@nutomic.com>
Date: Fri, 11 Dec 2020 15:41:39 +0100
Subject: [PATCH] try to run migrations on db connection in tests

---
 Cargo.lock          | 12 +-----------
 Cargo.toml          |  1 -
 lemmy_db/Cargo.toml |  1 +
 lemmy_db/src/lib.rs | 10 ++++++++--
 src/main.rs         |  9 ---------
 5 files changed, 10 insertions(+), 23 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index ef39f5fd..7144eac1 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -935,16 +935,6 @@ dependencies = [
  "lazy_static",
 ]
 
-[[package]]
-name = "ctor"
-version = "0.1.16"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7fbaabec2c953050352311293be5c6aba8e141ba19d6811862b232d6fd020484"
-dependencies = [
- "quote",
- "syn",
-]
-
 [[package]]
 name = "darling"
 version = "0.10.2"
@@ -1809,6 +1799,7 @@ dependencies = [
  "bcrypt",
  "chrono",
  "diesel",
+ "diesel_migrations",
  "lazy_static",
  "lemmy_utils",
  "log",
@@ -1848,7 +1839,6 @@ dependencies = [
  "awc",
  "cargo-husky",
  "chrono",
- "ctor",
  "diesel",
  "diesel_migrations",
  "env_logger",
diff --git a/Cargo.toml b/Cargo.toml
index ad5a6cd5..99d755cc 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -49,7 +49,6 @@ reqwest = { version = "0.10", features = ["json"] }
 activitystreams = "0.7.0-alpha.4"
 actix-rt = { version = "1.1", default-features = false }
 serde_json = { version = "1.0", features = ["preserve_order"]}
-ctor = "0.1.16"
 
 [dev-dependencies.cargo-husky]
 version = "1"
diff --git a/lemmy_db/Cargo.toml b/lemmy_db/Cargo.toml
index 904b1693..f85225e0 100644
--- a/lemmy_db/Cargo.toml
+++ b/lemmy_db/Cargo.toml
@@ -10,6 +10,7 @@ path = "src/lib.rs"
 [dependencies]
 lemmy_utils = { path = "../lemmy_utils" }
 diesel = { version = "1.4", features = ["postgres","chrono","r2d2","64-column-tables","serde_json"] }
+diesel_migrations = "1.4"
 chrono = { version = "0.4", features = ["serde"] }
 serde = { version = "1.0", features = ["derive"] }
 serde_json = { version = "1.0", features = ["preserve_order"]}
diff --git a/lemmy_db/src/lib.rs b/lemmy_db/src/lib.rs
index b90c9416..50aaa39a 100644
--- a/lemmy_db/src/lib.rs
+++ b/lemmy_db/src/lib.rs
@@ -4,6 +4,8 @@ extern crate diesel;
 extern crate strum_macros;
 #[macro_use]
 extern crate lazy_static;
+#[macro_use]
+extern crate diesel_migrations;
 
 use chrono::NaiveDateTime;
 use diesel::{result::Error, *};
@@ -214,11 +216,13 @@ lazy_static! {
 }
 
 #[cfg(test)]
-pub mod tests {
+mod tests {
   use super::fuzzy_search;
   use crate::{get_database_url_from_env, is_email_regex};
   use diesel::{Connection, PgConnection};
 
+  embed_migrations!();
+
   pub fn establish_unpooled_connection() -> PgConnection {
     let db_url = match get_database_url_from_env() {
       Ok(url) => url,
@@ -227,7 +231,9 @@ pub mod tests {
         e
       ),
     };
-    PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url))
+    let conn = PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
+    embedded_migrations::run(&conn).unwrap();
+    conn
   }
 
   #[test]
diff --git a/src/main.rs b/src/main.rs
index 77063185..c55c3655 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -92,12 +92,3 @@ async fn main() -> Result<(), LemmyError> {
 
   Ok(())
 }
-
-#[cfg(test)]
-#[ctor::ctor]
-fn init() {
-  use lemmy_db::tests::establish_unpooled_connection;
-  let conn = establish_unpooled_connection();
-  embedded_migrations::run(&conn).unwrap();
-  run_advanced_migrations(&conn).unwrap();
-}
-- 
2.44.1