From 08748bbededb375f8eb690efcfeeae37c04fe8fa Mon Sep 17 00:00:00 2001
From: Felix Ableitner <me@nutomic.com>
Date: Fri, 11 Dec 2020 14:49:10 +0100
Subject: [PATCH] try to init db from lemmy in tests

---
 .drone.yml          |  3 ---
 Cargo.lock          | 11 +++++++++++
 Cargo.toml          |  1 +
 lemmy_db/src/lib.rs |  2 +-
 src/main.rs         |  9 +++++++++
 5 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/.drone.yml b/.drone.yml
index c9d0a100..c6977ed9 100644
--- a/.drone.yml
+++ b/.drone.yml
@@ -34,9 +34,6 @@ steps:
     commands:
       - sudo apt-get update
       - sudo apt-get -y install --no-install-recommends espeak postgresql-client
-      - cargo install diesel_cli --no-default-features --features postgres --target-dir target/
-      - ls -la target/
-      - ./target/diesel migration run
       - cargo test --workspace --no-fail-fast
 
   - name: cargo build
diff --git a/Cargo.lock b/Cargo.lock
index cf741d68..ef39f5fd 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -935,6 +935,16 @@ 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"
@@ -1838,6 +1848,7 @@ dependencies = [
  "awc",
  "cargo-husky",
  "chrono",
+ "ctor",
  "diesel",
  "diesel_migrations",
  "env_logger",
diff --git a/Cargo.toml b/Cargo.toml
index 99d755cc..ad5a6cd5 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -49,6 +49,7 @@ 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/src/lib.rs b/lemmy_db/src/lib.rs
index bad646d1..b90c9416 100644
--- a/lemmy_db/src/lib.rs
+++ b/lemmy_db/src/lib.rs
@@ -214,7 +214,7 @@ lazy_static! {
 }
 
 #[cfg(test)]
-mod tests {
+pub mod tests {
   use super::fuzzy_search;
   use crate::{get_database_url_from_env, is_email_regex};
   use diesel::{Connection, PgConnection};
diff --git a/src/main.rs b/src/main.rs
index c55c3655..31c5f970 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -92,3 +92,12 @@ 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();
+}
\ No newline at end of file
-- 
2.44.1